gpt4 book ai didi

oracle - JDBC 连接默认自动提交行为

转载 作者:行者123 更新时间:2023-12-03 11:39:41 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com