postgre数据库在上一个语句执行失败后,需要提交事务才能开始下一个语句

如题!

今天工作中遇到问题,在上一条sql语句执行失败后,紧接着执行下一条sql语句,结果报以下异常:


PSQLException: current transaction is aborted, commands ignored until end of transaction block


通过google到stackoverflow,发现原来是因为设置了connection.setAutocommit(false)


这样,postgre数据库在上一个语句执行失败后,会认为当前事务已经被破坏,如果马上开始下一条语句的话,就会报以上错误。


解决办法有:

1. 设置connection.setAutocommit(true)

2. 在执行下一条sql之前,提交一次事务

3.  上一个sql执行失败就抛出异常,不往下继续执行了。

4. Oracle没有以上问题,在某一条语句执行失败后,无视这条失败语句,继续执行


参考以下地址:

http://stackoverflow.com/questions/10399727/psqlexception-current-transaction-is-aborted-commands-ignored-until-end-of-tra


start doing statement.execute

keep on truckin, keep using the last connection because what could go wrong?

org.postgresql.util.PSQLException: 
  ERROR: current transaction is aborted, commands ignored until 
  end of transaction block

Workarounds:

You have a few options:

  1. Simplest solution: Don't be in a transaction. Set the connection.setAutoCommit(false); to connection.setAutoCommit(true);. It works because then the failed SQL is just ignored as a failed sql statement. You are welcome to fail sql statements all you want and postgresql won't stop you.

  2. Stay being in a transaction, but when you detect that the first sql has failed, either rollback/re-start or commit/restart the transaction. Then you can continue failing as many sql queries on that database connection as you want.

  3. Don't catch and ignore the Exception that is thrown when a sql statement fails. Then the program will stop on the malformed query.

  4. Get Oracle instead, Oracle doesn't throw an exception when you fail a query on a connection within a transaction and continue using that connection.




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值