Oded
Oded

Reputation: 664

glassfish JDBC connection pool

My application uses a mysql jdbc connection pool. When I ping the database via the glassfish admin site, I get a good response. When I use my application, everything works fine.

My problem happens when for any reason the mysql server crashes. After a crash, I restart the mysql server and when I ping the database via the glassfish admin site I get a good response, but when I use my application, I get an error.

I am looking for a way to tell glassfish to reestablish the connections. Any ideas?

Upvotes: 0

Views: 3368

Answers (3)

Sometimes GlassFish doesn't realize that the connections in the pool aren't good anymore. Hence, it may keep giving out the closed connection back to application for a while (I've seen this happening up to 20 minutes).

In that case, you can force GF to close the old connections & create new connections by flushing the connection pool.

Snippet from http://docs.oracle.com/cd/E19798-01/821-1752/giyeb/index.html.

You can flush a JDBC connection pool in one of these ways:

In the Administration Console, open the Resources component, open the JDBC component, select Connection Pools, and select the connection pool you want to flush. Then select the Flush button in the top left corner of the page. For details, click the Help button in the Administration Console.

Use the asadmin flush-connection-pool command. For details, see the Oracle GlassFish Server 3.0.1 Reference Manual.

Another option is to enable validation in the connection pool. This will force GF to check if the connection is working before giving it out to application. Once it realizes that the connection is closed, it'll replace that with a new connection.

Upvotes: 0

Eelke
Eelke

Reputation: 22063

Had a look around in the glassfish documentation and found the following:

com.sun.appserv.jdbc.DataSource ds=
   (com.sun.appserv.jdbc.DataSource)context.lookup("dataSource");
Connection con = ds.getConnection();
Statement stmt = null;
try{
   stmt = con.createStatement();
   stmt.executeUpdate("Update");
}
catch (BadConnectionException e){
   ds.markConnectionAsBad(con) //marking it as bad for removal
}
finally{
   stmt.close();    
   con.close(); //Connection will be destroyed during close.
}

Comes from this page Look for the heading: Marking Bad Connections, it's about two thirds down the page.

Upvotes: 1

jwenting
jwenting

Reputation: 5663

you're probably hanging on to your connections inside your application rather than returning them to the pool as soon as you're done with them.

Upvotes: 1

Related Questions