commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hope, Matthew" <Matthew.H...@capitalone.com>
Subject RE: DBCP: Closed connections
Date Fri, 25 Apr 2003 07:43:23 GMT
Statement.close() javadoc contract guarantees that it will cascade close()
to the ResultSet...

Closing them both seems a waste. Though it would protect you from a crappy
implementation not obeying the contract...

It is somewhat annoying that code of the form

Statement s = conn.getConnection();
try { 
	...
} catch (SQLException e) {
	...
}
Finally
{
	if (s != null) {
		try {
			s.close();
		} catch (SQLException e) {
			// do nothing since we aren't interested
		}
	}
}

Exists everywhere.

I tend to add 3 overloaded methods to some Database utility class
Already in use in the application. Would be good to have these in some class
so we wouldn't have to keep using them. (the boolean is just in case you
really want to know if close() got called succesfully)

public static boolean quickClose(Statement s)
{
	if (s != null) {
		try {
			s.close();
			return true;
		} catch (SQLException e) {
			// do nothing since we aren't interested
		}
	}
	return false;
}

Matt

-----Original Message-----
From: Chris Forbis [mailto:chris.forbis@veritas.com] 
Sent: 24 April 2003 16:49
To: 'Jakarta Commons Users List'
Subject: RE: DBCP: Closed connections


Yea that would be a major issue :)

A procedure that is good to have around :)


//Feel free to pass null in when they are not needed...
public static void closeDatabaseObjects( Connection conn, Statement stm,
ResultSet rs) {
  if (rs!=null) {
    try {
      rs.close();
    } catch (SQLException e) {
      System.err.println("Error on ResultSet close ("+e.toString()+")");
    }
  }
  
  if (stm!=null) {
    try {
      stm.close();
    } catch (SQLException e) {
      System.err.println("Error on Statement close ("+e.toString()+")");
    }
  }
  
  if (conn!=null) {
    try {
      conn.close();
    } catch (SQLException e) {
      System.err.println("Error on Connection close ("+e.toString()+")");
    }
  }
}


-- a use of this --

Conenction conn = null;
Statement stm = null;
ResultSet rs = null;
try{
  conn = myGetNewConnectionFromPoolCode();
  stm = conn.createStatement();
  rs = stm.executeQuery("some select query");
  while (rs.next()){
    //do stuff with the result
  }
} catch (SQLException e) {
  //Some error code
} finally {
  closeDatabaseObjects( conn, stm, rs );
  //or just closeDatabaseObjects( null, stm, rs); if you don't want to close
the database Connection
}


-----Original Message-----
From: Behrens Matt - Grand Rapids [mailto:Matt.Behrens@Kohler.Com] 
Sent: Thursday, April 24, 2003 7:25 AM
To: Jakarta Commons Users List
Subject: Re: DBCP: Closed connections

Chris Forbis wrote:

> Don't know if this is your issue but I had the same issue and it took me
> forever to track down...  It all came back to the following code...
> 
> //Stm = Statement
> //Rs = ResultSet
> 
> stm.close();
> rs.close();
> 
> The problem was simple, because of the order of these is wrong the
> connection pool got messed up bad.
> 
> Had to change to 
> rs.close();
> stm.close();
> 
> and all started working fine...

I don't close my ResultSets or Statements -- just the connections.  I 
suppose this could be a problem, actually.  I don't know.

I've had a few little issues like that.  Most memorable was when I had 
inexplicable hangs (this is an interactive server app where the users 
come in via telnet), and I beat my head against the wall for a little 
bit before I realized I was just running out of connections because I 
never explicitly returned a few to the pool...

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
 
**************************************************************************
The information transmitted herewith is sensitive information intended only
for use by the individual or entity to which it is addressed. If the reader
of this message is not the intended recipient, you are hereby notified that
any review, retransmission, dissemination, distribution, copying or other
use of, or taking of any action in reliance upon this information is
strictly prohibited. If you have received this communication in error,
please contact the sender and delete the material from your computer.

Mime
View raw message