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
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=alwaysin the connection (see https://jdbc.postgresql.org/documentation/head/connect.html) to avoid the 'current transaction is aborted' syndroma.
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) {}
}
}
I was working with spring boot jpa and fixed by implementing @EnableTransactionManagement
Attached file may help you.
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.
I was working with spring boot jpa and fixed by implementing @EnableTransactionManagement
Attached file may help you.
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.