[jboss-cvs] jboss-seam/src/main/org/jboss/seam/transaction ...
Gavin King
gavin.king at jboss.com
Sat Jun 23 03:11:45 EDT 2007
User: gavin
Date: 07/06/23 03:11:45
Modified: src/main/org/jboss/seam/transaction
EntityTransaction.java HibernateTransaction.java
Transaction.java
Removed: src/main/org/jboss/seam/transaction ETTransaction.java
HTransaction.java
Log:
try again, now that I actually have time to think about it
Revision Changes Path
1.2 +128 -15 jboss-seam/src/main/org/jboss/seam/transaction/EntityTransaction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: EntityTransaction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/transaction/EntityTransaction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- EntityTransaction.java 22 Jun 2007 20:53:42 -0000 1.1
+++ EntityTransaction.java 23 Jun 2007 07:11:45 -0000 1.2
@@ -2,50 +2,163 @@
import static org.jboss.seam.annotations.Install.FRAMEWORK;
-import javax.naming.NamingException;
import javax.persistence.EntityManager;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.core.Expressions.ValueExpression;
/**
- * Support for JPA EntityTransaction API
+ * Support for the JPA EntityTransaction API.
+ *
+ * Adapts JPA transaction management to a
+ * UserTransaction interface.
*
* @author Gavin King
*
*/
@Name("org.jboss.seam.transaction.transaction")
- at Scope(ScopeType.STATELESS)
+ at Scope(ScopeType.EVENT)
@Install(value=false, precedence=FRAMEWORK)
@BypassInterceptors
-public class EntityTransaction extends Transaction
+public class EntityTransaction extends UserTransaction
{
private ValueExpression<EntityManager> entityManager;
+ private EntityManager currentEntityManager;
+
+ private javax.persistence.EntityTransaction getDelegate()
+ {
+ if (currentEntityManager==null)
+ {
+ //should never occur
+ throw new IllegalStateException("session is null");
+ }
+ return currentEntityManager.getTransaction();
+ }
+
+ private void initEntityManager()
+ {
+ currentEntityManager = entityManager.getValue();
+ if (currentEntityManager==null)
+ {
+ throw new IllegalStateException("session was null: " + entityManager.getExpressionString());
+ }
+ }
+
+ public void begin() throws NotSupportedException, SystemException
+ {
+ //TODO: translate exceptions that occur into the correct JTA exception
+ assertNotActive();
+ initEntityManager();
+ try
+ {
+ getDelegate().begin();
+ }
+ catch (RuntimeException re)
+ {
+ clearEntityManager();
+ throw re;
+ }
+ }
+
+ public void commit() throws RollbackException, HeuristicMixedException,
+ HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
+ {
+ assertActive();
+ try
+ {
+ javax.persistence.EntityTransaction delegate = getDelegate();
+ if ( delegate.getRollbackOnly() )
+ {
+ delegate.rollback();
+ throw new RollbackException();
+ }
+ else
+ {
+ delegate.commit();
+ }
+ }
+ finally
+ {
+ clearEntityManager();
+ }
+ }
- @Unwrap
- @Override
- public UserTransaction getTransaction() throws NamingException
+ public int getStatus() throws SystemException
+ {
+ if ( isEntityManagerSet() && getDelegate().getRollbackOnly() )
{
- EntityManager em = entityManager.getValue();
- if ( em==null )
+ return Status.STATUS_MARKED_ROLLBACK;
+ }
+ else if ( isEntityManagerSet() && getDelegate().isActive() )
{
- return createNoTransaction();
+ return Status.STATUS_ACTIVE;
}
else
{
- return createETTransaction(em);
+ return Status.STATUS_NO_TRANSACTION;
}
}
- protected UserTransaction createETTransaction(EntityManager em)
+ public void rollback() throws IllegalStateException, SecurityException, SystemException
+ {
+ //TODO: translate exceptions that occur into the correct JTA exception
+ assertActive();
+ try
{
- return new ETTransaction( em.getTransaction() );
+ getDelegate().rollback();
+ }
+ finally
+ {
+ clearEntityManager();
+ }
+ }
+
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ assertActive();
+ getDelegate().setRollbackOnly();
+ }
+
+ public void setTransactionTimeout(int timeout) throws SystemException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ private boolean isEntityManagerSet()
+ {
+ return currentEntityManager!=null;
+ }
+
+ private void clearEntityManager()
+ {
+ currentEntityManager = null;
+ }
+
+ private void assertActive()
+ {
+ if ( !isEntityManagerSet() )
+ {
+ throw new IllegalStateException("transaction is not active");
+ }
+ }
+
+ private void assertNotActive() throws NotSupportedException
+ {
+ if ( isEntityManagerSet() )
+ {
+ throw new NotSupportedException("transaction is already active");
+ }
}
public ValueExpression<EntityManager> getEntityManager()
@@ -53,7 +166,7 @@
return entityManager;
}
- public void setEntityManager(ValueExpression<EntityManager> entityManager)
+ public void setSession(ValueExpression<EntityManager> entityManager)
{
this.entityManager = entityManager;
}
1.2 +128 -14 jboss-seam/src/main/org/jboss/seam/transaction/HibernateTransaction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: HibernateTransaction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/transaction/HibernateTransaction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- HibernateTransaction.java 22 Jun 2007 20:53:42 -0000 1.1
+++ HibernateTransaction.java 23 Jun 2007 07:11:45 -0000 1.2
@@ -2,50 +2,164 @@
import static org.jboss.seam.annotations.Install.FRAMEWORK;
-import javax.naming.NamingException;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
import org.hibernate.Session;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.core.Expressions.ValueExpression;
/**
- * Support for Hibernate Transaction API
+ * Support for the Hibernate Transaction API.
+ *
+ * Adapts Hibernate transaction management to a
+ * UserTransaction interface.
*
* @author Gavin King
*
*/
@Name("org.jboss.seam.transaction.transaction")
- at Scope(ScopeType.STATELESS)
+ at Scope(ScopeType.EVENT)
@Install(value=false, precedence=FRAMEWORK)
@BypassInterceptors
-public class HibernateTransaction extends Transaction
+public class HibernateTransaction extends UserTransaction
{
private ValueExpression<Session> session;
+ private Session currentSession;
+ private boolean rollbackOnly; //Hibernate Transaction doesn't have a "rollback only" state
+
+ private org.hibernate.Transaction getDelegate()
+ {
+ if (currentSession==null)
+ {
+ //should never occur
+ throw new IllegalStateException("session is null");
+ }
+ return currentSession.getTransaction();
+ }
+
+ private void initSession()
+ {
+ currentSession = session.getValue();
+ if (currentSession==null)
+ {
+ throw new IllegalStateException("session was null: " + session.getExpressionString());
+ }
+ }
+
+ public void begin() throws NotSupportedException, SystemException
+ {
+ assertNotActive();
+ initSession();
+ try
+ {
+ getDelegate().begin();
+ }
+ catch (RuntimeException re)
+ {
+ clearSession();
+ throw re;
+ }
+ }
+
+ public void commit() throws RollbackException, HeuristicMixedException,
+ HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
+ {
+ assertActive();
+ try
+ {
+ if (rollbackOnly)
+ {
+ getDelegate().rollback();
+ throw new RollbackException();
+ }
+ else
+ {
+ getDelegate().commit();
+ }
+ }
+ finally
+ {
+ clearSession();
+ }
+ }
- @Unwrap
- @Override
- public UserTransaction getTransaction() throws NamingException
+ public int getStatus() throws SystemException
+ {
+ if (rollbackOnly)
{
- Session s = session.getValue();
- if ( s==null )
+ return Status.STATUS_MARKED_ROLLBACK;
+ }
+ else if ( isSessionSet() && getDelegate().isActive() )
{
- return createNoTransaction();
+ return Status.STATUS_ACTIVE;
}
else
{
- return createHTransaction(s);
+ return Status.STATUS_NO_TRANSACTION;
}
}
- protected UserTransaction createHTransaction(Session session)
+ public void rollback() throws IllegalStateException, SecurityException, SystemException
+ {
+ //TODO: translate exceptions that occur into the correct JTA exception
+ assertActive();
+ try
{
- return new HTransaction( session.getTransaction() );
+ getDelegate().rollback();
+ }
+ finally
+ {
+ clearSession();
+ }
+ }
+
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ assertActive();
+ rollbackOnly = true;
+ }
+
+ public void setTransactionTimeout(int timeout) throws SystemException
+ {
+ assertActive();
+ getDelegate().setTimeout(timeout);
+ }
+
+ private boolean isSessionSet()
+ {
+ return currentSession!=null;
+ }
+
+ private void clearSession()
+ {
+ currentSession = null;
+ }
+
+ private void assertActive()
+ {
+ if ( !isSessionSet() )
+ {
+ throw new IllegalStateException("transaction is not active");
+ }
+ }
+
+ private void assertNotActive() throws NotSupportedException
+ {
+ //TODO: translate exceptions that occur into the correct JTA exception
+ if ( isSessionSet() )
+ {
+ throw new NotSupportedException("transaction is already active");
+ }
}
public ValueExpression<Session> getSession()
1.9 +2 -2 jboss-seam/src/main/org/jboss/seam/transaction/Transaction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Transaction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/transaction/Transaction.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- Transaction.java 21 Jun 2007 04:36:14 -0000 1.8
+++ Transaction.java 23 Jun 2007 07:11:45 -0000 1.9
@@ -33,7 +33,7 @@
*
*/
@Name("org.jboss.seam.transaction.transaction")
- at Scope(ScopeType.STATELESS)
+ at Scope(ScopeType.EVENT)
@Install(precedence=BUILT_IN)
@BypassInterceptors
public class Transaction
@@ -55,7 +55,7 @@
public static UserTransaction instance()
{
- return (UserTransaction) Component.getInstance(Transaction.class, ScopeType.STATELESS);
+ return (UserTransaction) Component.getInstance(Transaction.class, ScopeType.EVENT);
}
@Unwrap
More information about the jboss-cvs-commits
mailing list