我正在使用 JDBC 连接到 Oracle。我测试了 connection.setAutoCommit(false)
对比 connection.setAutoCommit(true)
结果正如预期的那样。
虽然默认情况下连接应该像 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();
最佳答案
来自 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.)
另一件事是 - 你省略了连接创建的细节,所以我只是猜测 - 如果你正在使用一些框架,或者从数据源或连接池获取连接,
autocommit
可转off
通过那些框架/池/数据源 - 解决方案是永远不要相信默认设置;-)
关于oracle - JDBC 连接默认自动提交行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11021304/