What happens if you don't commit a transaction to a database (say, SQL Server)?

前端 未结 9 2269
礼貌的吻别
礼貌的吻别 2020-11-27 03:08

Suppose I have a query:

begin tran
-- some other sql code

And then I forget to commit or roll back.

If another client tries to exec

9条回答
  •  遥遥无期
    2020-11-27 03:30

    The behaviour is not defined, so you must explicit set a commit or a rollback:

    http://docs.oracle.com/cd/B10500_01/java.920/a96654/basic.htm#1003303

    "If auto-commit mode is disabled and you close the connection without explicitly committing or rolling back your last changes, then an implicit COMMIT operation is executed."

    Hsqldb makes a rollback

    con.setAutoCommit(false);
    stmt.executeUpdate("insert into USER values ('" +  insertedUserId + "','Anton','Alaf')");
    con.close();
    

    result is

    2011-11-14 14:20:22,519 main INFO [SqlAutoCommitExample:55] [AutoCommit enabled = false] 2011-11-14 14:20:22,546 main INFO [SqlAutoCommitExample:65] [Found 0# users in database]

提交回复
热议问题