JDBC连接默认的autoCommit行为 [英] JDBC connection default autoCommit behavior
问题描述
我正在使用JDBC来连接到Oracle.我测试了connection.setAutoCommit(false)
vs connection.setAutoCommit(true)
,结果与预期的一样.
I'm working with JDBC to connect to Oracle. I tested connection.setAutoCommit(false)
vs connection.setAutoCommit(true)
and the results were as expected.
默认情况下,连接应该像autoCommit(true)
一样工作(如果我错了,请纠正我),但是在调用connection.commit()
之前,不会插入任何记录.关于默认行为有什么建议吗?
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?
String insert = "INSERT INTO MONITOR (number, name,value) VALUES (?,?,?)";
conn = connection; //connection details avoided
preparedStmtInsert = conn.prepareStatement(insert);
preparedStmtInsert.execute();
conn.commit();
推荐答案
来自 Oracle JDBC文档:
创建连接后,它处于自动提交模式.这表示 每个单独的SQL语句都被视为事务,并且 执行后立即自动提交. (更多 精确地,默认情况是默认情况下要提交一条SQL语句 完成,而不是在执行时.当所有 已检索到其结果集和更新计数.在几乎 但是,在所有情况下,声明均已完成,因此已提交, 立即执行.)
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.)
另一件事是-您忽略了连接创建的详细信息,所以我只是在猜测-如果您使用的是某些框架,或者从数据源或连接池获取连接,则autocommit
可能会变成off
这些框架/池/数据源-解决方案是永远不要信任默认设置;-)
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 ;-)
这篇关于JDBC连接默认的autoCommit行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!