java – Oracle数据库SELECT … FOR UPDATE with autocommit on

我正在对Oracle数据库(11g)运行此查询.该连接具有autocommit的默认值,即’true’.

Connection con = driver.connect(url, properties);
String query = "SELECT EMPID, NAME FROM EMPLOYEE FOR UPDATE";

Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = statement.executeQuery(query);

SELECT… FOR UPDATE声明它“锁定所选行,以便其他用户无法锁定或更新行,直到您结束事务为止.”使用autocommit应该在执行查询后立即执行.但锁定存在直到连接关闭.

(我通过在sqlplus上运行查询选择* FROM DBA_DML_LOCKS WHERE NAME =’EMPLOYEE’来检查这一点.)

这是一个错误还是有人请解释原因?
先感谢您.

解决方法:

documentation所述:

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

另一个documentation

Enabling auto-commit may be more convenient, but gives you less control. For example, you have no option to roll back changes. In addition, some SQLJ or JDBC features are incompatible with auto-commit mode. For example, you must disable the auto-commit flag for update batching or SELECT FOR UPDATE syntax to work properly.

因此,对于此查询类型,您应该使用手动提交

上一篇:数据分页处理系列之三:Neo4j图数据分页处理


下一篇:Linux - CentOS7静默安装oracle11g