22
22
/**
23
23
* @author Andrea Boriero
24
24
*/
25
- public class JdbcIsolationDelegate implements IsolationDelegate {
25
+ public final class JdbcIsolationDelegate implements IsolationDelegate {
26
26
27
27
private final JdbcConnectionAccess connectionAccess ;
28
28
private final SqlExceptionHelper sqlExceptionHelper ;
@@ -40,88 +40,117 @@ public JdbcIsolationDelegate(JdbcConnectionAccess connectionAccess, SqlException
40
40
this .sqlExceptionHelper = sqlExceptionHelper ;
41
41
}
42
42
43
- protected JdbcConnectionAccess jdbcConnectionAccess () {
44
- return this .connectionAccess ;
45
- }
46
-
47
- protected SqlExceptionHelper sqlExceptionHelper () {
48
- return this .sqlExceptionHelper ;
49
- }
50
-
51
43
@ 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 ;
54
47
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
+ }
69
53
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" );
71
61
}
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
- }
81
62
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 ;
84
70
}
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" );
87
73
}
88
74
else {
89
- throw new HibernateException ( "Error performing isolated work" , e );
75
+ throw new HibernateException ( "Error performing isolated work" , exception );
90
76
}
91
77
}
92
78
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 );
107
80
}
108
81
}
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 ;
111
139
}
112
140
}
113
141
114
142
@ 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 {
116
145
// No connection, nothing to be suspended
117
146
try {
118
147
return callable .call ();
119
148
}
120
149
catch ( HibernateException e ) {
121
150
throw e ;
122
151
}
123
- catch ( Exception e ) {
124
- throw new HibernateException ( e );
152
+ catch ( Exception exception ) {
153
+ throw new HibernateException ( exception );
125
154
}
126
155
}
127
156
}
0 commit comments