From a25e24f37ee46595ce30045c6fb9efdb692b7386 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 15 Feb 2009 19:48:18 +0000 Subject: [PATCH] generified TransactionCallback; WebSphereUowTransactionManager preserves original exception in case of rollback (SPR-5270) --- .../jta/WebSphereUowTransactionManager.java | 30 +++++++++++-------- ...kPreferringPlatformTransactionManager.java | 4 +-- .../support/TransactionCallback.java | 6 ++-- .../support/TransactionOperations.java | 4 +-- .../support/TransactionTemplate.java | 6 ++-- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java index f537aa0b4ae..aed7d9db017 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/jta/WebSphereUowTransactionManager.java @@ -36,6 +36,7 @@ import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.DefaultTransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionSynchronizationManager; +import org.springframework.util.ReflectionUtils; /** * WebSphere-specific PlatformTransactionManager implementation that delegates @@ -62,11 +63,8 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * despite the official WebSphere recommendations). Use the {@link #execute} style * for any code that might require transaction suspension. * - *

This transaction manager is compatible with WebSphere 7.0 as well as recent - * WebSphere 6.0.x and 6.1.x versions. Check the documentation for your specific - * WebSphere version to find out whether UOWManager support is available. - * - *

The default JNDI location for the UOWManager is "java:comp/websphere/UOWManager". + *

This transaction manager is compatible with WebSphere 6.1.0.9 and above. + * The default JNDI location for the UOWManager is "java:comp/websphere/UOWManager". * If the location happens to differ according to your WebSphere documentation, * simply specify the actual location through this transaction manager's * "uowManagerName" bean property. @@ -191,7 +189,7 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager } - public Object execute(TransactionDefinition definition, TransactionCallback callback) throws TransactionException { + public T execute(TransactionDefinition definition, TransactionCallback callback) throws TransactionException { if (definition == null) { // Use defaults if no transaction definition given. definition = new DefaultTransactionDefinition(); @@ -257,7 +255,7 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager if (debug) { logger.debug("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx); } - UOWActionAdapter action = new UOWActionAdapter( + UOWActionAdapter action = new UOWActionAdapter( definition, callback, (uowType == UOWManager.UOW_TYPE_GLOBAL_TRANSACTION), !joinTx, newSynch, debug); this.uowManager.runUnderUOW(uowType, joinTx, action); if (debug) { @@ -282,11 +280,11 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager /** * Adapter that executes the given Spring transaction within the WebSphere UOWAction shape. */ - private class UOWActionAdapter implements UOWAction { + private class UOWActionAdapter implements UOWAction { private final TransactionDefinition definition; - private final TransactionCallback callback; + private final TransactionCallback callback; private final boolean actualTransaction; @@ -296,9 +294,11 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager private boolean debug; - private Object result; + private T result; - public UOWActionAdapter(TransactionDefinition definition, TransactionCallback callback, + private Throwable exception; + + public UOWActionAdapter(TransactionDefinition definition, TransactionCallback callback, boolean actualTransaction, boolean newTransaction, boolean newSynchronization, boolean debug) { this.definition = definition; this.callback = callback; @@ -316,6 +316,9 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager this.result = this.callback.doInTransaction(status); triggerBeforeCommit(status); } + catch (Throwable ex) { + this.exception = ex; + } finally { if (status.isLocalRollbackOnly()) { if (status.isDebug()) { @@ -332,7 +335,10 @@ public class WebSphereUowTransactionManager extends JtaTransactionManager } } - public Object getResult() { + public T getResult() { + if (this.exception != null) { + ReflectionUtils.rethrowRuntimeException(this.exception); + } return this.result; } } diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/CallbackPreferringPlatformTransactionManager.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/CallbackPreferringPlatformTransactionManager.java index 1613d3a7172..2b892aa3f22 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/CallbackPreferringPlatformTransactionManager.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/CallbackPreferringPlatformTransactionManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ public interface CallbackPreferringPlatformTransactionManager extends PlatformTr * @throws TransactionException in case of initialization, rollback, or system errors * @throws RuntimeException if thrown by the TransactionCallback */ - Object execute(TransactionDefinition definition, TransactionCallback callback) + T execute(TransactionDefinition definition, TransactionCallback callback) throws TransactionException; } diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionCallback.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionCallback.java index 85f1f9efb10..b725a9f3991 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionCallback.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionCallback.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ import org.springframework.transaction.TransactionStatus; * @see TransactionTemplate * @see CallbackPreferringPlatformTransactionManager */ -public interface TransactionCallback { +public interface TransactionCallback { /** * Gets called by {@link TransactionTemplate#execute} within a transactional context. @@ -50,6 +50,6 @@ public interface TransactionCallback { * @see TransactionTemplate#execute * @see CallbackPreferringPlatformTransactionManager#execute */ - Object doInTransaction(TransactionStatus status); + T doInTransaction(TransactionStatus status); } diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionOperations.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionOperations.java index 2361e029a88..5a86a53bfcb 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionOperations.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,6 @@ public interface TransactionOperations { * @return a result object returned by the callback, or null if none * @throws TransactionException in case of initialization, rollback, or system errors */ - Object execute(TransactionCallback action) throws TransactionException; + T execute(TransactionCallback action) throws TransactionException; } diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionTemplate.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionTemplate.java index ec26db77a88..b8ec65ee7f2 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionTemplate.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/support/TransactionTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -117,13 +117,13 @@ public class TransactionTemplate extends DefaultTransactionDefinition } - public Object execute(TransactionCallback action) throws TransactionException { + public T execute(TransactionCallback action) throws TransactionException { if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) { return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action); } else { TransactionStatus status = this.transactionManager.getTransaction(this); - Object result = null; + T result = null; try { result = action.doInTransaction(status); }