oracle – JDBC连接默认autoCommit行为

前端之家收集整理的这篇文章主要介绍了oracle – JDBC连接默认autoCommit行为前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用JDBC连接到Oracle。我测试了connection.setAutoCommit(false)vs connection.setAutoCommit(true)并且结果如预期的那样。

虽然默认情况下,连接应该像AutoCommit(true)一样工作[如果我错了就纠正我],但是没有记录被插入,直到connect.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.)

另一件事就是 – 你忽略了连接创建的细节,所以我只是猜测 – 如果你正在使用一些框架,或从数据源或连接池获取一个连接,那么这些框架/池/数据源可能会关闭自动提交 – 解决方案是永远不信任默认设置;-)

猜你在找的Oracle相关文章