Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

How to use connection pool with tomcat 1.2.9

Nhut Thai Le

2007-07-07

Replies:

Hi guys,
I 'm developing a simple web app with netbeans with bundle tomcat 1.2.9. I want to use connection pooling fir my web app. Thus, i config as follow:
 
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? It's been 2 weeks i have stuck with this problem. Hope anyone can give a suggestion
 
Thai


-------------------------
Where there's will, there's a way

---------------------------------
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
©2008 junlu.com - Jax Systems, LLC, U.S.A.