[英]Can I change AutoCommit property of a JDBC connection multiple times
我正在處理僅使用“ autoCommit = true”創建連接的連接池。
但是,對於我的特定用例,我需要“ autoCommit = false”,以便可以在JDBC語句上設置“ fetch size”屬性。
我的初始測試測試表明,可以在JDBC Connection實例上設置AutoCommit屬性,然后在將連接返回到池之前再次對其進行重置。
Connection conn = pool.getConnection();
try {
conn.setAutoCommit(false);
// execute queries for my use case using above connection.
} finally {
conn.setAutoCommit(true);
// do other cleanup like statement and result set close
}
pool.returnConnection(conn);
有人知道這是否是正確的用例嗎?
我正在使用Postgres,但稍后可能會遷移到Oracle。
最終更新 :是的,您可以多次更改autoCommit,也可以在發現的語句中使用commit / rollback命令來解決它。 我的建議是堅持將autoCommit設置為false,並始終在需要它們的地方使用事務。
我也使用Postgres和Oracle,並且始終使用autocommit = false,因為我無法使用autocommit = true管理事務
您可以在測試時更改自動提交,但我鼓勵您顯式管理事務,即使它只是一條語句。
如果可以使用諸如Spring(或Guice)之類的框架,則可以通過AOP進行事務管理,而無需擔心提交和回退指令。
在Oracle中,提交時間不取決於所提交的數據量,而且較高的提交頻率(就功能要求而言)也可能損害性能。
更新 :從您的評論中可以看出,Postgres在自動提交中尊重交易邊界。 我無法重現此行為,這里是一個簡單的測試用例:
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestPostgresAutocommit {
public static void main(String[] args) throws Exception {
Connection connection= DriverManager.getConnection("jdbc:postgresql://pgdev/dbxxx","xxx","xxx");
connection.setAutoCommit(true);
Connection connection2= DriverManager.getConnection("jdbc:postgresql://pgdev/dbxxx","xxx","xxx");
connection2.setAutoCommit(true);
Statement statement=connection.createStatement();
for (int i=0; i<10; i++) {
statement.execute("insert into test_gc(col1,col2) values ("+ i + "," + "'" + i + "')");
}
statement.close();
countElements(connection2);
statement=connection.createStatement();
statement.execute("delete from test_gc");
statement.close();
statement=connection.createStatement();
statement.execute("begin");
statement.close();
statement=connection.createStatement();
for (int i=0; i<10; i++) {
statement.execute("insert into test_gc(col1,col2) values ("+ i + "," + "'" + i + "')");
}
connection.rollback();
countElements(connection2);
}
private static void countElements(Connection connection2) throws Exception {
Statement statement2=connection2.createStatement();
ResultSet rs=statement2.executeQuery("select count(*) from test_gc");
rs.next();
System.out.println("row num in table=" + rs.getInt(1));
rs.close();
statement2.close();
}
}
程序無法回滾並出現異常:
org.postgresql.util.PSQLException:啟用autoCommit時無法回滾。
因此,當autoCommit為true時,不可能管理事務。 你找到不同的東西了嗎?
更新II :即使我認為這段代碼反映了您評論中的數據,我仍然得到了例外:
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestPostgresAutocommit {
public static void main(String[] args) throws Exception {
//System.out.println("start");
Connection connection= DriverManager.getConnection("jdbc:postgresql://pgdev/dbxxx","xxx","xxx");
connection.setAutoCommit(true);
Connection connection2= DriverManager.getConnection("jdbc:postgresql://pgdev/dbdxxx","xxx","xxx");
connection2.setAutoCommit(true);
Statement statement=connection.createStatement();
for (int i=0; i<10; i++) {
statement.execute("insert into test_gc(col1,col2) values ("+ i + "," + "'" + i + "')");
}
statement.close();
countElements(connection2);
statement=connection.createStatement();
statement.execute("delete from test_gc");
statement.close();
statement=connection.createStatement();
statement.execute("begin");
for (int i=0; i<10; i++) {
statement.execute("insert into test_gc(col1,col2) values ("+ i + "," + "'" + i + "')");
}
connection.rollback();
countElements(connection2);
}
private static void countElements(Connection connection2) throws Exception {
Statement statement2=connection2.createStatement();
ResultSet rs=statement2.executeQuery("select count(*) from test_gc");
rs.next();
System.out.println("row num in table=" + rs.getInt(1));
rs.close();
statement2.close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.