Follow these simple steps to setup Database Connection Pooling inside Tomcat 6.x
Step 1
Add following element to conf/context.xml file. This Resource will be avaible to all Applications deployed
<Resource name="jdbc/SampleDb" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="3" maxWait="10000"
username="" password=""
driverClassName=""
url=""/>
provide values for all attributes (username, password, driverClassName, url etc)
Step 2
Add following to web.xml of your Application
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/SampleDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step 3
Refer JNDI Data source inside your persistence.xml
If you want to use JTA use this configuration
<persistence-unit name="SamplePU" transaction-type="JTA">
<jta-data-source>java:/comp/env/jdbc/SampleDB</jta-data-source>
..
</persistence-unit>
and for non JTA
<persistence-unit name="SamplePU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:/comp/env/jdbc/SampleDB</non-jta-data-source>
..
</persistence-unit>
Step 1
Add following element to conf/context.xml file. This Resource will be avaible to all Applications deployed
<Resource name="jdbc/SampleDb" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="3" maxWait="10000"
username="" password=""
driverClassName=""
url=""/>
provide values for all attributes (username, password, driverClassName, url etc)
Step 2
Add following to web.xml of your Application
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/SampleDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step 3
Refer JNDI Data source inside your persistence.xml
If you want to use JTA use this configuration
<persistence-unit name="SamplePU" transaction-type="JTA">
<jta-data-source>java:/comp/env/jdbc/SampleDB</jta-data-source>
..
</persistence-unit>
and for non JTA
<persistence-unit name="SamplePU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:/comp/env/jdbc/SampleDB</non-jta-data-source>
..
</persistence-unit>
Comments