Hi,

Hello, 

I'd like to know how to implement connection pooling in my
application.
This is what I had done so far.  Pls let me know what I need to change

to use the pooling mechanism.

1.  I created a singleton class: DatabaseOperations.
2.  It has an Connection instance: conn.
3.  During initialization, it gets connection as follows:
     DataSource ds  = (DataSource)
ctx.lookup("java:comp/env/jdbc/mySQLDatabase");
     conn = ds.getConnection();

4.  I have several public operations method (e.g selectOperations(...),
insertOperations(...)).
5.  Let's say I'd like to perform a select statement.  Inside my
selectOperations(...):
     Statement stmt = conn.createStatement();
     ResultSet rs = stmt.executeQuery(...);
     <process the result>

     In my finally block, I have:
     rslt.close();
     stmt.close();

Questions:
1.  Do I need to code any different to enable the connection pooling?
        NO

2.  When I called ds.getConnection(), does it opens up several
connections for pooling
     or just one connection?
        Depends on how you have it configured. maxActive and maxIdle settings for the 
resource.

3.  Do I need to close conn every time as well?  Since conn is an
instance, 
     how does it gets connection everytime an operation method is
called? (insert, select, update).

        Yes, you have to close the connection each time, which returns the connection 
to the pool for
other processes to use. 

I have the following methods in my generic DataBase Operations class.
    static public Connection getConnection() throws SQLException {
        Connection conn = null;
        try{
            conn = ds.getConnection();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    static public void releaseConnection(Connection con){
        try{
        if (con != null)
            con.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

which get and close my connections for me. So, when I am in the selectOperations(..) I 
call the getConnetion()
to get a connection and in the finally block I call the releaseConnection() method.


Hope this helps.
Kal.


CONFIDENTIALITY NOTE:  All e-mail sent to or from this address will be received by the 
Waterfield Group corporate e-mail system and is subject to archival, monitoring, 
and/or review by someone other than the recipient or the sender.

This e-mail and any of its attachments may contain proprietary information, which is 
privileged and confidential.  This e-mail is intended solely for the use of the 
individual or entity to which it is addressed.  If you are not the intended recipient 
of this e-mail, you are hereby notified that any dissemination, distribution, copying, 
or action taken in relation to the contents of and attachments to this e-mail is 
strictly prohibited and may be unlawful.  If you have received this e-mail in error, 
please notify the sender immediately and permanently delete the original and any copy 
of this e-mail and any printout.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to