Skip to content

Commit a1635c1

Browse files
committed
clean up logic in XxxxIsolationDelegates
and make them more consistent with each other
1 parent cbb93ec commit a1635c1

File tree

4 files changed

+200
-151
lines changed

4 files changed

+200
-151
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/JdbcLogging.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ public interface JdbcLogging extends BasicLogger {
142142

143143
@LogMessage(level = TRACE)
144144
@Message(value = "Unable to reset connection back to auto-commit", id = 100019)
145-
void unableToResetAutoCommit();
145+
void unableToResetAutoCommit(@Cause Exception ignored);
146146

147147
@LogMessage(level = INFO)
148148
@Message(value = "Unable to release isolated connection", id = 100020)
149-
void unableToReleaseIsolatedConnection(@Cause Throwable ignored);
149+
void unableToReleaseIsolatedConnection(@Cause Exception ignored);
150150

151151
@LogMessage(level = INFO)
152152
@Message(value = "Unable to roll back isolated connection on exception ", id = 100021)

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

Lines changed: 88 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* @author Andrea Boriero
2424
*/
25-
public class JdbcIsolationDelegate implements IsolationDelegate {
25+
public final class JdbcIsolationDelegate implements IsolationDelegate {
2626

2727
private final JdbcConnectionAccess connectionAccess;
2828
private final SqlExceptionHelper sqlExceptionHelper;
@@ -40,88 +40,117 @@ public JdbcIsolationDelegate(JdbcConnectionAccess connectionAccess, SqlException
4040
this.sqlExceptionHelper = sqlExceptionHelper;
4141
}
4242

43-
protected JdbcConnectionAccess jdbcConnectionAccess() {
44-
return this.connectionAccess;
45-
}
46-
47-
protected SqlExceptionHelper sqlExceptionHelper() {
48-
return this.sqlExceptionHelper;
49-
}
50-
5143
@Override
52-
public <T> T delegateWork(WorkExecutorVisitable<T> work, boolean transacted) throws HibernateException {
53-
boolean wasAutoCommit = false;
44+
public <T> T delegateWork(WorkExecutorVisitable<T> work, boolean transacted)
45+
throws HibernateException {
46+
final Connection connection;
5447
try {
55-
final Connection connection = jdbcConnectionAccess().obtainConnection();
56-
try {
57-
if ( transacted ) {
58-
if ( connection.getAutoCommit() ) {
59-
wasAutoCommit = true;
60-
connection.setAutoCommit( false );
61-
}
62-
}
63-
64-
T result = work.accept( new WorkExecutor<>(), connection );
65-
66-
if ( transacted ) {
67-
connection.commit();
68-
}
48+
connection = connectionAccess.obtainConnection();
49+
}
50+
catch ( SQLException sqle ) {
51+
throw sqlExceptionHelper.convert( sqle, "Unable to obtain isolated JDBC connection" );
52+
}
6953

70-
return result;
54+
try {
55+
final boolean wasAutoCommit;
56+
try {
57+
wasAutoCommit = disableAutoCommit( transacted, connection );
58+
}
59+
catch (SQLException sqle) {
60+
throw sqlExceptionHelper.convert( sqle, "Unable to manage autocommit on isolated JDBC connection" );
7161
}
72-
catch ( Exception e ) {
73-
try {
74-
if ( transacted && !connection.isClosed() ) {
75-
connection.rollback();
76-
}
77-
}
78-
catch ( Exception exception ) {
79-
JDBC_MESSAGE_LOGGER.unableToRollBackIsolatedConnection( exception );
80-
}
8162

82-
if ( e instanceof HibernateException ) {
83-
throw e;
63+
try {
64+
return doWorkAndCommit( work, transacted, connection );
65+
}
66+
catch (Exception exception) {
67+
rollBack( transacted, connection, exception );
68+
if ( exception instanceof HibernateException he ) {
69+
throw he;
8470
}
85-
else if ( e instanceof SQLException sqle ) {
86-
throw sqlExceptionHelper().convert( sqle, "Error performing isolated work" );
71+
else if ( exception instanceof SQLException sqle ) {
72+
throw sqlExceptionHelper.convert( sqle, "Error performing isolated work" );
8773
}
8874
else {
89-
throw new HibernateException( "Error performing isolated work", e );
75+
throw new HibernateException( "Error performing isolated work", exception );
9076
}
9177
}
9278
finally {
93-
if ( transacted && wasAutoCommit ) {
94-
try {
95-
connection.setAutoCommit( true );
96-
}
97-
catch ( Exception ignore ) {
98-
JDBC_MESSAGE_LOGGER.unableToResetAutoCommit();
99-
}
100-
}
101-
try {
102-
jdbcConnectionAccess().releaseConnection( connection );
103-
}
104-
catch ( Exception ignored ) {
105-
JDBC_MESSAGE_LOGGER.unableToReleaseIsolatedConnection( ignored );
106-
}
79+
resetAutoCommit( transacted, wasAutoCommit, connection );
10780
}
10881
}
109-
catch ( SQLException sqle ) {
110-
throw sqlExceptionHelper().convert( sqle, "Unable to obtain isolated JDBC connection" );
82+
finally {
83+
releaseConnection( connection );
84+
}
85+
}
86+
87+
private static <T> T doWorkAndCommit(WorkExecutorVisitable<T> work, boolean transacted, Connection connection)
88+
throws SQLException {
89+
T result = work.accept( new WorkExecutor<>(), connection );
90+
if ( transacted ) {
91+
connection.commit();
92+
}
93+
return result;
94+
}
95+
96+
private void releaseConnection(Connection connection) {
97+
try {
98+
connectionAccess.releaseConnection( connection );
99+
}
100+
catch ( Exception exception ) {
101+
JDBC_MESSAGE_LOGGER.unableToReleaseIsolatedConnection( exception );
102+
}
103+
}
104+
105+
private static void rollBack(boolean transacted, Connection connection, Exception original) {
106+
try {
107+
if ( transacted && !connection.isClosed() ) {
108+
connection.rollback();
109+
}
110+
}
111+
catch ( Exception exception ) {
112+
JDBC_MESSAGE_LOGGER.unableToRollBackIsolatedConnection( exception );
113+
original.addSuppressed( exception );
114+
}
115+
}
116+
117+
private static void resetAutoCommit(boolean transacted, boolean wasAutoCommit, Connection connection) {
118+
if ( transacted && wasAutoCommit ) {
119+
try {
120+
connection.setAutoCommit( true );
121+
}
122+
catch ( Exception exception ) {
123+
JDBC_MESSAGE_LOGGER.unableToResetAutoCommit( exception );
124+
}
125+
}
126+
}
127+
128+
private static boolean disableAutoCommit(boolean transacted, Connection connection)
129+
throws SQLException {
130+
if ( transacted ) {
131+
final boolean wasAutoCommit = connection.getAutoCommit();
132+
if ( wasAutoCommit ) {
133+
connection.setAutoCommit( false );
134+
}
135+
return wasAutoCommit;
136+
}
137+
else {
138+
return false;
111139
}
112140
}
113141

114142
@Override
115-
public <T> T delegateCallable(Callable<T> callable, boolean transacted) throws HibernateException {
143+
public <T> T delegateCallable(Callable<T> callable, boolean transacted)
144+
throws HibernateException {
116145
// No connection, nothing to be suspended
117146
try {
118147
return callable.call();
119148
}
120149
catch ( HibernateException e ) {
121150
throw e;
122151
}
123-
catch ( Exception e ) {
124-
throw new HibernateException( e );
152+
catch ( Exception exception ) {
153+
throw new HibernateException( exception );
125154
}
126155
}
127156
}

0 commit comments

Comments
 (0)