oracle JDBC 连接默认自动提交行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11021304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC connection default autoCommit behavior
提问by stackex
I'm working with JDBC to connect to Oracle. I tested connection.setAutoCommit(false)
vs connection.setAutoCommit(true)
and the results were as expected.
我正在使用 JDBC 连接到 Oracle。我测试了connection.setAutoCommit(false)
vs connection.setAutoCommit(true)
,结果如预期。
While by default connection is supposed to work as if autoCommit(true)
[correct me if I'm wrong], but none of the records are being inserted till connection.commit()
was called. Any advice regarding default behaviour?
虽然默认情况下连接应该像autoCommit(true)
[纠正我如果我错了]一样工作,但connection.commit()
在调用之前没有插入任何记录。关于默认行为的任何建议?
String insert = "INSERT INTO MONITOR (number, name,value) VALUES (?,?,?)";
conn = connection; //connection details avoided
preparedStmtInsert = conn.prepareStatement(insert);
preparedStmtInsert.execute();
conn.commit();
回答by npe
From Oracle JDBC documentation:
When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. (To be more precise, the default is for a SQL statement to be committed when it is completed, not when it is executed. A statement is completed when all of its result sets and update counts have been retrieved. In almost all cases, however, a statement is completed, and therefore committed, right after it is executed.)
创建连接时,它处于自动提交模式。这意味着每个单独的 SQL 语句都被视为一个事务,并在执行后立即自动提交。(更准确地说,默认情况下 SQL 语句在完成时提交,而不是在执行时提交。当检索到所有结果集和更新计数时,语句就完成了。然而,在几乎所有情况下, ,语句在执行后立即完成,因此被提交。)
The other thing is - you ommitted connection creation details, so I'm just guessing - if you are using some frameworks, or acquiring a connection from a datasource or connection pool, the autocommit
may be turned off
by those frameworks/pools/datasources - the solution is to never trust in default settings ;-)
另一件事是-您省略了连接创建的详细信息,所以我只是猜测-如果您正在使用某些框架,或者从数据源或连接池获取连接,则这些框架/池/数据源autocommit
可能会转向off
-解决方案永远不要相信默认设置;-)