JDBC MySql Connection Pooling practices?

The exception indicates a typical case of application code which leaks database connections. You need to ensure that you acquire and close all of them ( Connection Statement and ResultSet ) in a try/finally block in the very same method block according the normal JDBC idiom.

The exception indicates a typical case of application code which leaks database connections. You need to ensure that you acquire and close all of them (Connection, Statement and ResultSet) in a try/finally block in the very same method block according the normal JDBC idiom: public void create(Entity entity) throws SQLException { Connection connection = null; PreparedStatement statement = null; try { connection = database.getConnection(); statement = connection. PrepareStatement(SQL_CREATE); statement.

SetSomeObject(1, entity.getSomeProperty()); // ... statement.executeUpdate(); } finally { if (statement! = null) try { statement.close(); } catch (SQLException logOrIgnore) {} if (connection! = null) try { connection.close(); } catch (SQLException logOrIgnore) {} } } Yes, you still need to close connections yourself, even when using connection pooling.It's a common mistake among starters that they think that it will then automatically handle the close.

This is not true. The connection pool namely returns a wrapped connection which does something like the following in the close(): public void close() throws SQLException { if (this. Connection is still eligible for reuse) { do not close this.

Connection, but just return it to pool for reuse; } else { actually invoke this.connection.close(); } } Not closing them would cause the connection not being released back to the pool for reuse and thus it will acquire a new one again and again until the DB runs out of connections which will cause your application to crash. Also see this basic JDBC/DAO tutorial for more hints how to get started with JDBC (in webapplications) the proper way. Update: you don't necessarly need to nest them that deep in their own finally blocks.

The following is also just fine: public static void closeQuietly(Connection connection, Statement statement, ResultSet resultSet) { if (resultSet! = null) try { resultSet.close(); } catch (SQLException logOrIgnore) {} if (statement! = null) try { statement.close(); } catch (SQLException logOrIgnore) {} if (connection!

= null) try { connection.close(); } catch (SQLException logOrIgnore) {} } which can be used as } finally { closeQuietly(connection, statement, resultSet); }.

Thank you for your thorough answer, it was really helpful! I edited my comment with the app-modifications. – Daniel Szalay Feb 22 '10 at 19:28 Thanks for the great info again!

– Daniel Szalay Feb 22 '10 at 19:44.

AFAIK, JDBC connection pooling is considered more or less a standard feature in these java application servers, and IMO, you should not want to build this yourself if you're just interested in creating an application. Here's a link that should get you started: weblogs.java.net/blog/2007/09/12/totd-9-... What you probably should be doing is find out how to let your application grab a connection from the pool using jndi.

He has already configured a connection pooled datasource in the appserver. He's only not closing resources properly as per the exception. – BalusC Feb 22 '10 at 19:15 oh crap, you're right.

Thanks BalusC. – Roland Bouman Feb 22 '10 at 20:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions