JDBC connection default autoCommit behavior

一世执手 提交于 2020-01-01 07:31:29

问题


I'm working with JDBC to connect to Oracle. I tested connection.setAutoCommit(false) vs connection.setAutoCommit(true) and the results were as expected.

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();

回答1:


From 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.)

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 ;-)



来源:https://stackoverflow.com/questions/11021304/jdbc-connection-default-autocommit-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!