views:

21

answers:

1

Hi,

I am creating a multi-threaded application. However, I have experienced lots of unexpected behavior from my application when I have one connection object serving all threads.

I am in a dilemma. Should I let every thread create, use and dispose its own connection object or should I use a connection pool?

I have tried connection pooling which makes the application painfully shower. However, my intuition is that if I let every thread create its own connection object, I might get a "too many connection" error.

Please let me know if there is any way to assist in this.

Regards.

+1  A: 

Regardless of the threading issue, you should definitely go for a connection pool. It will greatly increase connecting performance. Then to the threading issue, this is indeed a major problem. The normal JDBC idiom is to acquire and close all resources in the shortest possible scope. I.e. all should happen in the very same method block. The problem symptoms which you're describing confirms that you aren't closing those resources properly.

Closing should always happen regardless of whether the connection is coming from a pool or not. Closing a non-pooled connection will prevent it from being timed-out by the database when it's been hold open for a too long time. Closing a pooled connection will actually release it back to the pool and make it available for the next lease.

Here's how the normal JDBC idiom look like for the case of a INSERT.

public void create(Entity entity) throws SQLException {
    // Declare.
    Connection connection = null;
    PreparedStatement statement = null;

    try { 
        // Acquire.
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL_CREATE);

        // Use.
        statement.setSomeObject(1, entity.getSomeProperty());
        // ...
        statement.executeUpdate();
    } finally {
        // Close.
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
}
BalusC
Hi BalusC, Thanks for your response. My threads are now dealing with their own connection object, closing their defined ResultSet and Statement objects. This solved the "too many connection" problem.
mwangi
You're welcome. Don't forget to mark the answer accepted. See also http://stackoverflow.com/faq :)
BalusC
I also solved the threading issue by introducing a locking mechanism. I introduced custom start,stop,pause and resume methods in all my threads. Three threads can only be active at given time. - main, "producer" and "worker". I have many workers but one "producer"Here is the pseudo code for the algorithm1. Start Main Thread2. Start the Producer Thread3. Start while-true loop4. While producer has not paused, main thread sleep for x time5. Start a worker who will pick the data that concerns him/her :)6. While worker is working, main thread sleep for x time 7. end while-true loop
mwangi