It depends. The JDBC specification allows drivers to decide exactly when to start a new transaction, so you cannot generalize on this. A driver might start a new transaction directly after the commit()
/ rollback
or setAutoCommit(false)
, although I'd be surprised if a lot of drivers actually do that.
Specifically JDBC 4.2 (section 10.1) says (emphasis mine):
When to start a new transaction is a decision made implicitly by either the JDBC driver or the underlying data source. Although some data sources implement an explicit “begin transaction” statement, there is no JDBC API to do so. Typically, a new transaction is started when the current SQL statement requires one and there is no transaction already in place.
It is more efficient to only start the transaction once it is really necessary, and starting the transaction too soon will hamper visibility of data (depending on the isolation level). I assume (but haven't verified) that is what most drivers do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.