JNDI Howto

The Java Naming and Directory Interface (JNDI) is part of the Java platform, providing applications based on Java technology with a unified interface to multiple naming and directory services. You can build powerful and portable directory-enabled applications using this industry standard.

When you deploy your application inside an application server, the container will setup the JNDI tree for you. But if you are writing a framework or just a standalone application, then the following examples will show you how to construct and bind references to DBCP datasources.

Another source of information is Naming. Naming includes an in-memory JNDI service provider that was extracted from the Jakarta Tomcat JNDI implementation. It also contains a easy way to construct a JNDI tree from an XML file and some ResourceFactories.

The following examples are using the sun filesystem JNDI service provider. You can download it from the JNDI software download page.

You can of course use the apache JNDI service provider by downloading the Naming core jar. The initial context factory property should be changed to: org.apache.naming.java.javaURLContextFactory

BasicDataSource

  System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
  System.setProperty(Context.PROVIDER_URL, "file:///tmp");
  InitialContext ic = new InitialContext();

  // Construct BasicDataSource reference
  Reference ref = new Reference("javax.sql.DataSource",
    "org.apache.commons.dbcp.BasicDataSourceFactory", null);
  ref.add(new StringRefAddr("driverClassName", "org.apache.commons.dbcp.TesterDriver"));
  ref.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
  ref.add(new StringRefAddr("username", "username"));
  ref.add(new StringRefAddr("password", "password"));
  ic.rebind("jdbc/basic", ref);
   
  // Use
  InitialContext ic2 = new InitialContext();
  DataSource ds = (DataSource) ic2.lookup("jdbc/basic");
  assertNotNull(ds);
  Connection conn = ds.getConnection();
  assertNotNull(conn);
  conn.close();

PerUserPoolDataSource

  System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
  System.setProperty(Context.PROVIDER_URL, "file:///tmp");
  InitialContext ic = new InitialContext();

  // Construct DriverAdapterCPDS reference
  Reference cpdsRef = new Reference("org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS",
    "org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS", null);
  cpdsRef.add(new StringRefAddr("driver", "org.apache.commons.dbcp.TesterDriver"));
  cpdsRef.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
  cpdsRef.add(new StringRefAddr("user", "foo"));
  cpdsRef.add(new StringRefAddr("password", "bar"));
  ic.rebind("jdbc/cpds", cpdsRef);
     
  // Construct PerUserPoolDataSource reference
  Reference ref = new Reference("org.apache.commons.dbcp.datasources.PerUserPoolDataSource",
    "org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory", null);
  ref.add(new StringRefAddr("dataSourceName", "jdbc/cpds"));
  ref.add(new StringRefAddr("defaultMaxActive", "100"));
  ref.add(new StringRefAddr("defaultMaxIdle", "30"));
  ref.add(new StringRefAddr("defaultMaxWait", "10000"));
  ic.rebind("jdbc/peruser", ref);
     
  // Use
  InitialContext ic2 = new InitialContext();
  DataSource ds = (DataSource) ic2.lookup("jdbc/peruser");
  assertNotNull(ds);
  Connection conn = ds.getConnection("foo","bar");
  assertNotNull(conn);
  conn.close();