aman207
aman207

Reputation: 1150

Should a database connection stay open all the time or only be opened when needed?

I have a bukkit plugin (minecraft) that requires a connection to the database.

Should a database connection stay open all the time, or be opened and closed when needed?

Upvotes: 33

Views: 34577

Answers (5)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

The database connection must be opened only when its needed and closed after doing all the necessary job with it. Code sample:

  • Prior to Java 7:

      Connection con = null;
      try {
          con = ... //retrieve the database connection
          //do your work...
      } catch (SQLException e) {
          //handle the exception
      } finally {
          try {
              if (con != null) {
                  con.close();
              }
          } catch (SQLException shouldNotHandleMe) {
              //...
          }
      }
    
  • Java 7:

      try (Connection con = ...) {
      } catch (SQLException e) {
      }
      //no need to call Connection#close since now Connection interface extends Autocloseable
    

But since manually opening a database connection is too expensive, it is highly recommended to use a database connection pool, represented in Java with DataSource interface. This will handle the physical database connections for you and when you close it (i.e. calling Connection#close), the physical database connection will just be in SLEEP mode and still be open.

Related Q/A:

Some tools to handle database connection pooling:

Upvotes: 30

Sarkhan
Sarkhan

Reputation: 1291

You need to close your connections after each query executions.Sometimes you need to execute multiple queries at the same time because the queries are hanging from each other.Such as "first insert task then assign it to the employees".At this time execute your queries on the same transaction and commit it, if some errors occur then rollback.By default autocommit is disabled in JDBC. Example

Use connection pooling.If you are developing a webapplication then use App Server connection pooling.App server will use the same pooling for each of your applications so you can control the connection count from the one point.Highly recommend the Apache Tomcat Connection pooling.Example

As an additional info: Connection, Statement and ResultSet.

1.If you close connection you don't need close statement or resultset.Both of them will be closed automatically

2.If you close Statement it will close ResultSet also

3.if you use try-with-resources like this:

try (Connection con = ...) {
} catch (SQLException e) {
}

it will close the connection automatically.Because try-with-resources require autoclosable objects and Connection is autocloseable.You can see the details about try-with-resources here

Upvotes: 3

intiko81
intiko81

Reputation: 21

Actually, it's all matter on how you write your application! It's an art, but sadly everyone takes a tutorial for a good practice like Microsoft's tutorials.

If you know what you are coding, then you keep your connection open for the lifetime of the application. It's simple, not because you have to go at work in the morning that everyday we have to build a special route just for you! You take that single route or 2 or 4 like everyone does! You judge for the traffics and you build 2, 4 or 6 routes as needed. If there is traffic with these 4 or 6 routes, you wait!

Happy coding.

Upvotes: 2

qiGuar
qiGuar

Reputation: 1804

Depends on what are your needs.

Creating a connection takes some time, so if you need to access database frequently it's better to keep the connection open. Also it's better to create a pool, so that many users can access database simultaneously(if it's needed).

If you need to use this connection only few times you may not keep it open, but you will have delay when you would like to access database. So i suggest you to make a timer that will keep connection open for some time(connection timeout).

Upvotes: 6

Sai Avinash
Sai Avinash

Reputation: 4753

The Connection should be opened only when required. If it is open before the actual need, it reduces one active connection from the connection pool..so it ultimately effects the users of the application.

So,it is always a better practice to open connection only when required and closing it after completion of process.

Always try puttting you connection close logic inside the finally block that will ensure that your connection will be closed,even if any exception occurs in the application

finally
{
connection.close()
}

Upvotes: 0

Related Questions