Java 数据库连接应该一直打开还是只在需要时打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18962852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should a database connection stay open all the time or only be opened when needed?
提问by aman207
I have a bukkit plugin (minecraft) that requires a connection to the database.
我有一个需要连接到数据库的 bukkit 插件 (minecraft)。
Should a database connection stay open all the time, or be opened and closed when needed?
数据库连接应该一直保持打开状态,还是在需要时打开和关闭?
采纳答案by Luiggi Mendoza
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
在 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) { //... } }
爪哇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 closeit (i.e. calling Connection#close
), the physical database connection will just be in SLEEP mode and still be open.
但是由于手动打开数据库连接成本太高,所以强烈推荐使用数据库连接池。这将为您处理物理数据库连接,当您关闭它(即调用Connection#close
)时,物理数据库连接将仅处于睡眠模式并且仍处于打开状态。
Related Q/A:
相关问答:
Some tools to handle database connection pooling:
一些处理数据库连接池的工具:
回答by Itrulia
Everytime you connect you have a delay, now imagine what happens if you always connect and close
每次连接都会有延迟,现在想象一下如果你总是连接和关闭会发生什么
回答by Sai Avinash
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 块中,以确保您的连接将被关闭,即使应用程序中发生任何异常
finally
{
connection.close()
}
回答by qiGuar
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).
如果你只需要使用这个连接几次,你可能不会让它保持打开状态,但是当你想要访问数据库时会延迟。所以我建议你制作一个计时器,让连接保持打开一段时间(连接超时)。
回答by intiko81
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.
实际上,这完全取决于您如何编写应用程序!这是一门艺术,但遗憾的是,每个人都需要像 Microsoft 的教程这样的良好实践教程。
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!
如果您知道自己在编码什么,那么您就可以在应用程序的整个生命周期内保持连接打开。很简单,不是因为你早上要上班,而是每天都要为你打造一条特别的路线!您像每个人一样选择那条路线或 2 或 4 条路线!您判断交通情况,并根据需要构建 2、4 或 6 条路线。如果这4、6条路线有流量,你就等着吧!
Happy coding.
快乐编码。
回答by Sarkhan
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
您需要在每次查询执行后关闭您的连接。有时您需要同时执行多个查询,因为查询彼此挂起。例如“先插入任务,然后将其分配给员工”。此时执行您的查询同一个事务并提交它,如果发生一些错误,则回滚。默认情况下,JDBC 中禁用自动提交。例子
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
使用连接池。如果您正在开发 Web 应用程序,则使用 App Server 连接池。App 服务器将为您的每个应用程序使用相同的池,以便您可以从一个点控制连接数。强烈推荐 Apache Tomcat 连接池。例子
As an additional info: Connection, Statement and ResultSet.
作为附加信息:Connection、Statement 和 ResultSet。
1.If you close connection you don't need close statement or resultset.Both of them will be closed automatically
1.如果你关闭连接,你不需要关闭语句或结果集。它们都会自动关闭
2.If you close Statement it will close ResultSet also
2.如果你关闭 Statement 它也会关闭 ResultSet
3.if you use try-with-resources like this:
3.如果你像这样使用try-with-resources:
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
它会自动关闭连接。因为 try-with-resources 需要可自动关闭的对象,而 Connection 是可自动关闭的。您可以在此处查看有关 try-with-resources 的详细信息