[jboss-cvs] jboss-seam/src/main/org/jboss/seam/core ...
Gavin King
gavin.king at jboss.com
Sun Jun 17 23:21:33 EDT 2007
User: gavin
Date: 07/06/17 23:21:33
Modified: src/main/org/jboss/seam/core
LocalTransactionListener.java
ManagedHibernateSession.java
ManagedJbpmContext.java
ManagedPersistenceContext.java
TransactionListener.java
Log:
JBSEAM-1482
Revision Changes Path
1.4 +2 -0 jboss-seam/src/main/org/jboss/seam/core/LocalTransactionListener.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: LocalTransactionListener.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/LocalTransactionListener.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- LocalTransactionListener.java 30 May 2007 20:27:06 -0000 1.3
+++ LocalTransactionListener.java 18 Jun 2007 03:21:33 -0000 1.4
@@ -1,10 +1,12 @@
package org.jboss.seam.core;
import javax.ejb.Local;
+import javax.transaction.Synchronization;
@Local
public interface LocalTransactionListener
{
public void scheduleEvent(String type, Object... parameters);
+ public void registerSynchronization(Synchronization sync);
public void destroy();
}
1.36 +50 -6 jboss-seam/src/main/org/jboss/seam/core/ManagedHibernateSession.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ManagedHibernateSession.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedHibernateSession.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -b -r1.35 -r1.36
--- ManagedHibernateSession.java 11 Jun 2007 05:30:27 -0000 1.35
+++ ManagedHibernateSession.java 18 Jun 2007 03:21:33 -0000 1.36
@@ -1,4 +1,4 @@
-//$Id: ManagedHibernateSession.java,v 1.35 2007/06/11 05:30:27 gavin Exp $
+//$Id: ManagedHibernateSession.java,v 1.36 2007/06/18 03:21:33 gavin Exp $
package org.jboss.seam.core;
import static org.jboss.seam.InterceptionType.NEVER;
@@ -11,6 +11,8 @@
import javax.naming.NamingException;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
import org.hibernate.FlushMode;
import org.hibernate.Session;
@@ -23,11 +25,13 @@
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.Lifecycle;
import org.jboss.seam.core.Expressions.ValueExpression;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.seam.persistence.HibernateSessionProxy;
+import org.jboss.seam.transaction.Transaction;
import org.jboss.seam.util.Naming;
/**
@@ -40,7 +44,7 @@
@Scope(ScopeType.CONVERSATION)
@Intercept(NEVER)
public class ManagedHibernateSession
- implements Serializable, HttpSessionActivationListener, Mutable, PersistenceContextManager
+ implements Serializable, HttpSessionActivationListener, Mutable, PersistenceContextManager, Synchronization
{
/** The serialVersionUID */
@@ -54,6 +58,8 @@
private ValueExpression<SessionFactory> sessionFactory;
private List<Filter> filters = new ArrayList<Filter>(0);
+ private transient boolean synchronizationRegistered;
+
public boolean clearDirty()
{
return true;
@@ -101,13 +107,19 @@
}
@Unwrap
- public Session getSession()
+ public Session getSession() throws SystemException
{
if (session==null) initSession();
//join the transaction
- if ( !Lifecycle.isDestroying() )
+ if ( !synchronizationRegistered && !Lifecycle.isDestroying() && Transaction.instance().isActive() )
+ {
+ LocalTransactionListener transactionListener = TransactionListener.instance();
+ if (transactionListener!=null)
{
+ transactionListener.registerSynchronization(this);
+ synchronizationRegistered = true;
+ }
session.isOpen();
}
@@ -130,6 +142,40 @@
@Destroy
public void destroy()
{
+ if ( !synchronizationRegistered )
+ {
+ //in requests that come through SeamPhaseListener,
+ //there can be multiple transactions per request,
+ //but they are all completed by the time contexts
+ //are destroyed
+ //so wait until the end of the request to close
+ //the session
+ //on the other hand, if we are still waiting for
+ //the transaction to commit, leave it open
+ close();
+ }
+ PersistenceContexts.instance().untouch(componentName);
+ }
+
+ public void afterCompletion(int status)
+ {
+ synchronizationRegistered = false;
+ if ( !Contexts.isConversationContextActive() )
+ {
+ //in calls to MDBs and remote calls to SBs, the
+ //transaction doesn't commit until after contexts
+ //are destroyed, so wait until the transaction
+ //completes before closing the session
+ //on the other hand, if we still have an active
+ //conversation context, leave it open
+ close();
+ }
+ }
+
+ public void beforeCompletion() {}
+
+ private void close()
+ {
if ( log.isDebugEnabled() )
{
log.debug("destroying seam managed session for session factory: " + sessionFactoryJndiName);
@@ -138,8 +184,6 @@
{
session.close();
}
-
- PersistenceContexts.instance().untouch(componentName);
}
private SessionFactory getSessionFactoryFromJndiOrValueBinding()
1.28 +8 -9 jboss-seam/src/main/org/jboss/seam/core/ManagedJbpmContext.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ManagedJbpmContext.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedJbpmContext.java,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -b -r1.27 -r1.28
--- ManagedJbpmContext.java 18 Jun 2007 01:21:06 -0000 1.27
+++ ManagedJbpmContext.java 18 Jun 2007 03:21:33 -0000 1.28
@@ -77,7 +77,6 @@
if ( !synchronizationRegistered && !Lifecycle.isDestroying() && Transaction.instance().isActive() )
{
jbpmContext.getSession().getTransaction().registerSynchronization(this);
- //Transactions.registerSynchronization(this);
synchronizationRegistered = true;
}
return jbpmContext;
@@ -120,6 +119,8 @@
@Destroy
public void destroy()
{
+ if ( !synchronizationRegistered )
+ {
//in requests that come through SeamPhaseListener,
//there can be multiple transactions per request,
//but they are all completed by the time contexts
@@ -128,8 +129,6 @@
//the session
//on the other hand, if we are still waiting for
//the transaction to commit, leave it open
- if ( !synchronizationRegistered )
- {
closeContext();
}
}
1.45 +48 -7 jboss-seam/src/main/org/jboss/seam/core/ManagedPersistenceContext.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ManagedPersistenceContext.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/ManagedPersistenceContext.java,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -b -r1.44 -r1.45
--- ManagedPersistenceContext.java 11 Jun 2007 05:30:27 -0000 1.44
+++ ManagedPersistenceContext.java 18 Jun 2007 03:21:33 -0000 1.45
@@ -1,4 +1,4 @@
-//$Id: ManagedPersistenceContext.java,v 1.44 2007/06/11 05:30:27 gavin Exp $
+//$Id: ManagedPersistenceContext.java,v 1.45 2007/06/18 03:21:33 gavin Exp $
package org.jboss.seam.core;
import static org.jboss.seam.InterceptionType.NEVER;
@@ -12,6 +12,7 @@
import javax.persistence.EntityManagerFactory;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
+import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import org.jboss.seam.Component;
@@ -44,7 +45,7 @@
@Intercept(NEVER)
@Install(false)
public class ManagedPersistenceContext
- implements Serializable, HttpSessionActivationListener, Mutable, PersistenceContextManager
+ implements Serializable, HttpSessionActivationListener, Mutable, PersistenceContextManager, Synchronization
{
private static final long serialVersionUID = -4972387440275848126L;
private static final LogProvider log = Logging.getLogProvider(ManagedPersistenceContext.class);
@@ -55,6 +56,8 @@
private ValueExpression<EntityManagerFactory> entityManagerFactory;
private List<Filter> filters = new ArrayList<Filter>(0);
+ private transient boolean synchronizationRegistered;
+
public boolean clearDirty()
{
return true;
@@ -105,8 +108,14 @@
if (entityManager==null) initEntityManager();
//join the transaction
- if ( !Lifecycle.isDestroying() && Transaction.instance().isActive() )
+ if ( !synchronizationRegistered && !Lifecycle.isDestroying() && Transaction.instance().isActive() )
+ {
+ LocalTransactionListener transactionListener = TransactionListener.instance();
+ if (transactionListener!=null)
{
+ transactionListener.registerSynchronization(this);
+ synchronizationRegistered = true;
+ }
entityManager.joinTransaction();
}
@@ -141,6 +150,40 @@
@Destroy
public void destroy()
{
+ if ( !synchronizationRegistered )
+ {
+ //in requests that come through SeamPhaseListener,
+ //there can be multiple transactions per request,
+ //but they are all completed by the time contexts
+ //are destroyed
+ //so wait until the end of the request to close
+ //the session
+ //on the other hand, if we are still waiting for
+ //the transaction to commit, leave it open
+ close();
+ }
+ PersistenceContexts.instance().untouch(componentName);
+ }
+
+ public void afterCompletion(int status)
+ {
+ synchronizationRegistered = false;
+ if ( !Contexts.isConversationContextActive() )
+ {
+ //in calls to MDBs and remote calls to SBs, the
+ //transaction doesn't commit until after contexts
+ //are destroyed, so wait until the transaction
+ //completes before closing the session
+ //on the other hand, if we still have an active
+ //conversation context, leave it open
+ close();
+ }
+ }
+
+ public void beforeCompletion() {}
+
+ private void close()
+ {
if ( log.isDebugEnabled() )
{
log.debug("destroying seam managed persistence context for persistence unit: " + persistenceUnitJndiName);
@@ -150,8 +193,6 @@
{
entityManager.close();
}
-
- PersistenceContexts.instance().untouch(componentName);
}
public EntityManagerFactory getEntityManagerFactoryFromJndiOrValueBinding()
@@ -205,7 +246,8 @@
this.persistenceUnitJndiName = persistenceUnitName;
}
- public String getComponentName() {
+ public String getComponentName()
+ {
return componentName;
}
@@ -245,7 +287,6 @@
break;
}
}
-
@Override
public String toString()
{
1.7 +46 -9 jboss-seam/src/main/org/jboss/seam/core/TransactionListener.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: TransactionListener.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/TransactionListener.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- TransactionListener.java 18 Jun 2007 01:43:52 -0000 1.6
+++ TransactionListener.java 18 Jun 2007 03:21:33 -0000 1.7
@@ -2,6 +2,7 @@
import static org.jboss.seam.annotations.Install.BUILT_IN;
+import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
@@ -10,14 +11,18 @@
import javax.ejb.Remove;
import javax.ejb.SessionSynchronization;
import javax.ejb.Stateful;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.log.Log;
/**
* Temporary solution for getting JTA transaction lifecycle
@@ -33,7 +38,9 @@
@Install(value=false, precedence=BUILT_IN)
public class TransactionListener implements LocalTransactionListener, SessionSynchronization
{
- static class Event
+ private static @Logger Log log;
+
+ static class Event implements Serializable
{
private String type;
private Object[] parameters;
@@ -50,6 +57,7 @@
}
private List<Event> events = new ArrayList<Event>();
+ private List<Synchronization> synchronizations = new ArrayList<Synchronization>();
public static LocalTransactionListener instance()
{
@@ -60,28 +68,46 @@
return (LocalTransactionListener) Component.getInstance(TransactionListener.class, ScopeType.EVENT);
}
- public void afterBegin() throws EJBException, RemoteException
- {
- Events.instance().raiseEvent("org.jboss.seam.afterTransactionBegin");
- }
+ public void afterBegin() throws EJBException, RemoteException {}
public void scheduleEvent(String type, Object... parameters)
{
events.add( new Event(type, parameters) );
}
+ public void registerSynchronization(Synchronization sync)
+ {
+ synchronizations.add(sync);
+ }
+
public void afterCompletion(boolean success) throws EJBException, RemoteException
{
Events.instance().raiseEvent("org.jboss.seam.afterTransactionCompletion", success);
+ for (Synchronization sync: synchronizations)
+ {
try
{
- if (success)
+ sync.afterCompletion(success ? Status.STATUS_COMMITTED : Status.STATUS_ROLLEDBACK);
+ }
+ catch (Exception e)
{
- for (Event event: events) event.call();
+ log.error("Exception processing transaction Synchronization after completion", e);
}
}
- finally
+ synchronizations.clear();
+ if (success)
+ {
+ for (Event event: events)
+ {
+ try
+ {
+ event.call();
+ }
+ catch (Exception e)
{
+ log.error("Exception processing transaction success event", e);
+ }
+ }
events.clear();
}
}
@@ -89,6 +115,17 @@
public void beforeCompletion() throws EJBException, RemoteException
{
Events.instance().raiseEvent("org.jboss.seam.beforeTransactionCompletion");
+ for (Synchronization sync: synchronizations)
+ {
+ try
+ {
+ sync.beforeCompletion();
+ }
+ catch (Exception e)
+ {
+ log.error("Exception processing transaction Synchronization before completion", e);
+ }
+ }
}
@Remove @Destroy
More information about the jboss-cvs-commits
mailing list