IBM Websphere JPA configuration - how to update persistence.xml

I am new to EJB 3 and JPA.

I created a data source on the jdbc/AppDataSource application server. The default save provider remains as com.ibm.websphere.persistence.PersistenceProviderImpl . And I left the default jta data source JNDI name as AppDataSource . I am really confused regarding JTA and non-JTA. What makes them different?

I generated entities and created an EntityTransaction object in a bean. After calling persist() and commit() methods, I get an error message:

 javax.ejb.EJBException: See nested exception; nested exception is: <openjpa-1.2.1-SNAPSHOT-r422266:686069 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property. Caused by: <openjpa-1.2.1-SNAPSHOT-r422266:686069 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property. 

How does openjpa open here?

At the moment, my persistence.xml contains only entity class names.

How can I make it use the default application server values ​​for the data source. Or how can I provide the jpa provider information and data source data in persistence.xml ?

Please enter your details.

+4
source share
1 answer

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> <!-- OpenJPA specific 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

+6
source

All Articles