2. Integrating with the Transaction Manager
Prev | Chapter 8. Deployment | Next

## 2. Integrating with the Transaction Manager

OpenJPA `EntityManager`s have the ability to automatically synchronize their transactions with an external transaction manager. Whether or not `EntityManager`s 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:

- `local`: Perform transaction operations locally.

- `managed`: Integrate with the application server's managed global transactions.

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.

### Note

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:

- `auto`: This is the default. It is an alias for the `org.apache.openjpa.ee.AutomaticManagedRuntime` class. This managed runtime is able to automatically integrate with several common application servers.

- `invocation`: An alias for the `org.apache.openjpa.ee.InvocationManagedRuntime` class. You can configure this runtime to invoke any static method in order to obtain the appserver's transaction manager.

- `jndi`: An alias for the `org.apache.openjpa.ee.JNDIManagedRuntime` class. You can configure this runtime to look up the transaction manager at any JNDI location.

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)"/>

Prev | Up | Next
Chapter 8. Deployment | Home | 3. XA Transactions