2.  Integrating with the Transaction Manager

OpenJPA EntityManagers have the ability to automatically synchronize their transactions with an external transaction manager. Whether or not EntityManagers from a given EntityManagerFactory exhibit this behavior by default depends on the transaction type you set for the factory's persistence unit in your persistence.xml file. OpenJPA uses the given transaction type internally to set the openjpa.TransactionMode configuration property. This property accepts the following modes:

You can override the global transaction mode setting when you obtain an EntityManager using the EntityManagerFactory's createEntityManager(Map props) method. Simply set the openjpa.TransactionMode key of the given Map to the desired value.


You can also override the openjpa.ConnectionUserName, openjpa.ConnectionPassword, and openjpa.ConnectionRetainMode settings using the given Map.

In order to use global transactions, OpenJPA must be able to access the application server's javax.transaction.TransactionManager. OpenJPA can automatically discover the transaction manager for most major application servers. Occasionally, however, you might have to point OpenJPA to the transaction manager for an unrecognized or non-standard application server setup. This is accomplished through the openjpa.ManagedRuntime configuration property. This property describes an org.apache.openjpa.ee.ManagedRuntime implementation to use for transaction manager discovery. You can specify your own implementation, or use one of the built-ins:

See the Javadoc for of each class for details on the bean properties you can pass to these plugins in your configuration string.

Example 8.1. Configuring Transaction Manager Integration

<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ManagedRuntime" value="jndi(TransactionManagerName=java:/TransactionManager)"/>