Call commit on autoCommit=false connection for SELECT statements JDBC?

家住魔仙堡 提交于 2019-12-07 21:39:58

问题


I do have a webapp written in Java on Tomcat, all connections should be autoCommit=false by default. Now, if I do run SELECT statement only in a transaction. Do I still need to call commit() or is it sufficient just to close the connection?

For what it's worth: I am on Oracle 11.2.

There is a similar question but does not actually give an answer for this case.


回答1:


It is sufficient to close the connection, no need to call commit or rollback.

But according to connection.close(), it is recommended to call either commit or rollback.




回答2:


Select statements do not disturb the underlying model or the data contained within the model. It is safe to close the connection without calling any commands related to transactions (like commit).

Actually strike that. I had not considered adjacent selects made to a model in my first answer. Say you execute select id from users where age > 20 and follow it up with select id from users where age = 20, any updates made between these queries would affect the ACID nature of the selects and return duplicate results within the 2 queries. To guarantee consistent results you would need to wrap both selects in the same transaction with a commit().

So yes, It makes sense to commit your selects.



来源:https://stackoverflow.com/questions/15656034/call-commit-on-autocommit-false-connection-for-select-statements-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!