Skip to content

Commit 4561943

Browse files
committed
Fix spurious errors due to PostgreSQL and CockroachDB statement timeout not resetting properly
1 parent 078a452 commit 4561943

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

hibernate-testing/src/main/java/org/hibernate/testing/transaction/TransactionUtil.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,15 @@ private static void setJdbcTimeout(Dialect dialect, Connection connection, long
604604
st.execute( String.format( "SET statement_timeout TO %d", millis ) );
605605
}
606606
catch (SQLException ex) {
607-
// Ignore if resetting the statement timeout to 0 fails
608-
// Since PostgreSQL is transactional anyway,
609-
// the prior change of statement timeout will be undone on rollback
610-
if ( millis != 0 ) {
607+
if ( "ERROR: current transaction is aborted, commands ignored until end of transaction block".equals( ex.getMessage() ) ) {
608+
// The connection would be rolled back anyway, but session variables are not transactional,
609+
// so rollback immediately to be able to reset the statement timeout as well
610+
connection.rollback();
611+
try (Statement st = connection.createStatement()) {
612+
st.execute( String.format( "SET statement_timeout TO %d", millis ) );
613+
}
614+
}
615+
else {
611616
throw ex;
612617
}
613618
}

0 commit comments

Comments
 (0)