【发布时间】:2016-06-19 22:20:35
【问题描述】:
我正在尝试从 java 向 PosgresSQL 运行查询,但我收到来自 stmt.execute(sql) 的错误
我想执行一个新查询来帮助我打印出失败的特定行,但是当我到达 catch (Exception e) 时,事务被中止。
我无法创建新事务,因为我正在使用临时表。如何防止交易中止?
org.postgresql.util.PSQLException: 错误: 当前事务被中止,命令被忽略直到事务块结束
try (Statement stmt = data.db.getConnection().createStatement()) {
// data.db.getConnection().setSavepoint("sp01");
// insert to fact table
TableSchema factTableSchema = factInfo.getTableSchema();
// build SQL
String sql = "Select * From....";
try {
stmt.execute(sql); // this row is failing
}
catch (Exception e) {
try {
// now i would like to run a query only in case arrived here, but the transaction is closed
// how could i prevent from trasanction to close ?
ResultSet rs = stmt.executeQuery(" SELECT Bla,Bla From..");
Log.debug("");
}
catch (Exception e2) {
Log.debug("");
}
}
【问题讨论】:
-
创建一个新的声明。
-
你得到了什么异常,因为
select通常不会导致异常,除非你有语法错误。请注意,PostgreSQL 会因错误而中止事务,并且除了启动新事务之外没有任何恢复。 -
第一个例外是整数超出范围。
-
使用正确的sql来避免它?因为这正是我对交易的期望。失败时中止和回滚...
-
@Fildor 这是个人喜好问题,有些人(以及许多数据库系统)将事务提交或错误后回滚留给程序处理(例如,当可以采取一些纠正措施时)。
标签: java sql postgresql connection