How to start a transaction in JDBC?

后端 未结 8 1116
梦谈多话
梦谈多话 2020-12-02 08:48

Connection.setTransactionIsolation(int) warns:

Note: If this method is called during a transaction, the result is implementation-defined.

8条回答
  •  温柔的废话
    2020-12-02 09:38

    Answering my own question:

    • JDBC connections start out with auto-commit mode enabled, where each SQL statement is implicitly demarcated with a transaction.
    • Users who wish to execute multiple statements per transaction must turn auto-commit off.
    • Changing the auto-commit mode triggers a commit of the current transaction (if one is active).
    • Connection.setTransactionIsolation() may be invoked anytime if auto-commit is enabled.
    • If auto-commit is disabled, Connection.setTransactionIsolation() may only be invoked before or after a transaction. Invoking it in the middle of a transaction leads to undefined behavior.

    Sources:

    • Javadoc
    • JDBC Tutorial

提交回复
热议问题