Friday, 15 July 2011

Shared connection pool between multiple webapps of tomcat

For configuring a container managed connection pool you define a resource in context.xml as

<ResourceLink
name="jdbc/myoracle"
global="shared/jdbc/myoracle"
type="javax.sql.DataSource"
factory="org.apache.naming.factory.DataSourceLinkFactory"
username="username"
password="password" />

and do a JNDI lookup to access the resource and get a db connection. Even though the connection pool is maintained by the container, it creates a pool for every webapp on tomcat and doesn't really share the pool between webapps. This usually causes your environments use up too many connections and result in session unavailable exceptions.

To make your webapps use a truely sharable connection pool across all webapps on the container, use the following configuration

1. Remove any definition you have from context.xml (These configuration files are usually found on tomcat_home/conf)
2. Move this definition to server.xml and copy under as

<GlobalNamingResources>
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver"
maxActive="2" maxIdle="1" maxWait="-1"
name="shared/jdbc/myoracle" password="password"
type="javax.sql.DataSource" url="jdbc:oracle:thin:@server:1521:XE" username="username"/>
</GlobalNamingResources>

3. Link this resource to your webapplication context by copying the below xml under element

<ResourceLink
name="jdbc/myoracle"
global="shared/jdbc/myoracle"
type="javax.sql.DataSource"
factory="org.apache.naming.factory.DataSourceLinkFactory"
username="username"
password="password" />
4. Restart tomcat
5. Look into your database administration console to verify the connections you are holding.

Now, all your web application contexts link to just a global truely shared connection pool and use less connections.

No comments:

Post a Comment