Re: connection problem - why idle - Mailing list pgsql-jdbc

From Andrew Nelson
Subject Re: connection problem - why idle
Date
Msg-id s0490f7b.035@gwdom2-med.med.utah.edu
Whole thread Raw
In response to connection problem - why idle  (dinakar <din_akar@yahoo.com>)
List pgsql-jdbc
I don't really know for sure but you should probably be a bit more
carefull when closing your connections.
I personnally am extremly anal and do it as follows.

Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try {
// initialize conn, statements and resultsets here
// then do sql
// ...
rs.close(); rs = null;
stmt.close(); rs = null;
conn.close(); conn = null;
// you may also want to include something like this to see if
connection are ever closed
System.out.println("connection successfully closed in try");
}finally {
if(rs!=null) {
try { rs.close(); } catch(Exception e) {}
}
if(stmt != null) {
try {stmt.close();} catch(Exception e) {}
}
if(conn != null) {
try {conn.close(); System.out.println("connection successfully closed
in finally");} catch(Exception e) {}
}
}

If worst comes to worse you could maybe configure the tomcat connection
pool to start blocking at 99 connections.

>>> dinakar <din_akar@yahoo.com> 03/05/04 10:30PM >>>
Hi All,

 We have hosted an web application and now it is live.


 s/w used : Apache, Tomcat 4.1.x, Postgresql 7.x

 It seems that there is a major problem with the
database connectivity.

 What we are seeing is that over a period of time the
database reports more and more connections from the
our web application until the connection limit (100 on
our setup) is reached.

this is a sample code i am using across application.

whether we need to release the connection explicitly ?

As of now i have not released the connection only i
have closed it .... ?

/*****************************************************/

       -------------my db class-----------------
try {

    this.setConnection(ConnectionPool.getConnection());
    ArrayList arrList = new ArrayList();

    con.setAutoCommit(false);
    preStmt     = con.prepareStatement("SELECT * from t");
    resultSet     = preStmt.executeQuery();

    while (resultSet.next()) {
        .....
        .....
    }
    return arrList;
}
catch(Exception ex) {
    sberror.append(ex.getMessage());
    return null;
}

finally {
    try {
        con.setAutoCommit(true);
        resultSet.close();
        preStmt.close();
        con.close();
    }
    catch (SQLException e) {
           sberror.append(e.getMessage());
        return null;
    }
}


      -----------my Connectionpool class-----------

public static Connection getConnection() {

    Connection con = null;
    javax.sql.DataSource ds;
    try {
        InitialContext ctx = new InitialContext();
        ds = (DataSource)
ctx.lookup("java:comp/env/jdbc/postgres");
        if (ds != null) {
            con = ds.getConnection();
        }
        else
            return null;
        }
    catch (Exception e) {
        return null;
    }
    return con;
}


/*****************************************************/


please help...

Thanks,
Dinakar

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you're looking for faster
http://search.yahoo.com

---------------------------(end of
broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to
majordomo@postgresql.org

pgsql-jdbc by date:

Previous
From: dinakar
Date:
Subject: any way in PG to check idle connections and close it automatically
Next
From: "Philip A. Chapman"
Date:
Subject: Exception while executing function with CallableStatement