Skip to content

Commit a8cddb9

Browse files
committed
HHH-14434 Fix autocommit reset for connection used in DdlTransactionIsolatorNonJtaImpl
1 parent 9135f69 commit a8cddb9

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class DdlTransactionIsolatorNonJtaImpl implements DdlTransactionIsolator
2222
private final JdbcContext jdbcContext;
2323

2424
private Connection jdbcConnection;
25+
private boolean unsetAutoCommit;
2526

2627
public DdlTransactionIsolatorNonJtaImpl(JdbcContext jdbcContext) {
2728
this.jdbcContext = jdbcContext;
@@ -49,6 +50,7 @@ public Connection getIsolatedConnection() {
4950
try {
5051
jdbcConnection.commit();
5152
jdbcConnection.setAutoCommit( true );
53+
unsetAutoCommit = true;
5254
}
5355
catch (SQLException e) {
5456
throw jdbcContext.getSqlExceptionHelper().convert(
@@ -80,10 +82,28 @@ public Connection getIsolatedConnection() {
8082
public void release() {
8183
if ( jdbcConnection != null ) {
8284
try {
83-
jdbcContext.getJdbcConnectionAccess().releaseConnection( jdbcConnection );
85+
if ( unsetAutoCommit ) {
86+
try {
87+
jdbcConnection.setAutoCommit( false );
88+
}
89+
catch (SQLException e) {
90+
throw jdbcContext.getSqlExceptionHelper().convert(
91+
e,
92+
"Unable to set auto commit to false for JDBC Connection used for DDL execution"
93+
);
94+
}
95+
}
8496
}
85-
catch (SQLException e) {
86-
throw jdbcContext.getSqlExceptionHelper().convert( e, "Unable to release JDBC Connection used for DDL execution" );
97+
finally {
98+
try {
99+
jdbcContext.getJdbcConnectionAccess().releaseConnection( jdbcConnection );
100+
}
101+
catch (SQLException e) {
102+
throw jdbcContext.getSqlExceptionHelper().convert(
103+
e,
104+
"Unable to release JDBC Connection used for DDL execution"
105+
);
106+
}
87107
}
88108
}
89109
}

0 commit comments

Comments
 (0)