JNDI DBCP Example for FireBird

Example context.xml file

This file provides a JNDI resource representing a FireBird datasource, to allow your application to use DBCP connection pooling.

You will need to replace YOUR_USERNAME, YOUR_PASSWORD, YOUR_DATABASE_NAME, and if necessary, the server name.

Save this file as <webapps>/<your application folder (eg: ROOT)>/META-INF/context.xml

Note that most applications do not have a META-INF folder by default. Create it next to the WEB-INF folder, so that your application contains both a WEB-INF and a META-INF folder.

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/mydatabase" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="YOUR_USERNAME" 
              password="YOUR_PASSWORD"
              driverClassName="org.firebirdsql.jdbc.FBDriver"
              url="jdbc:firebirdsql://firebird.metawerx.net:3050/d:/firebird/data/YOUR_DATABASE_NAME.FDB"
              validationQuery="select 1"
              maxActive="10" 
              maxIdle="2"/>
</Context>

Accessing the datasource from Java

This datasource can be accessed in your java code with the following line:

// Get DataSource
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydatabase");
// Get Connection and Statement
Connection c = ds.getConnection();
Statement s = c.createStatement();

Tips

  • Remember to close your connections, especially after Exceptions, otherwise you will run out of connections. In this case your application will appear to have frozen, as it waits endlessly for a connection to become available from the pool. For more info, see Connection Leak.
  • When using context.xml or server.xml to initiate the pool in this way, the JDBC driver JAR file must be available in <jakarta>/common/lib. Simply placing it into <app>/WEB-INF/lib will not work. This is because the context starts before your application is loaded, so the WEB-INF/lib CLASSPATH does not exist at the time the connection pool is initiated.
  • If you get a javax.naming.NameNotFoundException error, you have either not installed the context.xml file correctly, or have used a different name in the context.xml <Resource> element's "name" attribute to what you are using in your Context.lookup() call.

More Information

navigation
metawerx specific
search
Share
tools
help

referring pages

Share