This question actually includes many concepts, and I will not describe them in detail. For more detailed details, I suggest reading entire chapters 5 - Entity Managers and Sustainability Contexts of the JPA 1.0 specification (and other relevant sections mentioned at the end of this answer). I would rather try to describe the most common scenario in the Java EE environment.
In an EJB environment, the following are commonly used:
- Container managed object manager (must be a JTA entity manager)
- non-Beans scope transaction (SLSB)
- extended in Beans state session (SFSB)
- JTA transaction management (not local-local transactions, if that's really what you want)
Here's how to configure persistence.xml for a JTA entity manager that uses a data source named JNDI java:comp/env/jdbc/AppDataSource , OpenJPA is the persistence provider used by WebSphere:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="MyPu" transaction-type="JTA"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <jta-data-source>java:comp/env/jdbc/AppDataSource</jta-data-source> <class>com.acme.domain.Foo</class> <class>com.acme.domain.Bar</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="openjpa.TransactionMode" value="managed"/> <property name="openjpa.ConnectionFactoryMode" value="managed"/> <property name="openjpa.jdbc.DBDictionary" value="db2"/> </properties> </persistence-unit> </persistence>
For OpenJPA properties, see the OpenJPA documentation.
And this is how SLSB (using container-managed transactions) can get the container-managed Entity Manager:
@Stateless public class EmployeeServiceBean implements EmployeeService { @PersistenceContext(unitName="MyPu") private EntityManager em; public Employee createEmployee(int id, String name, long salary) { Employee emp = new Employee(id); emp.setName(name); emp.setSalary(salary); em.persist(emp); return emp; } ... }
And it's all. The life cycle of the entity manager is managed transparently by the container for the application (no createEM / close), and the object manager is involved in the container-managed JTA transaction (without an explicit start / commit).
As I previously hinted, I'm just scratching the surface, my goal is to somehow put you on the right track. To move on, I suggest taking a book (for example, EJB3 in action). Meanwhile, the links below will be a good read.
References
- JPA 1.0 Specification
- Section 5.2.1 "Obtaining Entity Manager in the Java EE Environment"
- Section 5.5 "Transaction Management"
- Section 5.6 "Container-Managed Retention Contexts"
- Section 6.2.1.2 "transaction type"
- Section 6.2.1.4 "Provider"
- Section 6.2.1.5 "jta-data-source, non-jta-data-source"
Resources
source share