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

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

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

BasicDataSource ds = new BasicDataSource();
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 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
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


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
in the same application.

package com.blah.blah.util.jndi;

import tyrex.resource.Resources;
import tyrex.resource.ResourceException;

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 {
TransactionDomain domain = TransactionDomain.createDomain(
"domain.xml" );
DataSource dataSource = (DataSource) domain.getResources().

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 ,
throw new java.lang.RuntimeException(
"JNDI Context failed to initialize. : " + ex.getMessage() );

// ignore
public void cleanup() {