File tree Expand file tree Collapse file tree 5 files changed +42
-19
lines changed
engine/transaction/internal
resource/transaction/backend/jdbc/internal
test/java/org/hibernate/test/resource/transaction Expand file tree Collapse file tree 5 files changed +42
-19
lines changed Original file line number Diff line number Diff line change 21
21
22
22
/**
23
23
* @author Andrea Boriero
24
+ * @author Steve Ebersole
24
25
*/
25
26
public class TransactionImpl implements Transaction {
26
27
private static final Logger LOG = CoreLogging .logger ( TransactionImpl .class );
@@ -71,6 +72,13 @@ public void commit() {
71
72
@ Override
72
73
public void rollback () {
73
74
TransactionStatus status = transactionDriverControl .getStatus ();
75
+ if ( status == TransactionStatus .ROLLED_BACK || status == TransactionStatus .NOT_ACTIVE ) {
76
+ // Allow rollback() calls on completed transactions, just no-op.
77
+ LOG .debug ( "rollback() called on an inactive transaction" );
78
+ invalidate ();
79
+ return ;
80
+ }
81
+
74
82
if ( !status .canRollback () ) {
75
83
throw new TransactionException ( "Cannot rollback transaction in current status [" + status .name () + "]" );
76
84
}
Original file line number Diff line number Diff line change @@ -222,19 +222,35 @@ protected void errorIfInvalid() {
222
222
223
223
@ Override
224
224
public void commit () {
225
- if ( rollbackOnly ) {
226
- throw new TransactionException ( "Transaction was marked for rollback only; cannot commit" );
225
+ try {
226
+ if ( rollbackOnly ) {
227
+ throw new TransactionException ( "Transaction was marked for rollback only; cannot commit" );
228
+ }
229
+
230
+ JdbcResourceLocalTransactionCoordinatorImpl .this .beforeCompletionCallback ();
231
+ jdbcResourceTransaction .commit ();
232
+ JdbcResourceLocalTransactionCoordinatorImpl .this .afterCompletionCallback ( true );
233
+ }
234
+ catch (RuntimeException e ) {
235
+ try {
236
+ rollback ();
237
+ }
238
+ catch (RuntimeException e2 ) {
239
+ log .debug ( "Encountered failure rolling back failed commit" , e2 );;
240
+ }
241
+ throw e ;
227
242
}
228
-
229
- JdbcResourceLocalTransactionCoordinatorImpl .this .beforeCompletionCallback ();
230
- jdbcResourceTransaction .commit ();
231
- JdbcResourceLocalTransactionCoordinatorImpl .this .afterCompletionCallback ( true );
232
243
}
233
244
234
245
@ Override
235
246
public void rollback () {
236
- jdbcResourceTransaction .rollback ();
237
- JdbcResourceLocalTransactionCoordinatorImpl .this .afterCompletionCallback ( false );
247
+ if ( rollbackOnly || getStatus () == TransactionStatus .ACTIVE ) {
248
+ rollbackOnly = false ;
249
+ jdbcResourceTransaction .rollback ();
250
+ JdbcResourceLocalTransactionCoordinatorImpl .this .afterCompletionCallback ( false );
251
+ }
252
+
253
+ // no-op otherwise.
238
254
}
239
255
240
256
@ Override
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ public void testUserSynchronizationExceptions() {
106
106
// exception in beforeCompletion
107
107
registry .clearSynchronizations ();
108
108
registry = new SynchronizationRegistryStandardImpl ();
109
- synchronization = new SynchronizationErrorImpl ( false , true );
109
+ synchronization = SynchronizationErrorImpl . forAfter ( );
110
110
registry .registerSynchronization ( synchronization );
111
111
try {
112
112
registry .notifySynchronizationsAfterTransactionCompletion ( Status .STATUS_COMMITTED );
Original file line number Diff line number Diff line change @@ -88,14 +88,13 @@ public boolean shouldAutoJoinTransaction() {
88
88
catch (TransactionException expected ) {
89
89
}
90
90
finally {
91
- assertEquals ( TransactionStatus .MARKED_ROLLBACK , transactionCoordinator .getTransactionDriverControl ().getStatus () );
92
- transactionCoordinator .getTransactionDriverControl ().rollback ();
91
+ assertEquals ( TransactionStatus .NOT_ACTIVE , transactionCoordinator .getTransactionDriverControl ().getStatus () );
93
92
}
94
93
}
95
94
96
95
@ Test
97
96
@ SuppressWarnings ("EmptyCatchBlock" )
98
- public void testSynchronizationFailureMarksTransactionForRollbackOnly () {
97
+ public void testSynchronizationFailure () {
99
98
final TransactionCoordinatorOwnerTestingImpl owner = new TransactionCoordinatorOwnerTestingImpl ();
100
99
final JdbcResourceLocalTransactionCoordinatorBuilderImpl transactionCoordinatorBuilder =
101
100
new JdbcResourceLocalTransactionCoordinatorBuilderImpl ();
@@ -122,8 +121,10 @@ public boolean shouldAutoJoinTransaction() {
122
121
catch (Exception expected ) {
123
122
}
124
123
finally {
125
- assertEquals ( TransactionStatus .MARKED_ROLLBACK , transactionCoordinator .getTransactionDriverControl ().getStatus () );
126
- transactionCoordinator .getTransactionDriverControl ().rollback ();
124
+ assertEquals (
125
+ TransactionStatus .NOT_ACTIVE ,
126
+ transactionCoordinator .getTransactionDriverControl ().getStatus ()
127
+ );
127
128
}
128
129
}
129
130
}
Original file line number Diff line number Diff line change @@ -401,14 +401,13 @@ public void testMarkRollbackOnly() throws Exception {
401
401
catch (TransactionException expected ) {
402
402
}
403
403
finally {
404
- assertEquals ( TransactionStatus .MARKED_ROLLBACK , transactionCoordinator .getTransactionDriverControl ().getStatus () );
405
- transactionCoordinator .getTransactionDriverControl ().rollback ();
404
+ assertEquals ( TransactionStatus .NOT_ACTIVE , transactionCoordinator .getTransactionDriverControl ().getStatus () );
406
405
}
407
406
}
408
407
409
408
@ Test
410
409
@ SuppressWarnings ("EmptyCatchBlock" )
411
- public void testSynchronizationFailureMarksTransactionForRollbackOnly () throws Exception {
410
+ public void testSynchronizationFailure () throws Exception {
412
411
JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl (
413
412
transactionCoordinatorBuilder ,
414
413
owner ,
@@ -434,8 +433,7 @@ public void testSynchronizationFailureMarksTransactionForRollbackOnly() throws E
434
433
catch (Exception expected ) {
435
434
}
436
435
finally {
437
- assertEquals ( TransactionStatus .MARKED_ROLLBACK , transactionCoordinator .getTransactionDriverControl ().getStatus () );
438
- transactionCoordinator .getTransactionDriverControl ().rollback ();
436
+ assertEquals ( TransactionStatus .NOT_ACTIVE , transactionCoordinator .getTransactionDriverControl ().getStatus () );
439
437
}
440
438
}
441
439
}
You can’t perform that action at this time.
0 commit comments