dipanjaya prusty
dipanjaya prusty

Reputation: 21

Why does a JDBC connection default to having auto-commit enabled?

Why we need to set autocommit as false to perform local transaction why not it is false by default. What is the reason behind the autocommit as true predefined.

Upvotes: 2

Views: 1783

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 109045

The JDBC specification specifies that auto-commit true is the default. It does not provide a rationale for this behaviour, so the only reason is "because the specification says so".

Specifically, JDBC 4.3, section 10.1.1 Disabling Auto-commit Mode says:

The default is for auto-commit mode to be enabled when the Connection object is created.

As a possible reason, JDBC was inspired by ODBC, and ODBC also defines that connections default to auto-commit mode (see Setting the Commit Mode). And in Auto-Commit Mode, the ODBC documentation says:

In databases without transaction support, auto-commit mode is the only supported mode. In such databases, statements are committed when they are executed and there is no way to roll them back; they are therefore always in auto-commit mode.

If the underlying DBMS does not support auto-commit mode transactions, the driver can emulate them by manually committing each SQL statement as it is executed.

So, the reason for it being the default in ODBC seems so it can support a wide variety of database, including those that do not support transactions, with the same common behaviour. Emulating auto-commit mode is easy for databases that do not have an auto-commit mode, while the reverse - emulating transactions - is impossible for databases that do not support transactions.

Likely JDBC follows the same reasons, but this was not documented in publicly available documentation.

Note that connection pools sometimes provide an option to configure the default value of auto-commit (for example, HikariCP has a property autoCommit).

Upvotes: 4

Related Questions