简体   繁体   English

为什么要关闭数据库连接

[英]why to close Database connection

Why should we close a database connection? 为什么要关闭数据库连接? what can happen if a connection is left open? 如果连接保持打开状态会怎样? does the connection gets closed automatically when app exits? 应用退出时连接会自动关闭吗?

1) Open connections as late as possible 1)尽可能晚地打开连接

2) Close connections as soon as possible 2)尽快关闭连接

The connection itself is returned to the connection pool. 连接本身将返回到连接池。 Connections are a limited resource. 连接是有限的资源。 Any new connection you establish that has exactly the same connection string will be able to reuse the connection from the pool. 您建立的具有完全相同的连接字符串的任何新连接都将能够重用池中的连接。

It's not really necessary to close your database connection. 确实没有必要关闭数据库连接。 See this question on stackoverflow for a more detailed explanation. 有关更多详细说明,请参见有关stackoverflow的问题。

To release the resourcess like handler,we should use DBHelper.close(); 要释放类似处理程序的资源,我们应该使用DBHelper.close();。 to close Database, if you not close database,its may arise problem when you try to reconnect to Database. 关闭数据库,如果不关闭数据库,则在尝试重新连接到数据库时可能会出现问题。 So to overcome this problem , we must close Database. 因此,要克服此问题,我们必须关闭数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM