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 6648167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:37:24+00:00 2026-05-26T00:37:24+00:00

Is it ok to open a connection in the init method and close it

  • 0

Is it ok to open a connection in the init method and close it in the destroy method? Also what’s the best way to open a connection to a mysql database. Currently I’m using this:

Class.forName("com.mysql.jdbc.Driver");

Connection connect = DriverManager.getConnection("jdbc:mysql://" + ipaddress + "?user=" + user + "&password=" + pass);

But I read somewhere that this is inefficient and I should use connection pool. How to do that?

  • 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-26T00:37:24+00:00Added an answer on May 26, 2026 at 12:37 am

    I would strongly suggest using a connection pool, either explicitly (such as c3p0) or one provided by your servlet container.

    Open your database connection when you need it, then close it as soon as you can – the connection pool will take care of the real network connection.

    Unless you do this, you’ll end up with one connection for your whole application – which means you can only process one query at a time, and all your code needs to synchronize around database queries. Surely you want multiple entirely-independent queries to be able to execute simultaneously? You can’t do this with a single connection.

    As for the best way of opening a connection having configured an appropriate connection pool – you may well still end up using DriverManager.getConnection(), but specifying the connection pool instead of mysql directly.

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

Sidebar

Related Questions

I open a connection like this: Using conn as New OdbcConnection(connectionString) conn.Open() //do stuff
What is the best way to manage a database connection in a Java servlet?
How do I check for an open connection in jdbc for oracle database? Note:
In c#.net when you open a connection to a database, in my case it's
Have a use case wherein need to maintain a connection open to a database
I'm using c#, and have an open tcpip connection receiving data. Is it possible
When I open a MySQL connection in PHP with just PHP's built-in MySQL functions,
I'm running into a problem using this method to address a javax.net.ssl.SSLPeerUnverifiedException: No peer
The flowing package is about a database connection class. It create a database, and
OK so what's the problem with this. I tried using MySQL JConnector for Java

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.