tags:

views:

216

answers:

2

I have a simple web application written in java which has servlets accessing the Mysql Database to generate reports. The report generation happens very frequently.

I am using the apache commons DBCP to get connections to the DB. I also close the connection explicity in the finally block always. But i do not explicitly close the Statements and ResultSets i create.

I'm forced to restart the tomcat instance everytime i get the Exception which says "java.sql.SQLException: Too many connections".

How do i overcome this.... do i need to increase the maxconnections in Mysql ?.

Any help is appreciated.

A: 

Use connection pooling and always close (return to pool) the connection once work is done.

http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html

Yada
The OP is already returning the connection to the pool by calling close() on the Connection.
Adamski
+1  A: 
  • Yes you should close your Statements / ResultSets - These will typically reference the raw JDBC Connection rather than the PooledConnection proxy Connection returned by the DBCP PooledDataSource.
  • Consider using C3P0 rather than DBCP.
  • Consider using Spring to manage your JDBC operations and you will never have to worry about this kind of thing.
Adamski
Yes. I think the real real problem is the Statements/ResultSets, because of the first point. When you close the delegated connection (the one who in fact returns to the pool) it doesn't close it's underlying-actual connection (it wants to keep it). So the RS and ST don't close never and ersources are exhausted.I don't know if C3P0 has something to automatically overcome this, but if you take care of this it must work very well. With DBCP.
helios
Sorry - My 2nd point about C3P0 was unrelated; you still need to close the Connection and all Statements / ResultSets correctly. It just seems to be preferred over DBCP (if you search for DBCP C3P0 you'll see what I mean).
Adamski