6 Replies Latest reply on Sep 2, 2010 4:40 PM by rszulgo

    No active transaction

    areian

      Hi.

      I'm trying to deploy an EJB/SessionBean package to my JBoss server, but for some reason it gives me a TransactionRequiredException, with the message that it didn't find any active transactions.
      I was under the impression that in a container managed environment like JBoss, the container would automatically create a new transaction for me, if it doesn't have any at hand.
      I have probably missed something, either in my server configuration or my code, but after 3 hours of searching the web I'm no closer to a solution.
      Any pointers as to what I might be missing would be greatly appreciated.
      If you need to see any of the config data or source snippets, let me know.

        • 1. Re: No active transaction
          peterj

          EJB 2.x or 3.0?

          What transaction attribute are you using? Hopefully not "mandatory".

          • 2. Re: No active transaction
            areian

            It's EJB 3.0, and I'm using whichever is the default transaction attribute (Do you have to specify the attribute explicitly?)

            Also, I don't know if there is any point in mentioning it, but it is running on an OS X box.

            • 3. Re: No active transaction
              peterj

              The default transaction attribute is "required", which will create a new transaction if there is not one.

              Could you post the full exception stack trace? Also, post the source code for the EJB that is showing this problem.

              Enclose the source code in UBBCode "code" tags - you can do this by selecting the source and clicking the Code button above the editor window. Also, click the Preview button to ensure that the formatting is correct before posting.

              • 4. Re: No active transaction
                areian

                Hey again.
                I Found a solution: It works if I manually inject an EntityManager using the EntityManagerFactory. I was just under the impression that it was supposed to do that automatically in a container mannaged environment.

                The revised code for the session bean follows:


                package sm.comm.frontend2.beans.sessions;
                
                import javax.ejb.Stateless;
                import javax.persistence.EntityManager;
                import javax.persistence.EntityManagerFactory;
                import javax.persistence.PersistenceContext;
                import javax.persistence.PersistenceUnit;
                import sm.beans.entities.Employee;
                import sm.comm.frontend2.dtos.assemblers.EmployeeAssembler;
                import sm.comm.frontend2.dtos.assemblers.SkillAssembler;
                import sm.comm.frontend2.dtos.DetailEmployeeDTO;
                import sm.comm.frontend2.dtos.SkillDTO;
                
                @Stateless
                public class F2FacadeBean implements F2FacadeRemote {
                 @PersistenceUnit
                 private EntityManagerFactory em;
                
                 public DetailEmployeeDTO EmployeeDTO(int id) {
                 Employee emp = em.createEntityManager().find(Employee.class, id);
                 return EmployeeAssembler.createDetailEmployeeDTO(emp);
                 }
                
                 public void persistSkill(SkillDTO skill) {
                 System.out.println(skill.getName());
                 System.out.println(em.createEntityManager().isOpen());
                 em.createEntityManager().persist(SkillAssembler.createSkill(skill));
                 }
                
                 public String hello() {
                 return "Hello World!";
                 }
                
                }
                


                It contains some debugging code, but the idea should be clear.

                I just got brain wave. Am I right in thinking that if I want to directly inject the EntityManager I should use @PersistenceContext, and if I use @PersistenceUnit I need to use the factory?


                Thanks a lot for the time taken to help out with this


                • 5. Re: No active transaction
                  peterj

                  I always use:

                  @PersistenceContext
                  private EntityManager em;


                  Then I do not have to call createEntityManager() within my code{

                  public void saveIt(SomeEntity se) {
                   em.persist(se);
                  }


                  • 6. Re: No active transaction
                    rszulgo

                    Why not mandatory ? Are there some issues related to this transaction type?