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

  • Home
  • SEARCH
  • 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 126777

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:22:18+00:00 2026-05-11T05:22:18+00:00

I have create a getDBConnection method in my Java application. This returns a connection

  • 0

I have create a getDBConnection method in my Java application. This returns a connection object, and hence I haven’t closed this connection in this method itself.

Now, I am invoking this method from various methods in my application at regular intervals, and closing them inside a try – finally block. I thought this should free up the connection after use. However, I am seeing a large number of connections opened (about 50) in the MySQL Administrator’s Server Connections tab.

//Defining a method to retrieve a database connection // PropDemo is a properties class that retrieves Database related values from a file public Connection getDBConnection() {      //Instantiating the Properties object     PropDemo prop = new PropDemo();     Connection con = null;      // Retrieving values from the parameters.properties file     String JdbcDriver = prop.getMessage('JdbcDriver');     String JdbcUrlPrefix = prop.getMessage('JdbcUrlPrefix');     String DBIP = prop.getMessage('DBIP');     String DBName = prop.getMessage('DBName');     String DBUser = prop.getMessage('DBUser');     String DBPassword = prop.getMessage('DBPassword');      try {          // Loading and instantiating the JDBC MySQL connector driver class         Class.forName(JdbcDriver).newInstance();         con = DriverManager.getConnection(JdbcUrlPrefix + DBIP + '/' + DBName, DBUser, DBPassword);             if (con.isClosed())                     Logger.log('Connection cannot be established', 'vm');      } catch (Exception e) {         Logger.log('Exception: ' + e, 'vm');         Logger.log(Logger.stack2string(e), 'vm');     }     return con;  } 

I am also closing the associated ResultSet and Statement Objects. What could be missing here?

I am planning to replace all the Statements with PreparedStatements for efficiency and security reasons. Will that help significantly? What else can be done?

EDIT: This is just a core java application that is repeatedly quering for changes in some fields in a MySQL database through MySQL-JDBC connector. I am not using any framework like Spring or Hibernate.

  • 0 0 Answers
  • 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. 2026-05-11T05:22:18+00:00Added an answer on May 11, 2026 at 5:22 am

    Your code looks sane.

    That’s how you’re creating a new connection.

    Probably the error is where you close it.

    You should close it in a finally block.

    Some additional questions.

    1) Are you sure those 50 conections come from this program ? Maybe there are some others comming from your same office. To confirm this you would need to stop the program, and look again in your connection monitor.

    2) Does your application uses many connection simultaneously? Probably its a peak when you’re using 50 at the same time.

    If you can post the code where you close the connection. Chances are the problem is there.

    Additionally I would suggest you to use a connection pool. You can build one your self or you can see the results from this page:

    How many JDBC connections in Java?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've been having the same problem. sqlite3-ruby version 1.2.3 seems… May 11, 2026 at 9:01 pm
  • Editorial Team
    Editorial Team added an answer Well, first off, what you have there is not an… May 11, 2026 at 9:01 pm
  • Editorial Team
    Editorial Team added an answer Before Jon Skeet turns up here is a link to… May 11, 2026 at 9:01 pm

Related Questions

Is it possible to create a MySQL database from Java? I have only seen
I have a table in SQLite: CREATE TABLE EventType ( [EventTypeID] INTEGER PRIMARY KEY,
I have create a Membership provider and changed my web.config to <membership defaultProvider=MyMembershipProvider> <providers>
I want to create a trigger that will update the column LastActivityDate to the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.