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

橙三吉。 提交于 2019-12-17 06:13:20

问题


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?


回答1:


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. 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:

  • Java Connection Pooling

Some tools to handle database connection pooling:

  • BoneCP
  • c3po
  • Apache Commons DBCP
  • HikariCP



回答2:


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).




回答3:


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




回答4:


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.




回答5:


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()
}



回答6:


Everytime you connect you have a delay, now imagine what happens if you always connect and close



来源:https://stackoverflow.com/questions/18962852/should-a-database-connection-stay-open-all-the-time-or-only-be-opened-when-neede

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!