javax.naming.NameNotFoundException: getting JNDI to work

javax.naming.NameNotFoundException: getting JNDI to work

Post by jonc » Fri, 04 Feb 2005 04:02:11


Hi all,
I would like to set up a DBCP connection pool for my stand-alone
application (so not in Tomcat, for which documentation is abundant).
However, I keep getting an "javax.naming.NameNotFoundException: Name
jdbc is not bound in this Context" exception. I have tried everything I
could think of and I'm stumped.
First of all, I tried following the tutorial at
http://www.yqcomputer.com/

After replacing the driverClassName, url, username and password with
real values, this code gives me the NameNotFoundException when I try to
rebind however. So I tried adding the line

ref.add(new StringRefAddr("dataSourceName", "jdbc/sender")); (before
rebinding)

but this improved nothing. So I then tried creating a BasicDataSource
by hand as follows:

BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useUnicode=true");
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername("xxxxxxxxxx");
ds.setPassword("xxxxxxxxxx");
Context ctx = new InitialContext();
ctx.bind("jdbc/test", ds);

It gives exactly the same error. Obviously I'm missing something
crucial here, but many hours of Googling have not turned up an answer
for me.

Could someone please point me in the right direction?
Thanks very much, Jonck
 
 
 

javax.naming.NameNotFoundException: getting JNDI to work

Post by gimme_this » Sat, 05 Feb 2005 07:08:49

use tyrex-1.0.1 from sourceforge.net to create a JNDI Context for my
database connections.

With tyrex you can create JNDIContexts for about anything - it doesn't
have to be a database.

I find it handy when executing junit tests which do database queries.
(For example, it works great with Hibernate if you are using a custom
connection
provider class.)

That is, with tyrex you can use the same connection classes in your web
applications as you do in junit or standalone applications.

To give you an idea ....

For tyrex, I'm using Oracle, so my (tyrex) domain.xml file looks like :
You use domain.xml file sort of like a tomcat/configure/server.xml
file.

<domain>
<name>BlahBlahDataSources</name>
<resources>
<dataSource>
<jar>C:\oracle\ora92\jdbc\lib\classes12.zip</jar>
<name>BlahBlahDataSource</name>
<class>oracle.jdbc.xa.client.OracleXADataSource</class>
<config>
<serverName>blah.blah.myoracle.db.server.com</serverName>
<portNumber>1521</portNumber>
<databaseName>blahblah</databaseName>
<user>blahblah</user>
<password>blahblah</password>
<driverType>thin</driverType>
</config>
<limits>
<maximum>50</maximum>
<minimum>5</minimum>
<initial>5</initial>
<maxRetain>300</maxRetain>
<timeout>10</timeout>
</limits>
</dataSource>
</resources>
</domain>

If you search around you can find a domain.xml compatible with MySQL.
A MySQL demo might even come with tyrex.


Then I have the following class which builds the JNDIContext for the
standalone (junit). It's abstract so I can connect to multiple
databases
in the same application.


package com.blah.blah.util.jndi;

import tyrex.resource.Resources;
import tyrex.resource.ResourceException;
import tyrex.tm.TransactionDomain;
import tyrex.tm.RuntimeContext;
import tyrex.tm.DomainConfigurationException;
import tyrex.tm.RecoveryException;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLExclseption;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


// This class is used to create a JNDI context for junit testing.

public abstract class JNDIContext {

public JNDIContext(){};

protected String DATASOURCE = null; // Assigned in subclass

protected RuntimeContext runCtx = null;

public void startUp(String DATASOURCE) {
try {
this.DATASOURCE=DATASOURCE;
TransactionDomain domain = TransactionDomain.createDomain(
"domain.xml" );
domain.recover();
DataSource dataSource = (DataSource) domain.getResources().
getResource(DATASOURCE);

runCtx = RuntimeContext.newRuntimeContext();
Context jndiCtx = runCtx.getEnvContext();

jndiCtx = jndiCtx.createSubcontext( "comp" );
jndiCtx = jndiCtx.createSubcontext( "env" );
jndiCtx = jndiCtx.createSubcontext( "jdbc" );
jndiCtx.bind( DATASOURCE , dataSource );

RuntimeContext.setRuntimeContext( runCtx );

} catch ( Exception ex) {
// DomainConfigurationException, RecoveryException, NamingException ,
ResourceException
throw new java.lang.RuntimeException(
"JNDI Context failed to initialize. : " + ex.getMessage() );
}
}

// ignore
public void cleanup() {