File tree 2 files changed +43
-0
lines changed
main/java/org/hibernate/resource/transaction/backend/jdbc/internal
test/java/org/hibernate/test/resource/transaction/jdbc
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 10
10
import java .util .List ;
11
11
import javax .transaction .Status ;
12
12
13
+ import org .hibernate .TransactionException ;
13
14
import org .hibernate .engine .jdbc .spi .JdbcServices ;
14
15
import org .hibernate .engine .transaction .spi .IsolationDelegate ;
15
16
import org .hibernate .engine .transaction .spi .TransactionObserver ;
@@ -185,6 +186,7 @@ public void removeObserver(TransactionObserver observer) {
185
186
public class TransactionDriverControlImpl implements TransactionDriver {
186
187
private final JdbcResourceTransaction jdbcResourceTransaction ;
187
188
private boolean invalid ;
189
+ private boolean rollbackOnly = false ;
188
190
189
191
public TransactionDriverControlImpl (JdbcResourceTransaction jdbcResourceTransaction ) {
190
192
super ();
@@ -211,6 +213,10 @@ protected void errorIfInvalid() {
211
213
212
214
@ Override
213
215
public void commit () {
216
+ if ( rollbackOnly ) {
217
+ throw new TransactionException ( "Transaction was marked for rollback only; cannot commit" );
218
+ }
219
+
214
220
JdbcResourceLocalTransactionCoordinatorImpl .this .beforeCompletionCallback ();
215
221
jdbcResourceTransaction .commit ();
216
222
JdbcResourceLocalTransactionCoordinatorImpl .this .afterCompletionCallback ( true );
@@ -229,7 +235,14 @@ public TransactionStatus getStatus() {
229
235
230
236
@ Override
231
237
public void markRollbackOnly () {
238
+ if ( log .isDebugEnabled () ) {
239
+ log .debug (
240
+ "JDBC transaction marked for rollback-only (exception provided for stack trace)" ,
241
+ new Exception ( "exception just for purpose of providing stack trace" )
242
+ );
243
+ }
232
244
245
+ rollbackOnly = true ;
233
246
}
234
247
}
235
248
}
Original file line number Diff line number Diff line change 6
6
*/
7
7
package org .hibernate .test .resource .transaction .jdbc ;
8
8
9
+ import org .hibernate .TransactionException ;
9
10
import org .hibernate .resource .transaction .TransactionCoordinator ;
10
11
import org .hibernate .resource .transaction .TransactionCoordinatorBuilder ;
11
12
import org .hibernate .resource .transaction .backend .jdbc .internal .JdbcResourceLocalTransactionCoordinatorBuilderImpl ;
@@ -50,4 +51,33 @@ public boolean shouldAutoJoinTransaction() {
50
51
assertEquals ( 0 , sync .getFailedCompletionCount () );
51
52
52
53
}
54
+
55
+ @ Test
56
+ @ SuppressWarnings ("EmptyCatchBlock" )
57
+ public void testMarkRollbackOnly () {
58
+ final TransactionCoordinatorOwnerTestingImpl owner = new TransactionCoordinatorOwnerTestingImpl ();
59
+ final JdbcResourceLocalTransactionCoordinatorBuilderImpl transactionCoordinatorBuilder =
60
+ new JdbcResourceLocalTransactionCoordinatorBuilderImpl ();
61
+
62
+ final TransactionCoordinator transactionCoordinator = transactionCoordinatorBuilder .buildTransactionCoordinator (
63
+ owner ,
64
+ new TransactionCoordinatorBuilder .TransactionCoordinatorOptions () {
65
+ @ Override
66
+ public boolean shouldAutoJoinTransaction () {
67
+ return false ;
68
+ }
69
+ }
70
+ );
71
+
72
+ transactionCoordinator .getTransactionDriverControl ().begin ();
73
+ transactionCoordinator .getTransactionDriverControl ().markRollbackOnly ();
74
+ try {
75
+ transactionCoordinator .getTransactionDriverControl ().commit ();
76
+ }
77
+ catch (TransactionException expected ) {
78
+ }
79
+ finally {
80
+ transactionCoordinator .getTransactionDriverControl ().rollback ();
81
+ }
82
+ }
53
83
}
You can’t perform that action at this time.
0 commit comments