tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jan Behrens" <>
Subject RE: tomcat problem related connection pooling help needed
Date Sun, 19 Dec 2004 13:55:04 GMT
ok, i will share what little knowledge i have...

when you "normaly" open a database connection, you get a connection object
that represents a database connection, you allways get a new connection and
have to ensure that this is definetly closed after usage. there a quite a
few drawbacks to this as you _must_ close the connection and in that the
number of java connection objects have to correspond to those maximal
available from your dbms (e.g. mysql). 

my first webapp at university was build like that and worked fine till i got
load on the system - then it crashed ;)

a connection pool helps you in avoiding mentioned problems. it basically is
a pool (funny that :) managed by a pool provider that consist of a limited
number of actual connection objects. these get created (and in specific
cases even destroyed/closed) by the pool provider. all you have to do is
configure it according to your specifications and then changed your db code
to request connections from the pool.

tomcat, beeing the great server it is, comes with a pool provider that you
could use to start with. i am not sure whether it is recommended for
production so you should check that before going live with your system. you
usually will set up a connection pool in combination with a jndi ressource
that provides everything needed for your app. 

first created the mentioned jndi by editing your applications xml file under


you will need to insert a new ressource within the context-tag. the
following should serve as a general guideline, you will have to change
names, paths etc according to your setup of course :)

<?xml version='1.0' encoding='utf-8'?>
<Context ... >
  <Resource auth="Container" name="jdbc/yourDatasourceName"
  <ResourceParams name="jdbc/yourDatasourceName">

the following two methods illustrate how to retrieve the session from the
context afterwards. NOTE: these are quite old as i have been using hibernate
for quite some time now, so use them as a general guideline only!

    private Connection connect() {
	  Connection conn = null;

        try {
            Context ctx = new InitialContext();

            if (ctx == null) {
                throw new Exception(
                        "BaseTable.connect() --> Could not get Database

            DataSource ds = (DataSource)

            if (ds != null) {
                conn = ds.getConnection();
        } catch (Exception e) {
            logger.error(" Connection error! ", e);

        return conn;

    private void disconnect(Connection conn) {
        try {
            java.sql.SQLWarning sqlw = conn.getWarnings();

            while (sqlw != null) {
                logger.error(" SQL Warnings: " + 
                sqlw = sqlw.getNextWarning();

        } catch (Exception e) {
                logger.error(" Error closing connection ", e);

        conn = null;

there are several good tutorials on this topic as well (just google or look
at either the tomcat or mysql driver manual) and i would highly recommend
looking into them, especially to ensure that you are closing every statement
and connection and that connections are correctly returned to the pool...

hth, jan

> -----Original Message-----
> From: Krishnakant Mane [] 
> Sent: Sunday, December 19, 2004 2:17 PM
> To:
> Subject: tomcat problem related connection pooling help needed
> hello Jan and other friends,
> I read the mail that was sent as a reply for my
> problem.
> thanks jan for ur valuable help.
> I must mention that I am new to the jdbc stuff.  so I
> could not figure out the connection pooling issue.  I 
> understand that with pooling I can have live objects of say 
> connection objects that I can use as a pool and that tomcat 
> can send them when needed. but I don't exactly know how to do 
> it please guide me abt jdbc connection pooling.  I know that 
> it is out of topic on this list, but to understand jdbc 
> pooling and my problem related to tomcat, I must first 
> understand in short how pooling in jdbc works. plese help me 
> on these issues. thanks Krishnakant.
> ___________________________________________________________ 
> Win a castle for NYE with your mates and Yahoo! Messenger 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message