Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:16:20+00:00 2026-05-16T10:16:20+00:00

I am creating a multi-threaded application. However, I have experienced lots of unexpected behavior

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:16:21+00:00Added an answer on May 16, 2026 at 10:16 am

    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) {}
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard that in Haskell, creating a multi-threaded application is as easy as
I am creating some multi-threaded code, and I have created a JobDispatcher class that
I am creating a multi-threaded application in which each thread will appear as a
Here's the scenario: I have a multi threaded java web application which is running
I have a multi-threaded Delphi program creating multiple instance of some classes, and I
I'm creating a multi-threaded application in C using Linux. I'm unsure whether I should
I'm currently developing a heavily multi-threaded application, dealing with lots of small data batch
I am creating a multi lingual application using ZF2.. and cannot determine how to
I am thinking of creating a multi-platform portable C++ server-client application. Is it even
I'm creating additional module to already multi-module maven project. And for this one I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.