-
1. Re: No active transaction
peterj Oct 28, 2008 11:37 AM (in response to areian)EJB 2.x or 3.0?
What transaction attribute are you using? Hopefully not "mandatory". -
2. Re: No active transaction
areian Oct 28, 2008 12:42 PM (in response to 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 Oct 28, 2008 4:10 PM (in response to areian)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 Oct 28, 2008 6:02 PM (in response to 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 Oct 28, 2008 6:38 PM (in response to areian)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 Sep 2, 2010 4:40 PM (in response to peterj)Why not mandatory ? Are there some issues related to this transaction type?