在 mysql jdbc 中,自动提交会影响所有连接吗?
当使用连接池时, 将设置连接的自动提交= false 只影响这个连接吗?
如果我关闭此连接而不设置 autocommit = true 并获取一个新连接,该连接的策略是否设置为 autocommit= true?
when using a connection pool,
will setting a connection's autocommit = false
affect this connection only?
if i close this connection without setting autocommit = true
and get a new connection will this connection's policy be set to autocommit= true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案是:“取决于所使用的连接池”。
但是,如果我是连接池,我会根据初始配置恢复自动提交状态。我认为其他连接池也会这样做。
The answer would to the point be: "depends on the connection pool used".
However, if I was a connection pool, I would have restored the autocommit state as per the initial configuration. I think that other connection pools would do the same.
测试场景:
结果:当此特定连接被重用时,它仍然具有 autocommit=false (其他新连接的默认值是 autocommit=true)。因此,一旦我在数据库端终止这个连接并再次调用我的代码,池就会为我提供一个默认 autocommit=true 的新连接。
结论:它仅影响此连接,但请确保在关闭之前将其设置回 autocommit=true! (这是基于真实测试,而不是假设)
注意:这里也注意到了此行为:http://www.coderanch.com/t/583969/JDBC/databases/Tomcat-connection-pool-auto-commit
我使用JDBC 与 Tomcat7、Java 1.7、MySQL5.6、Connector/J 5.1。
Test scenario:
Outcome: when this particular connection is reused, it still has autocommit=false (other new connections have the default autocommit=true). So, once I kill this connection on the database side and call my code again, only then the pool gives me a fresh connection with the default autocommit=true.
Conclusion: it affects only this connection, but make sure you set it back to autocommit=true before closing! (This is based on real testing, not an assumption)
Note: this beahvior is noted here too: http://www.coderanch.com/t/583969/JDBC/databases/Tomcat-connection-pool-auto-commit
I use JDBC with Tomcat7, Java 1.7, MySQL5.6, Connector/J 5.1.