How to start a transaction in JDBC?

后端 未结 8 1122
梦谈多话
梦谈多话 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:48

    JDBC implicitly demarcates each query/update you perform on the connection with a transaction. You can customize this behavior by calling setAutoCommit(false) to turn off the auto-commit mode and call the commit()/rollback() to indicate the end of a transaction. Pesudo code

    try
    {
      con.setAutoCommit(false);
    
       //1 or more queries or updates
    
       con.commit();
    }
    catch(Exception e)
    {
       con.rollback();
    }
    finally
    {
       con.close();
    }
    

    Now, there is a type in the method you have shown. It should be setTransactionIsolation(int level) and is not the api for transaction demarcation. It manages how/when the changes made by one operation become visible to other concurrent operations, the "I" in ACID (http://en.wikipedia.org/wiki/Isolation_(database_systems))

提交回复
热议问题