0 Replies Latest reply on Dec 13, 2007 4:13 PM by Ben Coughenour

    Retrieve the current transaction id in an session bean

    Ben Coughenour Newbie

      Part of my application's requirements involve me saving the transaction id of some operations. I am using a CMT StatelessSessionBean. I thought that I could get the id via entityManager.getTransaction(), but that yields the following IllegalStateException. Is there another way to get to this information without switching to user managed transactions?

      java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
       at org.jboss.ejb3.entity.TransactionScopedEntityManager.getTransaction(TransactionScopedEntityManager.java:226)
       at com.noverant.service.audit.StatelessAuditService.addEntry(StatelessAuditService.java:69)
       at sun.reflect.GeneratedMethodAccessor183.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
      ...
      


      Relevant portions of the class that is generating this error:
      package com.noverant.service.audit;
      
      import ...
      
      @Stateless (name="AuditService")
      public class StatelessAuditService implements AuditService
      {
       protected EntityManager entityManager = null;
       protected SessionContext sessionContext = null;
      
       @PersistenceContext(unitName = "nets-system")
       public void setEntityManager(EntityManager entityManager)
       {
       this.entityManager = entityManager;
       }
      
       @Resource
       public void setSessionContext(SessionContext sessionContext)
       {
       this.sessionContext = sessionContext;
       }
      
       public void addEntry(AuditEntry ae)
       {
       String transactionId = entityManager.getTransaction().toString();
       }
      
      }