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

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


{getToc} $title={Table of Contents} $count={true}


ERROR

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


Understanding the Error

This error means that you have executed a query that failed, and then tried to execute another query without rolling back the previous one. PostgreSQL does not allow you to continue a transaction after an error, unless you explicitly use a savepoint.


To solve this problem, you have a few options:

  • You can set autocommit to true in your connection settings, so that each query is executed in its own transaction and does not affect the others.
  • You can use rollback or commit to end the current transaction before executing another query.
  • You can use ON_ERROR_ROLLBACK in psql to automatically create and rollback savepoints for each query, so that you can continue the transaction after an error.
  • You can use a separate listener or tasklet with a new transaction to write the batch status, instead of doing it in the same writer as the data insertion.

Here is an example of how to use ON_ERROR_ROLLBACK in psql:

-- Enable ON_ERROR_ROLLBACK
\set ON_ERROR_ROLLBACK on

-- Create a table
CREATE TABLE test (id int, name varchar);

-- Insert some data
INSERT INTO test VALUES (1, 'Alice');
INSERT INTO test VALUES (2, 'Bob');

-- Try to insert a duplicate id
INSERT INTO test VALUES (1, 'Charlie'); -- This will fail

-- Try to select from the table
SELECT * FROM test; -- This will work

Conclusion

The problem is that PostgreSQL does not allow you to continue a transaction after an error, unless you explicitly use a savepoint. The solutions are to either enable autocommit, use rollback or commit, use ON_ERROR_ROLLBACK, or use a separate listener or tasklet with a new transaction.


Related Post




Previous Post Next Post