It is necessary to disconnect from the database after the job is done in Java? If it is not disconnected, will it lead to memory leaks?
在Java中完成作业后,必须断开与数据库的连接?如果未断开连接,是否会导致内存泄漏?
5 个解决方案
#1
4
You should provide more details like which framework you are using or something.
您应该提供更多详细信息,例如您正在使用的框架或其他内容。
Anyway, are you using JDBC? If so you should close the following objects by using their respective close()
methods: Statement, ResultSet and Connection.
无论如何,你使用JDBC吗?如果是这样,您应该使用各自的close()方法关闭以下对象:Statement,ResultSet和Connection。
#2
6
You must always close all your Connections, Statements and ResultSets.
您必须始终关闭所有Connections,Statements和ResultSet。
If not, is more probable you can't obtain new connections from the pool than a memory leak.
如果没有,则更有可能无法从池中获取新连接而不是内存泄漏。
#3
3
Assuming you are using JDBC, the answer is yes. If you don't close the connection, then the JDBC driver might try to close it in a finallizer, but that could hold the connection open for a very long time, causing resource issues (the amount of database connections allowed to be open at one time is finite). Typically JDBC programming is done with a database pool, and not closing the connection will mean that the pool will run out of available connections very quickly.
假设您使用的是JDBC,答案是肯定的。如果不关闭连接,则JDBC驱动程序可能会尝试在finallizer中关闭它,但这可能会使连接保持打开很长时间,从而导致资源问题(允许在一个数据库连接中打开数据库连接的数量)时间有限)。通常,JDBC编程是使用数据库池完成的,而不关闭连接意味着池将很快耗尽可用连接。
Some application servers (e.g. JBoss) will detect when a connection wasn't closed and close it for you if it is managing the transactions, but you should not rely on that.
某些应用程序服务器(例如JBoss)将检测连接何时未关闭,如果它正在管理事务,则会为您关闭它,但您不应该依赖它。
Of course some JDBC drivers are not pure java drivers, at which point memory leaks become a very real possibility.
当然,一些JDBC驱动程序不是纯粹的Java驱动程序,此时内存泄漏成为非常现实的可能性。
#4
1
I don't have a source, but I believe (if I remember right, it's been a while since I've touched JDBC) that it depends on the JDBC driver implementation. You should always close your connections and clean up after yourself as not all JDBC drivers do it for you (although some might).
我没有源代码,但我相信(如果我没记错的话,自从我触及JDBC以来已经有一段时间了),这取决于JDBC驱动程序的实现。您应该始终关闭连接并自行清理,因为并非所有JDBC驱动程序都会为您执行此操作(尽管有些人可能会这样做)。
This goes back to a rule that I like to follow - If I create or open something, I'm responsible for deleting or closing it.
这可以追溯到我想遵循的规则 - 如果我创建或打开某些内容,我负责删除或关闭它。
#5
-2
yes and yes
是的,是的
#1
4
You should provide more details like which framework you are using or something.
您应该提供更多详细信息,例如您正在使用的框架或其他内容。
Anyway, are you using JDBC? If so you should close the following objects by using their respective close()
methods: Statement, ResultSet and Connection.
无论如何,你使用JDBC吗?如果是这样,您应该使用各自的close()方法关闭以下对象:Statement,ResultSet和Connection。
#2
6
You must always close all your Connections, Statements and ResultSets.
您必须始终关闭所有Connections,Statements和ResultSet。
If not, is more probable you can't obtain new connections from the pool than a memory leak.
如果没有,则更有可能无法从池中获取新连接而不是内存泄漏。
#3
3
Assuming you are using JDBC, the answer is yes. If you don't close the connection, then the JDBC driver might try to close it in a finallizer, but that could hold the connection open for a very long time, causing resource issues (the amount of database connections allowed to be open at one time is finite). Typically JDBC programming is done with a database pool, and not closing the connection will mean that the pool will run out of available connections very quickly.
假设您使用的是JDBC,答案是肯定的。如果不关闭连接,则JDBC驱动程序可能会尝试在finallizer中关闭它,但这可能会使连接保持打开很长时间,从而导致资源问题(允许在一个数据库连接中打开数据库连接的数量)时间有限)。通常,JDBC编程是使用数据库池完成的,而不关闭连接意味着池将很快耗尽可用连接。
Some application servers (e.g. JBoss) will detect when a connection wasn't closed and close it for you if it is managing the transactions, but you should not rely on that.
某些应用程序服务器(例如JBoss)将检测连接何时未关闭,如果它正在管理事务,则会为您关闭它,但您不应该依赖它。
Of course some JDBC drivers are not pure java drivers, at which point memory leaks become a very real possibility.
当然,一些JDBC驱动程序不是纯粹的Java驱动程序,此时内存泄漏成为非常现实的可能性。
#4
1
I don't have a source, but I believe (if I remember right, it's been a while since I've touched JDBC) that it depends on the JDBC driver implementation. You should always close your connections and clean up after yourself as not all JDBC drivers do it for you (although some might).
我没有源代码,但我相信(如果我没记错的话,自从我触及JDBC以来已经有一段时间了),这取决于JDBC驱动程序的实现。您应该始终关闭连接并自行清理,因为并非所有JDBC驱动程序都会为您执行此操作(尽管有些人可能会这样做)。
This goes back to a rule that I like to follow - If I create or open something, I'm responsible for deleting or closing it.
这可以追溯到我想遵循的规则 - 如果我创建或打开某些内容,我负责删除或关闭它。
#5
-2
yes and yes
是的,是的