when is connection pool created?

Hi,

I declared a connection pool in META-INF/context.xml as follow:

<Context path="/Core1" docBase="Core1"

debug="5" reloadable="true" crossContext="false">

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="guest" password="iamguest" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://lightportal/core?autoReconnect=true"/>

</Context>

I also have a ulity UserRegistryBean which must use a db connection to authenticate user. The UserRegistryBean has a

setDataSource(DataSource ds) method. Now, the question is:

When i create a UserRegistryBean in the contextInitialized() of a ServletContextListener how can i set datasource for my UserRegistryBean? I cannot get DataSource from a ServletContextListener.

Is the connection pool i declare in context.xml created before the contextInitialized() of the ServletContextListener invoked?

[1390 byte] By [lnthai2002a] at [2007-11-27 9:28:55]
# 1

Tomcat uses the config file "context.xml" to look through your jar files for any object that implements the datasource interface. If you have ojdbc14.jar for Oracle as your JDBC included in your project, it finds it in there. It now implements a datasource that any project (including your project) can use. Your program use JNDI to get a reference of the datasource for your program. See

http://tomcat.apache.org/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html

Example: Put the following in your init() of your controller servlet (the one that handles all your url traffic for your project). Its run only once when the first person logs in and the datasource stays in existance for all users who log in afterwards.

InitialContext initCtx = new InitialContext();

DataSource ds = (DataSource) initCtx.lookup("java:comp/env/my-datasource");

Connection conn = ds.getConnection();

However, make DataSource ds a static class variable in your servlet rather than a local variable (since it must stay in existance even after init() finishes).

George123a at 2007-7-12 22:35:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

OK, i follow the example in tomcat doc.

Here is my server.xml:

<Server port="8005" shutdown="SHUTDOWN">

<Listener className="org.apache.catalina.core.AprLifecycleListener" />

<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />

<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>

<GlobalNamingResources>

<Environment name="simpleValue" type="java.lang.Integer" value="30"/>

<Resource name="UserDatabase" auth="Container"

type="org.apache.catalina.UserDatabase"

description="User database that can be updated and saved"

factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

pathname="conf/tomcat-users.xml" />

</GlobalNamingResources>

<Service name="Catalina">

<Connector port="8080" maxHttpHeaderSize="8192"

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

connectionTimeout="20000" disableUploadTimeout="true" />

<Connector port="8009"

enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

<Engine name="Catalina" defaultHost="localhost">

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"

resourceName="UserDatabase"/>

<Host name="localhost" appBase="webapps"

unpackWARs="true" autoDeploy="true"

xmlValidation="false" xmlNamespaceAware="false">

<Context path="/Core1" docBase="Core1" debug="5" reloadable="true" crossContext="true">

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="me" password="iam" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/mydb?autoReconnect=true"/>

</Context>

</Host>

</Engine>

</Service>

</Server>

And here is my WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<filter>

<filter-name>AccessControl</filter-name>

<filter-class>com.core1.filter.AccessControl</filter-class>

<init-param>

<param-name>loginPage</param-name>

<param-value>/WEB-INF/authentication/login.jsp</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>AccessControl</filter-name>

<url-pattern>/protected/*</url-pattern>

</filter-mapping>

<listener>

<listener-class>com.core1.listener.ResourceManagerListener</listener-class>

</listener>

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>

index.jsp

</welcome-file>

</welcome-file-list>

<error-page>

<exception-type>Exception</exception-type>

<location>/WEB-INF/error/error.jsp</location>

</error-page>

<resource-ref>

<description>DB connection</description>

<res-ref-name>jdbc/TestDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

I assume that by referencing the DataSource in WEB-INF/web.xml to the DataSource initialized in server.xml, I can access the Datasource from anywhere. However, in one of my Action Class(i'm using Struts framework) I have the following code

DataSource ds = this.getDataSource(request,"jdbc/TestDB");

if(ds == null)

throw new InternalException("datasource is null");

Supprisingly, the exception is thrown! The DataSource is null after all.Is there anything else i have to do?

lnthai2002a at 2007-7-12 22:35:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
You cant get the datasource referenced in the tomcat context.xml file tag from the request scope, context scope, etc.You need to optain it via JDNI. I dont have an example to show. So if you look up 'JDNI' and 'datasource' you should be able to find examples.
George123a at 2007-7-12 22:35:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...