de.mguennewig.pobjects.jdbc
Class JndiContainerPool

java.lang.Object
  extended by de.mguennewig.pobjects.jdbc.ContainerPool
      extended by de.mguennewig.pobjects.jdbc.JndiContainerPool

public class JndiContainerPool
extends ContainerPool

Provides a pool of JdbcContainer that uses JNDI to get connections.

To use this JNDI Container pool with Tomcat you have to define up to 3 things in the servlets Context block within Tomcat's server.xml file. Which then have to be referenced from the servlets web.xml file.

<jndiName>/source
The DataSource to use to get database connections.
<jndiName>/container
The class name of a JdbcContainer instance that works together with the configured database.
<jndiName>/schema
This optional context specifies the database schema to use and defaults to the empty string if not given.

An example servlet.xml configuration could look similar to this:

<Resource name="jdbc/pobjtest/source" auth="Container"
              type="javax.sql.DataSource" />

    <ResourceParams name="jdbc/pobjtest/source">
      <parameter>
        <name>factory</name>
        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
      </parameter>

      <parameter>
        <name>driverClassName</name>
        <value>com.mysql.jdbc.Driver</value>
      </parameter>

      <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost:3306/pobjtest?autoReconnect=true</value>
      </parameter>

      <parameter>
        <name>username</name>
        <value>pobjtest</value>
      </parameter>
      <parameter>
        <name>password</name>
        <value />
      </parameter>
    </ResourceParams>

    <Environment name="jdbc/pobjtest/container" type="java.lang.String"
                 value="de.mguennewig.pobjects.jdbc.MySQLContainer" />

The corresponding web.xml file sections would look like this:

  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/pobjtest/source</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  <env-entry>
      <description>DB Container</description>
      <env-entry-name>jdbc/pobjtest/container</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
  </env-entry>

To use the above configured database, get an instance of this pool within your servlet in the following way:

  ContainerPool pool =
    new JndiContainerPool(dictionary, "java:/comp/env/jdbc/pobjtest");

Author:
Michael Günnewig

Constructor Summary
JndiContainerPool(PObjDictionary dict, java.lang.String jndiName)
          Creates a new JNDI base container pool.
 
Method Summary
 
Methods inherited from class de.mguennewig.pobjects.jdbc.ContainerPool
freeConnection, freeContainer, getConnection, getContainer, getDictionary, getSchema
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JndiContainerPool

public JndiContainerPool(PObjDictionary dict,
                         java.lang.String jndiName)
                  throws javax.naming.NamingException
Creates a new JNDI base container pool.

The data source resource must be named <jndiName>/source, the schema name resource <jndiName>/schema and the container class name resource <jndiName>/container.

Throws:
javax.naming.NamingException - if any JNDI error occurs.