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

前端 未结 20 1675
借酒劲吻你
借酒劲吻你 2020-12-04 06:51

I am seeing the following (truncated) stacktrace in the server.log file of JBoss 7.1.1 Final:

Caused by: org.postgresql.util.PSQLException: 
ERROR: current t         


        
相关标签:
20条回答
  • 2020-12-04 07:30

    There's been some work done on the postgresql JDBC Driver, related to this behaviour:
    see https://github.com/pgjdbc/pgjdbc/pull/477

    It is now possible, by setting

    autosave=always
    in the connection (see https://jdbc.postgresql.org/documentation/head/connect.html) to avoid the 'current transaction is aborted' syndroma.
    Overhead due to handling a savepoint around the statement execution is kept very low (see link above for details).

    0 讨论(0)
  • 2020-12-04 07:31

    I think that the best solution is to use java.sql.Savepoint.

    Before you execute a query which can throw SQLException, use the method Connection.setSavepoint(), and if an exception is thrown you only rollback to this savepoint, not the whole transaction.

    Example code:

    Connection conn = null;
    Savepoint savepoint = null;
    try {
        conn = getConnection();
        savepoint = conn.setSavepoint();
        //execute some query
    } catch(SQLException e) {
        if(conn != null && savepoint != null) {
            conn.rollback(savepoint);
        }
    } finally {
       if(conn != null) {
          try {
              conn.close();
          } catch(SQLException e) {}
    
       }
    }
    
    0 讨论(0)
  • 2020-12-04 07:33

    I was working with spring boot jpa and fixed by implementing @EnableTransactionManagement

    Attached file may help you.

    0 讨论(0)
  • 2020-12-04 07:36

    I use spring with @Transactional annotation, and I catch the exception and for some exception I will retry 3 times.

    For posgresql, when got exception, you can't use same Connection to commit any more.You must rollback first.

    For my case, I use the DatasourceUtils to get current connection and call connection.rollback() manually. And the call the method recruive to retry.

    0 讨论(0)
  • 2020-12-04 07:37

    I was working with spring boot jpa and fixed by implementing @EnableTransactionManagement

    Attached file may help you.

    0 讨论(0)
  • 2020-12-04 07:38

    Check the output before the statement that caused current transaction is aborted. This typically means that database threw an exception that your code had ignored and now expecting next queries to return some data.

    So you now have a state mismatch between your application, which considers things are fine, and database, that requires you to rollback and re-start your transaction from the beginning.

    You should catch all exceptions and rollback transactions in such cases.

    Here's a similar issue.

    0 讨论(0)
提交回复
热议问题