12

After closing a database connection in java, can I reopen it? Or do I need to do DriverManager.getConnection() again?

3
  • 4
    What's so hard trying it out? Commented Apr 6, 2012 at 13:21
  • Also do you see any open method on Connection? Commented Apr 6, 2012 at 13:28
  • 1
    I don't see any open method in connection. So how do I try it? Calling getConnection will work, tried it, but isit the "correct" way? Commented Apr 6, 2012 at 14:25

5 Answers 5

9

If you had called connection.close();, the connection (assuming java.sql.Connection type) becomes useless. This operation releases this Connection object's database and JDBC resources.

So Yes you need to get a fresh connection when before you can proceed by

connection = DriverManager.getConnection() 
Sign up to request clarification or add additional context in comments.

1 Comment

I just started doing JDBC to SQL Server connections after doing everything in C# and boy was this a big mystery for half my day yesterday.
5

I'm not 100% sure that you need to call DriverManager.getConnection() but what's the harm? You already closed the connection, just grab a new one when you need it. The garbage collector worries about that closed connection after you discard it.

Comments

1

yes, you cant do anything after closing connection. you have to call getConnection

Comments

0

As Javadoc mentions:

getConnection: Attempts to establish a connection to the given database URL. 

Therefore yes, calling getConnection() again looks like the only way to establish a new connection with database once you closed previous connection.

Comments

0

Well Ideally I made slightly different approach and gave a different solution.

Instead of opening and closing the database, which literally happening on every request of the client, I made a singleton instance of a class and made single open connection which will be reused as long as the process of the server is alive.

In short:

I have getConnection() inside Database class

 public Connection getConnection() throws Exception { try { String connectionURL = "jdbc:mysql://localhost:3306/someDatabase"; Class.forName("com.mysql.jdbc.Driver").newInstance(); if (connection == null) { connection = DriverManager.getConnection(connectionURL, "someUser", LOCAL_MYSQL_PASSWORD); } return connection; } catch (Exception e) { } } 

The Database class is singleton, hence reusing the same class and reusing the same connection.

I've tested this by show processList, and this one was giving around 100 connections If I won't close the connection, and even If I close the connection, actual process will not likely to be killed, but put into sleep instead so it will be used whenever the same client will ask for same request, but if you have like 15 requests, then each of them will have separate processes, so closing and opening connection for me wasn't best solution.

By this I have 1 single process which is responsible layer for queries.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.