<<
Tomcat Data Sources
There are 2 methods for defining a Tomcat datasource:
Old Method
This method still applies if you want to share a datasource between web applications
Edit
conf/server.xml adding a new
Resource to the
GlobalNamingResources
<Resource name="jdbc/[name]"
auth="Container"
type="javax.sql.DataSource"
username="[username]"
password="[password]"
driverClassName="[driver class]"
url="[server connection url]"
maxActive="8"
/>
Create a new file,
<context>/META-INF/context.xml with the following content
<ResourceLink global="jdbc/[name]" name="jdbc/[name]" type="javax.sql.DataSource"/>
add a
resource-ref to your web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/[name]</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
reference the datasource as
java:env/jdbc/[name] in your application.
New Method
This only applies to Tomcat 5.5+
Copy the JDBC Driver to the Tomcat common/lib folder
Create a new file,
<context>/META-INF/context.xml with the following content
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/[name]"
auth="Container"
type="javax.sql.DataSource"
username="[username]"
password="[password]"
driverClassName="[driver class]"
url="[server connection url]"
maxActive="8"
/>
</Context>
reference the datasource as
java:env/jdbc/[name] in your application.
MySQL datasource tweaks
For improved MySQL performance, try defining your datasource as follows
<Resource
name="jdbc/[mydb]"
auth="Container"
type="javax.sql.DataSource"
description="MySQL database"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
maxActive="150"
maxIdle="40"
maxWait="10000"
username="[user]"
password="[pass]"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/[db]?autoReconnect=true"
removeAbandoned="true"
logAbandoned="true"
removeAbandonedTimeout="300"
testOnBorrow="true"
testOnReturn="true"
validationQuery="select 1"
/>