11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2017 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * 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
246246 "Transaction propagation 'nested' not supported for WebSphere UOW transactions" );
247247 }
248248 if (pb == TransactionDefinition .PROPAGATION_SUPPORTS ||
249- pb == TransactionDefinition .PROPAGATION_REQUIRED || pb == TransactionDefinition .PROPAGATION_MANDATORY ) {
249+ pb == TransactionDefinition .PROPAGATION_REQUIRED ||
250+ pb == TransactionDefinition .PROPAGATION_MANDATORY ) {
250251 joinTx = true ;
251252 newSynch = (getTransactionSynchronization () != SYNCHRONIZATION_NEVER );
252253 }
@@ -264,7 +265,8 @@ else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
264265 "Transaction propagation 'mandatory' but no existing transaction found" );
265266 }
266267 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 ) {
268270 uowType = UOWSynchronizationRegistry .UOW_TYPE_LOCAL_TRANSACTION ;
269271 newSynch = (getTransactionSynchronization () == SYNCHRONIZATION_ALWAYS );
270272 }
@@ -278,14 +280,15 @@ else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
278280 logger .debug ("Creating new transaction with name [" + definition .getName () + "]: " + definition );
279281 }
280282 SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend (null ) : null );
283+ UOWActionAdapter <T > action = null ;
281284 try {
282285 if (definition .getTimeout () > TransactionDefinition .TIMEOUT_DEFAULT ) {
283286 this .uowManager .setUOWTimeout (uowType , definition .getTimeout ());
284287 }
285288 if (debug ) {
286289 logger .debug ("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx );
287290 }
288- UOWActionAdapter < T > action = new UOWActionAdapter <T >(
291+ action = new UOWActionAdapter <T >(
289292 definition , callback , (uowType == UOWManager .UOW_TYPE_GLOBAL_TRANSACTION ), !joinTx , newSynch , debug );
290293 this .uowManager .runUnderUOW (uowType , joinTx , action );
291294 if (debug ) {
@@ -294,10 +297,24 @@ else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
294297 return action .getResult ();
295298 }
296299 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 ;
298308 }
299309 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 ;
301318 }
302319 finally {
303320 if (suspendedResources != null ) {
@@ -330,6 +347,7 @@ private class UOWActionAdapter<T> implements UOWAction, SmartTransactionObject {
330347
331348 public UOWActionAdapter (TransactionDefinition definition , TransactionCallback <T > callback ,
332349 boolean actualTransaction , boolean newTransaction , boolean newSynchronization , boolean debug ) {
350+
333351 this .definition = definition ;
334352 this .callback = callback ;
335353 this .actualTransaction = actualTransaction ;
@@ -349,12 +367,15 @@ public void run() {
349367 }
350368 catch (Throwable ex ) {
351369 this .exception = ex ;
370+ if (status .isDebug ()) {
371+ logger .debug ("Rolling back on application exception from transaction callback" , ex );
372+ }
352373 uowManager .setRollbackOnly ();
353374 }
354375 finally {
355376 if (status .isLocalRollbackOnly ()) {
356377 if (status .isDebug ()) {
357- logger .debug ("Transactional code has requested rollback" );
378+ logger .debug ("Transaction callback has explicitly requested rollback" );
358379 }
359380 uowManager .setRollbackOnly ();
360381 }
@@ -376,6 +397,10 @@ public T getResult() {
376397 return this .result ;
377398 }
378399
400+ public Throwable getException () {
401+ return this .exception ;
402+ }
403+
379404 @ Override
380405 public boolean isRollbackOnly () {
381406 return uowManager .getRollbackOnly ();
0 commit comments