users@glassfish.java.net

Re: Glassfish v2 ur2 DB connection pooling problem

From: <glassfish_at_javadesktop.org>
Date: Wed, 09 Jul 2008 00:15:24 PDT

Hi Shailini,

First of all thanx's for replying my post .. I didn't know that the physical connection is got by ds.geConnection(con)

I am mailing you some code snippets related to exceptions that are thrown...

public class Connect_1 {
    
    private static Connect_1 connect = null ;
    
    public static Connect_1 getConnect() throws Exception {
        if( connect == null)
        {
            System.out.println("Creating New Connect Object");
            connect = new Connect_1();
        }
        return connect;
    }
    
    private DataSource ds = null;
    
    private Connect_1() throws Exception {
        ds = getDataSource();
    }
    
    private DataSource getDataSource() throws Exception {
        InitialContext cxt = new InitialContext();
        if ( cxt == null ) {
            throw new Exception("Uh oh -- no context!");
        }
        DataSource ds_1 = (DataSource) cxt.lookup( "java:comp/env/jdbc/postgres" );
        if ( ds_1 == null ) {
            throw new Exception("Data source not found!");
        }
        // System.out.println("Returning new Data Source");
        return ds_1;
    }

    public synchronized static void closeConnection(ResultSet rs) {
         System.out.println("Closing Connection");
        Connection con = null ;
        Statement stat = null ;
        try {
            stat = rs.getStatement();
            con = stat.getConnection();
            rs.close(); rs=null;
            stat.close() ; stat = null;
            con.setAutoCommit(true);
            con.close() ;
            System.out.println(" Connection closed");
            con = null ;
        } catch (Exception e) {
            System.out.println("ClOSE CON: "+e.getMessage());
            e.printStackTrace();
        }finally {
            // Always make sure result sets and statements are closed,
            // and the connection is returned to the pool
            if (rs != null) {
                try { rs.close(); } catch (SQLException e) { ; }
                rs = null;
            }
            if (stat != null) {
                try { stat.close(); } catch (SQLException e) { ; }
                stat = null;
            }
            if (con != null) {
                System.out.println(" con not null Closing Connection");
                try {con.setAutoCommit(true) ;con.close(); System.out.println(" finally Connection closed");} catch (SQLException e) {e.getMessage() ; e.printStackTrace(); }
                con = null;
            }
            
        }
    }

    public ResultSet execSelect(String qry) throws SQLException {
        return this.getConnection().createStatement().executeQuery(qry);
    }

} //end of connect_1 class

hraps.SessionBean1.<init>(SessionBean1.java:80) (this is the start point)
start ........
                ResultSet rs = null;
                try {
                        String qry = "select a.user_desc_name , role_desc_name , to_char(last_log_in,'dd-Mon-yyyy hh:mi:ss') , eno , user_lock from users a , user_roles b where a.user_name='"+ extc.getRemoteUser().trim()+"' and a.user_name=b.user_name";
                        rs = Connect_1.getConnect().execSelect(qry); /// line no 80
                        if(rs.next()) {
                                      ///some code
                                }
                        }
                        
                } catch (Exception e) {
                        System.out.println("exception:"+ e.getMessage());
                        throw new FacesException(e);
                } finally {
                        Connect_1.closeConnection(rs);
                }
... end


I hope the code written is correct and there is not connection leak. It is running successfully in TOMCAT 5.5 - 6.0.

I have enabled leak so getting those messages.

Please comment on it.
[Message sent by forum member 'pratham_vishnu' (pratham_vishnu)]

http://forums.java.net/jive/thread.jspa?messageID=285286