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

前端 未结 20 1681
借酒劲吻你
借酒劲吻你 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:25

    The issue has been fixed in Infinispan 5.1.5.CR1: ISPN-2023

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

    I got this error using Java and PostgreSQL doing an insert on a table. I will illustrate how you can reproduce this error:

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

    Summary:

    The reason you get this error is because you have entered a transaction and one of your SQL Queries failed, and you gobbled up that failure and ignored it. But that wasn't enough, THEN you used that same connection, using the SAME TRANSACTION to run another query. The exception gets thrown on the second, correctly formed query because you are using a broken transaction to do additional work. PostgreSQL by default stops you from doing this.

    I'm using: PostgreSQL 9.1.6 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2), 64-bit".

    My PostgreSQL driver is: postgresql-9.2-1000.jdbc4.jar

    Using Java version: Java 1.7

    Here is the table create statement to illustrate the Exception:

    CREATE TABLE moobar
    (
        myval   INT
    );
    

    Java program causes the error:

    public void postgresql_insert()
    {
        try  
        {
            connection.setAutoCommit(false);  //start of transaction.
            
            Statement statement = connection.createStatement();
            
            System.out.println("start doing statement.execute");
            
            statement.execute(
                    "insert into moobar values(" +
                    "'this SQL statement fails, and it " +
                    "is gobbled up by the catch, okfine'); ");
         
            //The above line throws an exception because we try to cram
            //A string into an Int.  I Expect this, what happens is we gobble 
            //the Exception and ignore it like nothing is wrong.
            //But remember, we are in a TRANSACTION!  so keep reading.
    
            System.out.println("statement.execute done");
            
            statement.close();
            
        }
        catch (SQLException sqle)
        {
            System.out.println("keep on truckin, keep using " +
                    "the last connection because what could go wrong?");
        }
        
        try{
            Statement statement = connection.createStatement();
            
            statement.executeQuery("select * from moobar");
    
            //This SQL is correctly formed, yet it throws the 
            //'transaction is aborted' SQL Exception, why?  Because:
            //A.  you were in a transaction.
            //B.  You ran a SQL statement that failed.
            //C.  You didn't do a rollback or commit on the affected connection.
            
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
        }   
    
    }
    

    The above code produces this output for me:

    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.

    In defense of PostgreSQL's decision to do things this way... Oracle was making you soft in the middle letting you do dumb stuff and overlooking it.

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

    I had the same issue but then realised there is a table with the same name in the database. After deleting that I was able to import the file.

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

    The reason for this error is that there are other database before the wrong operation led to the current database operation can not be carried out(i use google translation to translate my chinese to english)

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

    This can happen if you are out of disk space on the volume.

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

    Try this COMMIT;

    I run that in pgadmin4. It may help. It has to do with the previous command stopping prematurely

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