关于statement.close和connection.close 备忘(上)
2011-06-27 17:56
246 查看
很多人都会有这个疑问。在回收数据库连接资源的时候,大家一般都用connection.close() ,那么在这个过程中其持有的statements和results是否被清理或标记为无效了呢?我们又应该用哪种方式来释放数据库资源呢?
这个既和你使用数据库连接的方式有关,也和驱动有关,也就是和你的jdbc/odbc…… Driver有关。
我们来看看mysql-connector的源代码:
v5.0.7 Connection.java
public void close() throws SQLException { 这里去掉了3.1.12版本的同步
realClose(true, true, false, null);
}
realClose中做了很多事,包括
closeAllOpenStatements();
this.io.quit(); 注意这里关闭了io
this.io.forceClose(); 注意这里关闭了io
其中closeAllOpenStatements()中会调用connection中所有stmt的realClose(false, true);
最后还有:
this.openStatements = null;
this.io = null;
ProfileEventSink.removeInstance(this); // 3.1.12版本没有这句
this.isClosed = true;
v5.0.7 Statement.java
public void close() throws SQLException {
realClose(true, true);
}
在realClose中,
this.closeAllOpenResults(); 关闭了所有Results
对connection做了一些简单清理工作:
if (this.connection != null) {
if (this.maxRowsChanged) {
this.connection.unsetMaxRows(this);
}
if (!this.connection.getDontTrackOpenResources()) {
this.connection.unregisterStatement(this);
}
}
最后还有this.connection = null;
那么可以看出,最重要的地方在于 connection.close 关闭了connection的io 而statement没有关闭io 只是将connection设为null。
可以说connection.close () 关闭得更为彻底。
但是这样关闭不一定是必要的。 事实上我们在使用过程中不是一定要关得这么彻底,比如用数据库连接池的时候,在数据库操作完成后,完全可以用statement.close() 。这样该释放的都释放了,但是连接还在那里 ,下次用的时候也不用重新做io,这样想必效率上要高些。
另外在使用过程中必须具体应用具体分析,还必须仔细看看jdbc的代码。毕竟每个Driver或多或少有不同之处。
我在每分钟万级(不到10w)访问的情况下应用过statement.close(),没发现有任何内存泄露问题。
下一步需要做做比较,看看在效率和资源上的具体差别。
by zwonder 201106271756
这个既和你使用数据库连接的方式有关,也和驱动有关,也就是和你的jdbc/odbc…… Driver有关。
我们来看看mysql-connector的源代码:
v5.0.7 Connection.java
public void close() throws SQLException { 这里去掉了3.1.12版本的同步
realClose(true, true, false, null);
}
realClose中做了很多事,包括
closeAllOpenStatements();
this.io.quit(); 注意这里关闭了io
this.io.forceClose(); 注意这里关闭了io
其中closeAllOpenStatements()中会调用connection中所有stmt的realClose(false, true);
最后还有:
this.openStatements = null;
this.io = null;
ProfileEventSink.removeInstance(this); // 3.1.12版本没有这句
this.isClosed = true;
v5.0.7 Statement.java
public void close() throws SQLException {
realClose(true, true);
}
在realClose中,
this.closeAllOpenResults(); 关闭了所有Results
对connection做了一些简单清理工作:
if (this.connection != null) {
if (this.maxRowsChanged) {
this.connection.unsetMaxRows(this);
}
if (!this.connection.getDontTrackOpenResources()) {
this.connection.unregisterStatement(this);
}
}
最后还有this.connection = null;
那么可以看出,最重要的地方在于 connection.close 关闭了connection的io 而statement没有关闭io 只是将connection设为null。
可以说connection.close () 关闭得更为彻底。
但是这样关闭不一定是必要的。 事实上我们在使用过程中不是一定要关得这么彻底,比如用数据库连接池的时候,在数据库操作完成后,完全可以用statement.close() 。这样该释放的都释放了,但是连接还在那里 ,下次用的时候也不用重新做io,这样想必效率上要高些。
另外在使用过程中必须具体应用具体分析,还必须仔细看看jdbc的代码。毕竟每个Driver或多或少有不同之处。
我在每分钟万级(不到10w)访问的情况下应用过statement.close(),没发现有任何内存泄露问题。
下一步需要做做比较,看看在效率和资源上的具体差别。
by zwonder 201106271756
相关文章推荐
- 关于JBoss -“Closing a connection for you,please close them yourself”
- 关于CommandBehavior.CloseConnection
- Connection、Statement、PreparedStatement 、Resultset注意点备忘
- 大家来说一说关于jdbc的那些事情,关于Connection,Statement和ResultSet的关闭顺序
- 关于Connection Statement ResultSet的关系
- 关于MVC中利用Javascript跳转页面的问题的备忘
- 关于文件操作中的close()和flush()
- 备忘(关于Bash脚本语言)
- 关于draw9patch如何使用,介绍的很详细,图文并茂,收藏下,备忘!
- 关于Qt中信号和槽的Connection
- 关于开启Sql Server 2005 Express版本的远程连接功能(备忘)
- QT中关于close()系统调用 和qt close()冲突
- CommandBehavior.CloseConnection的使用
- 在对_ConnectionPtr指针Close时需要try-catch
- 关于hyper-v配置联外网问题(用作备忘)
- 关于URL.openConnection()得到的连接对象问题
- 关于HttpURLConnection.setFollowRedirects
- CommandBehavior.CloseConnection的作用
- PowerDesigner实体模型CDM中关于建立Entity之间关系的备忘
- 关于vc 链接时提示转换coff文件失败的【备忘-转载-来自百度知道】