问题
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