As stated in this Stackoverflow answer and in this website, is it true that each commit()
call implicitly start another transaction block given setAutoCommit(false)
?

- 1
- 1

- 3,491
- 6
- 25
- 42
-
1Yes, it's true. Now you can ask the same question again with three links instead of two :). – JB Nizet Jun 20 '16 at 06:08
-
2It depends a bit on the JDBC driver and DBMS being used, but in general this is true, yes. – Jun 20 '16 at 06:21
-
1@a_horse_with_no_name See my answer, I would not assume that the next transaction is started immediately. – Mark Rotteveel Jun 20 '16 at 09:21
1 Answers
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.

- 100,966
- 191
- 140
- 197