1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -246,7 +246,8 @@ public <T> T execute(TransactionDefinition definition, TransactionCallback<T> ca
246
246
"Transaction propagation 'nested' not supported for WebSphere UOW transactions" );
247
247
}
248
248
if (pb == TransactionDefinition .PROPAGATION_SUPPORTS ||
249
- pb == TransactionDefinition .PROPAGATION_REQUIRED || pb == TransactionDefinition .PROPAGATION_MANDATORY ) {
249
+ pb == TransactionDefinition .PROPAGATION_REQUIRED ||
250
+ pb == TransactionDefinition .PROPAGATION_MANDATORY ) {
250
251
joinTx = true ;
251
252
newSynch = (getTransactionSynchronization () != SYNCHRONIZATION_NEVER );
252
253
}
@@ -264,7 +265,8 @@ else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
264
265
"Transaction propagation 'mandatory' but no existing transaction found" );
265
266
}
266
267
if (pb == TransactionDefinition .PROPAGATION_SUPPORTS ||
267
- pb == TransactionDefinition .PROPAGATION_NOT_SUPPORTED || pb == TransactionDefinition .PROPAGATION_NEVER ) {
268
+ pb == TransactionDefinition .PROPAGATION_NOT_SUPPORTED ||
269
+ pb == TransactionDefinition .PROPAGATION_NEVER ) {
268
270
uowType = UOWSynchronizationRegistry .UOW_TYPE_LOCAL_TRANSACTION ;
269
271
newSynch = (getTransactionSynchronization () == SYNCHRONIZATION_ALWAYS );
270
272
}
@@ -278,14 +280,15 @@ else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
278
280
logger .debug ("Creating new transaction with name [" + definition .getName () + "]: " + definition );
279
281
}
280
282
SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend (null ) : null );
283
+ UOWActionAdapter <T > action = null ;
281
284
try {
282
285
if (definition .getTimeout () > TransactionDefinition .TIMEOUT_DEFAULT ) {
283
286
this .uowManager .setUOWTimeout (uowType , definition .getTimeout ());
284
287
}
285
288
if (debug ) {
286
289
logger .debug ("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx );
287
290
}
288
- UOWActionAdapter < T > action = new UOWActionAdapter <T >(
291
+ action = new UOWActionAdapter <T >(
289
292
definition , callback , (uowType == UOWManager .UOW_TYPE_GLOBAL_TRANSACTION ), !joinTx , newSynch , debug );
290
293
this .uowManager .runUnderUOW (uowType , joinTx , action );
291
294
if (debug ) {
@@ -294,10 +297,24 @@ else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
294
297
return action .getResult ();
295
298
}
296
299
catch (UOWException ex ) {
297
- throw new TransactionSystemException ("UOWManager transaction processing failed" , ex );
300
+ TransactionSystemException tse =
301
+ new TransactionSystemException ("UOWManager transaction processing failed" , ex );
302
+ Throwable appEx = action .getException ();
303
+ if (appEx != null ) {
304
+ logger .error ("Application exception overridden by rollback exception" , appEx );
305
+ tse .initApplicationException (appEx );
306
+ }
307
+ throw tse ;
298
308
}
299
309
catch (UOWActionException ex ) {
300
- throw new TransactionSystemException ("UOWManager threw unexpected UOWActionException" , ex );
310
+ TransactionSystemException tse =
311
+ new TransactionSystemException ("UOWManager threw unexpected UOWActionException" , ex );
312
+ Throwable appEx = action .getException ();
313
+ if (appEx != null ) {
314
+ logger .error ("Application exception overridden by rollback exception" , appEx );
315
+ tse .initApplicationException (appEx );
316
+ }
317
+ throw tse ;
301
318
}
302
319
finally {
303
320
if (suspendedResources != null ) {
@@ -330,6 +347,7 @@ private class UOWActionAdapter<T> implements UOWAction, SmartTransactionObject {
330
347
331
348
public UOWActionAdapter (TransactionDefinition definition , TransactionCallback <T > callback ,
332
349
boolean actualTransaction , boolean newTransaction , boolean newSynchronization , boolean debug ) {
350
+
333
351
this .definition = definition ;
334
352
this .callback = callback ;
335
353
this .actualTransaction = actualTransaction ;
@@ -349,12 +367,15 @@ public void run() {
349
367
}
350
368
catch (Throwable ex ) {
351
369
this .exception = ex ;
370
+ if (status .isDebug ()) {
371
+ logger .debug ("Rolling back on application exception from transaction callback" , ex );
372
+ }
352
373
uowManager .setRollbackOnly ();
353
374
}
354
375
finally {
355
376
if (status .isLocalRollbackOnly ()) {
356
377
if (status .isDebug ()) {
357
- logger .debug ("Transactional code has requested rollback" );
378
+ logger .debug ("Transaction callback has explicitly requested rollback" );
358
379
}
359
380
uowManager .setRollbackOnly ();
360
381
}
@@ -376,6 +397,10 @@ public T getResult() {
376
397
return this .result ;
377
398
}
378
399
400
+ public Throwable getException () {
401
+ return this .exception ;
402
+ }
403
+
379
404
@ Override
380
405
public boolean isRollbackOnly () {
381
406
return uowManager .getRollbackOnly ();
0 commit comments