Search code examples
javaoracle-databasejdbcautocommit

Autocommit mode for jdbc connection to Oracle from Java application


Do I have to call connection.setAutoCommit(false) in order to group a few DML statements in one transaction? I'm a bit confused because jdbc doc says that a new connection is created in auto-commit mode. At the same time, Oracle doesn't have auto-commit transactions, a DML statement begins a new transaction if it doesn't exist. So, in my understanding, connection.setAutoCommit(false) is redundant (assuming I don't consider switching to a different RDMS).

Could anyone clarify this?

Thanks.


Solution

  • setting autocommit to true means in fact that each statement sent to the database will be enclosed in a begin/commit. A transaction will be started implicitely, but it will also be committed implicitely. And the same will be true for the next statement.

    If you want several statements to be part of a single transaction, you'll need to set autocommit to false, execute your various statements, and finally call commit explicitely.