[seam-commits] Seam SVN: r7507 - trunk/src/ioc/org/jboss/seam/ioc/spring.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Mar 4 15:32:41 EST 2008
Author: youngm
Date: 2008-03-04 15:32:41 -0500 (Tue, 04 Mar 2008)
New Revision: 7507
Modified:
trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTransaction.java
trunk/src/ioc/org/jboss/seam/ioc/spring/spring-2.1.xsd
Log:
JBSEAM-2702
Modified: trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTransaction.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTransaction.java 2008-03-04 04:04:06 UTC (rev 7506)
+++ trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTransaction.java 2008-03-04 20:32:41 UTC (rev 7507)
@@ -17,10 +17,12 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.contexts.ServletLifecycle;
import org.jboss.seam.core.Expressions.ValueExpression;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.seam.transaction.AbstractUserTransaction;
+import org.springframework.beans.factory.BeanFactory;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
@@ -28,6 +30,7 @@
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
+import org.springframework.web.context.support.WebApplicationContextUtils;
@Name("org.jboss.seam.transaction.transaction")
@Scope(ScopeType.EVENT)
@@ -39,6 +42,8 @@
private ValueExpression<PlatformTransactionManager> platformTransactionManager;
+ private String platformTransactionManagerName;
+
private DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
private boolean conversationContextRequired = true;
@@ -52,13 +57,11 @@
{
if (TransactionSynchronizationManager.isSynchronizationActive())
{
- TransactionSynchronizationManager
- .registerSynchronization(new JtaSpringSynchronizationAdapter(sync));
+ TransactionSynchronizationManager.registerSynchronization(new JtaSpringSynchronizationAdapter(sync));
}
else
{
- throw new IllegalStateException(
- "TransactionSynchronization not available with this Spring Transaction Manager");
+ throw new IllegalStateException("TransactionSynchronization not available with this Spring Transaction Manager");
}
}
@@ -68,16 +71,60 @@
{
throw new NotSupportedException("A Spring transaction is already active.");
}
- currentTransaction = platformTransactionManager.getValue().getTransaction(definition);
+ currentTransaction = getPlatformTransactionManagerRequired().getTransaction(definition);
}
- public void commit() throws RollbackException, HeuristicMixedException,
- HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
+ /**
+ * Obtains a PlatformTransactionManager from either the name or expression
+ * specified.
+ *
+ * @return
+ */
+ protected PlatformTransactionManager getPlatformTransactionManager()
{
+ if (((platformTransactionManagerName == null || "".equals(platformTransactionManagerName)) && platformTransactionManager == null) || (platformTransactionManagerName != null && !"".equals(platformTransactionManagerName)) && platformTransactionManager != null)
+ {
+ throw new IllegalArgumentException("When configuring spring:spring-transaction you must specify either platformTransactionManager or platformTransactionManagerName.");
+ }
+ if ((platformTransactionManagerName == null || "".equals(platformTransactionManagerName)))
+ {
+ return platformTransactionManager.getValue();
+ }
+ BeanFactory beanFactory = findBeanFactory();
+ if (beanFactory == null)
+ {
+ log.debug("BeanFactory either not found or not yet available.");
+ return null;
+ }
+ PlatformTransactionManager ptm = (PlatformTransactionManager) beanFactory.getBean(platformTransactionManagerName);
+ return ptm;
+ }
+
+ private PlatformTransactionManager getPlatformTransactionManagerRequired() {
+ PlatformTransactionManager ptm = getPlatformTransactionManager();
+ if (ptm == null)
+ {
+ throw new IllegalStateException("Unable to find PlatformTransactionManager");
+ }
+ return ptm;
+ }
+
+ /**
+ * Attempts to find a BeanFactory and return the instance found.
+ *
+ * @return BeanFactory or null if non found.
+ */
+ protected BeanFactory findBeanFactory()
+ {
+ return WebApplicationContextUtils.getWebApplicationContext(ServletLifecycle.getServletContext());
+ }
+
+ public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
+ {
assertActive();
try
{
- platformTransactionManager.getValue().commit(currentTransaction);
+ getPlatformTransactionManagerRequired().commit(currentTransaction);
}
finally
{
@@ -87,6 +134,11 @@
public int getStatus() throws SystemException
{
+ PlatformTransactionManager ptm = getPlatformTransactionManager();
+ if (ptm == null)
+ {
+ return Status.STATUS_NO_TRANSACTION;
+ }
if (TransactionSynchronizationManager.isActualTransactionActive())
{
TransactionStatus transaction = null;
@@ -94,7 +146,7 @@
{
if (currentTransaction == null)
{
- transaction = platformTransactionManager.getValue().getTransaction(definition);
+ transaction = ptm.getTransaction(definition);
if (transaction.isNewTransaction())
{
return Status.STATUS_COMMITTED;
@@ -128,7 +180,7 @@
{
if (currentTransaction == null)
{
- platformTransactionManager.getValue().commit(transaction);
+ ptm.commit(transaction);
}
}
}
@@ -140,7 +192,7 @@
assertActive();
try
{
- platformTransactionManager.getValue().rollback(currentTransaction);
+ getPlatformTransactionManagerRequired().rollback(currentTransaction);
}
finally
{
@@ -153,11 +205,9 @@
*/
private void assertActive()
{
- if (!TransactionSynchronizationManager.isActualTransactionActive()
- || currentTransaction == null)
+ if (!TransactionSynchronizationManager.isActualTransactionActive() || currentTransaction == null)
{
- throw new IllegalStateException("No transaction currently active that Seam started."
- + "Seam should only be able to committ or rollback transactions it started.");
+ throw new IllegalStateException("No transaction currently active that Seam started." + "Seam should only be able to committ or rollback transactions it started.");
}
}
@@ -168,11 +218,12 @@
throw new IllegalStateException("No Spring Transaction is currently available.");
}
TransactionStatus transaction = null;
+ PlatformTransactionManager ptm = getPlatformTransactionManagerRequired();
try
{
if (currentTransaction == null)
{
- transaction = platformTransactionManager.getValue().getTransaction(definition);
+ transaction = ptm.getTransaction(definition);
}
else
{
@@ -184,7 +235,7 @@
{
if (currentTransaction == null)
{
- platformTransactionManager.getValue().commit(transaction);
+ ptm.commit(transaction);
}
}
}
@@ -205,7 +256,7 @@
if (joinTransaction == null)
{
// If not set attempt to detect if we should join or not
- if (!(platformTransactionManager.getValue() instanceof JpaTransactionManager))
+ if (!(getPlatformTransactionManagerRequired() instanceof JpaTransactionManager))
{
super.enlist(entityManager);
}
@@ -217,23 +268,32 @@
}
@Destroy
- public void cleanupCurrentTransaction() {
- if(currentTransaction != null) {
- try {
+ public void cleanupCurrentTransaction()
+ {
+ if (currentTransaction != null)
+ {
+ try
+ {
log.debug("Attempting to rollback left over transaction. Should never be called.");
- platformTransactionManager.getValue().rollback(currentTransaction);
- } catch(Throwable e) {
- //ignore
+ getPlatformTransactionManagerRequired().rollback(currentTransaction);
}
+ catch (Throwable e)
+ {
+ // ignore
+ }
}
}
- public void setPlatformTransactionManager(
- ValueExpression<PlatformTransactionManager> platformTransactionManager)
+ public void setPlatformTransactionManager(ValueExpression<PlatformTransactionManager> platformTransactionManager)
{
this.platformTransactionManager = platformTransactionManager;
}
+ public void setPlatformTransactionManagerName(String platformTransactionManagerName)
+ {
+ this.platformTransactionManagerName = platformTransactionManagerName;
+ }
+
@Override
public boolean isConversationContextRequired()
{
@@ -277,15 +337,17 @@
sync.beforeCompletion();
}
- private int convertSpringStatus(int springStatus) {
- switch(springStatus) {
- case TransactionSynchronization.STATUS_COMMITTED :
- return Status.STATUS_COMMITTED;
- case TransactionSynchronization.STATUS_ROLLED_BACK :
- return Status.STATUS_ROLLEDBACK;
- default :
- return Status.STATUS_UNKNOWN;
- }
+ private int convertSpringStatus(int springStatus)
+ {
+ switch (springStatus)
+ {
+ case TransactionSynchronization.STATUS_COMMITTED:
+ return Status.STATUS_COMMITTED;
+ case TransactionSynchronization.STATUS_ROLLED_BACK:
+ return Status.STATUS_ROLLEDBACK;
+ default:
+ return Status.STATUS_UNKNOWN;
+ }
}
}
}
Modified: trunk/src/ioc/org/jboss/seam/ioc/spring/spring-2.1.xsd
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/spring/spring-2.1.xsd 2008-03-04 04:04:06 UTC (rev 7506)
+++ trunk/src/ioc/org/jboss/seam/ioc/spring/spring-2.1.xsd 2008-03-04 20:32:41 UTC (rev 7507)
@@ -56,6 +56,15 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
+ <xs:attribute name="platform-transaction-manager-name">
+ <xs:annotation>
+ <xs:documentation>
+ <![CDATA[
+ A spring bean name of a PlatformTransactionManager obtained from a BeanFactory instead of EL.
+ ]]>
+ </xs:documentation>
+ </xs:annotation>
+ </xs:attribute>
<xs:attribute name="conversation-context-required" type="xs:boolean" use="optional" default="true">
<xs:annotation>
<xs:documentation>
@@ -69,7 +78,7 @@
<xs:annotation>
<xs:documentation>
Should this transaction manager participate in request to join a transaction. For JTA
- transactions set to true.
+ transactions set to true.
</xs:documentation>
</xs:annotation>
</xs:attribute>
More information about the seam-commits
mailing list