<commit_before>---
title: JTA Global Transactions with Geode
---

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->


Use JTA global transactions to coordinate Geode cache transactions and JDBC transactions.

JTA is a standard Java interface you can use to coordinate Geode cache transactions and JDBC transactions globally under one umbrella. JTA provides direct coordination between the Geode cache and another transactional resource, such as a database. The parties involved in a JTA transaction include:

-   The Java application, responsible for starting the global transaction
-   The JTA transaction manager, responsible for opening, committing, and rolling back transactions
-   The transaction resource managers, including the Geode cache transaction manager and the JDBC resource manager, responsible for managing operations in the cache and database respectively

Using JTA, your application controls all transactions in the same standard way, whether the transactions act on the Geode cache, a JDBC resource, or both together. When a JTA global transaction is done, the Geode transaction and the database transaction are both complete.

When using JTA global transactions with Geode, you have three options:

1.  Coordinate with an external JTA transaction manager in a container (such as WebLogic or JBoss)
2.  Set Geode as the “last resource” while using a container (such as WebLogic or JBoss) as the JTA transaction manager
3.  Have Geode act as the JTA transaction manager

An application creates a global transaction by using `javax.transaction.UserTransaction` bound to the JNDI context `java:/UserTransaction` to start and terminate transactions. During the transaction, cache operations are done through Geode as usual as described in [Geode Cache Transactions](cache_transactions.html#topic_e15_mr3_5k).

**Note:**
See the Sun documentation for more information on topics such as JTA, `javax.transaction`, committing and rolling back global transactions, and the related exceptions.

-   **[Coordinating with External JTA Transactions Managers](#concept_cp1_zx1_wk)**

    Geode can work with the JTA transaction managers of several containers like JBoss, WebLogic, GlassFish, and so on.

-   **[Using Geode as the "Last Resource" in a Container-Managed JTA Transaction](#concept_csy_vfb_wk)**

    The "last resource" feature in certain 3rd party containers such as WebLogic allow the use one non-XAResource (such as Geode) in a transaction with multiple XAResources while ensuring consistency.

-   **[Using Geode as the JTA Transaction Manager](#concept_8567sdkbigige)**

    You can also use Geode as the JTA transaction manager.

-   **[Behavior of Geode Cache Writers and Loaders Under JTA](cache_plugins_with_jta.html)**

    When Geode participates in a global transactions, you can still have Geode cache writers and cache loaders operating in the usual way.

-   **[Turning Off JTA Transactions](turning_off_jta.html)**

    You can configure regions to not participate in any JTA global transaction.

<a id="concept_cp1_zx1_wk"></a>

# Coordinating with External JTA Transactions Managers

Geode can work with the JTA transaction managers of several containers like JBoss, WebLogic, GlassFish, and so on.

At startup Geode looks for a TransactionManager (`javax.transaction.TransactionManager`) that has been bound to its JNDI context. When Geode finds such an external transaction manager, all Geode region operations (such as get and put) will participate in global transactions hosted by this external JTA transaction manager.

This figure shows the high-level operation of a JTA global transaction whose resources include a Geode cache and a database.

<img src="../../images/transactions_jta_app_server.png" id="concept_cp1_zx1_wk__image_C2935E48415349659FC39BF5C7E75579" class="image" />

An externally coordinated JTA global transaction is run in the following manner:

1.  Each region operation looks up for presence of a global transaction. If one is detected, then a Geode transaction is started automatically, and we register a `javax.transaction.Synchronization` callback with the external JTA transaction manager.
2.  At transaction commit, Geode gets a `beforeCommit()` callback from the external JTA transaction manager. Geode does all locking and conflict detection at this time. If this fails, an exception is thrown back to JTA transaction manager, which then aborts the transaction.
3.  After a successful `beforeCommit()`callback, JTA transaction manager asks other data sources to commit their transaction.
4.  Geode then gets a `afterCommit()` callback in which changes are applied to the cache and distributed to other members.

You can disable JTA in any region that should not participate in JTA transactions. See [Turning Off JTA Transactions](turning_off_jta.html#concept_nw2_5gs_xk).

## <a id="task_j3g_3mn_1l" class="no-quick-link"></a>How to Run a JTA Transaction Coordinated by an External Transaction Manager

Use the following procedure to run a Geode global JTA transaction coordinated by an external JTA transaction manager.

1.  **Configure the external data sources in the external container.** Do not configure the data sources in cache.xml . They are not guaranteed to get bound to the JNDI tree.
2.  

    Configure Geode for any necessary transactional behavior in the `cache.xml` file. For example, enable `copy-on-read` and specify a transaction listener, as needed. See [Setting Global Copy on Read](working_with_transactions.html#concept_vx2_gs4_5k) and [Configuring Transaction Plug-In Event Handlers](working_with_transactions.html#concept_ocw_vf1_wk) for details. 
3.  

    Make sure that JTA transactions are enabled for the regions that will participate in the transaction. See [Turning Off JTA Transactions](turning_off_jta.html#concept_nw2_5gs_xk) for details. 
4.  

     Start the transaction through the external container. 
5.  

    Initialize the Geode cache. Geode will automatically join the transaction. 
6.  

     Execute operations in the cache and the database as usual. 
7.  

     Commit the transaction through the external container. 

<a id="concept_csy_vfb_wk"></a>

# Using Geode as the "Last Resource" in a Container-Managed JTA Transaction

The "last resource" feature in certain 3rd party containers such as WebLogic allow the use one non-XAResource (such as Geode) in a transaction with multiple XAResources while ensuring consistency.

In the previous two JTA transaction use cases, if the Geode member fails after the other data sources commit but before Geode receives the `afterCommit` callback, Geode and the other data sources may become inconsistent. To prevent this from occurring, you can use the container's "last resource optimization" feature, with Geode set as the "last resource". Using Geode as the last resource ensures that in the event of failure, Geode remains consistent with the other XAResources involved in the transaction.

To accomplish this, the application server container must use a JCA Resource Adapter to accomodate Geode as the transaction's last resource. The transaction manager of the container first issues a "prepare" message to the participating XAResources. If the XAResources all accept the transaction, then the manager issues a "commit" instruction to the non-XAResource (in this case, Geode). The non-XAResource (in this case, Geode) participates as a local transaction resource. If the non-XAResource fails, then the transaction manager can rollback the XAResources.

<img src="../../images/transactions_jca_adapter.png" id="concept_csy_vfb_wk__image_opb_sgb_wk" class="image" />

<a id="task_sln_x3b_wk"></a>

# How to Run JTA Transactions with Geode as a "Last Resource"

1.  Locate the `$GEMFIRE/lib/gemfire-jca.rar` file in your Geode installation. 
2.  Add your container-specific XML file to the `gemfire-jca.rar` file. 
<ol>
<li>Create a container-specific resource adapter XML file named &lt;container&gt;-ra.xml. For example, an XML file for a WebLogic resource adapter XML file might look something like this:

    ``` pre
    <?xml version="1.0"?>
    <!DOCTYPE weblogic-connection-factory-dd PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 9.0.0 Connector//EN' 
    'http://www.bea.com/servers/wls810/dtd/weblogic810-ra.dtd'>

    <weblogic-connection-factory-dd>
       <connection-factory-name>GFE JCA</connection-factory-name>
       <jndi-name>gfe/jca</jndi-name>
    </weblogic-connection-factory-dd>
    ```
</li>
<li>Create a folder named `META-INF`, and place the container-specific XML file inside the directory. For example, the folder structure would look like this:

    ``` pre
    META-INF/weblogic-ra.xml
    ```
</li>
<li>Navigate to the directory above the `META-INF` folder and execute the following command:

    ``` pre
    $ jar -uf <GEMFIRE_INSTALL_DIR>/lib/gemfire-jca.rar META-INF/weblogic-ra.xml
    ```
</li>
</ol>
3.  Make sure that `$GEMFIRE/lib/gemfire.jar` is accessible in the CLASSPATH of the JTA transaction coordinator container.
4.  Deploy `gemfire-jca.rar` file on the JTA transaction coordinator container . When deploying the file, you specify the JNDI name and so on. 
5.  Configure Geode for any necessary transactional behavior. Enable `copy-on-read` and specify a transaction listener, if you need one. See [Setting Global Copy on Read](working_with_transactions.html#concept_vx2_gs4_5k) and [Configuring Transaction Plug-In Event Handlers](working_with_transactions.html#concept_ocw_vf1_wk) for details.
6.  Get an initial context through `com.gemstone.cache.Cache.getJNDIContext`. For example:

    ``` pre
    Context ctx = cache.getJNDIContext();
    ```

    This returns `javax.naming.Context` and gives you the JNDI associated with the cache. The context contains the `TransactionManager`, `UserTransaction`, and any configured JDBC resource manager.

7.  Start and commit the global transaction using the `UserTransaction` object rather than with Geode's `CacheTransactionManager`. 

    ``` pre
    UserTransaction txManager = (UserTransaction)ctx.lookup("java:/UserTransaction");
    ```

8.  Obtain a Geode connection.

    ``` pre
    GFConnectionFactory cf = (GFConnectionFactory) ctx.lookup("gfe/jca");

    //This step of obtaining connection is what begins the
    //LocalTransaction.
    //If this is absent, GFE operations will not be part of any
    //transaction
    GFConnection gemfireConn = (GFConnection)cf.getConnection();
    ```

See [JCA Resource Adapter Example](jca_adapter_example.html#concept_swv_z2p_wk) for an example of how to set up a transaction using the JCA Resource Adapter.

## <a id="concept_8567sdkbigige" class="no-quick-link"></a>Using Geode as the JTA Transaction Manager

You can also use Geode as the JTA transaction manager.

Geode ships with its own implementation of a JTA transaction manager. However, note that this implementation is not XA-compliant; therefore, it does not persist any state, which could lead to an inconsistent state after recovering a crashed member.

<img src="../../images/transactions_jta.png" id="concept_8567sdkbigige__image_C8D94070E55F4BCC8B5FF3D5BEBA99ED" class="image" />

The Geode JTA transaction manager is initialized when the Geode cache is initialized. Until then, JTA is not available for use. The application starts a JTA transaction by using the `UserTransaction.begin` method. The `UserTransaction` object is the application’s handle to instruct the JTA transaction manager on what to do.

The Geode JTA implementation also supports the J2EE Connector Architecture (JCA) `ManagedConnectionFactory`.

The Geode implementation of JTA has the following limitations:
<commit_msg>GEODE-2301 Doc note to deprecate Geode JTA trans mgr<commit_after>---
title: JTA Global Transactions with Geode
---

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->


Use JTA global transactions to coordinate Geode cache transactions and JDBC transactions.

JTA is a standard Java interface you can use to coordinate Geode cache transactions and JDBC transactions globally under one umbrella. JTA provides direct coordination between the Geode cache and another transactional resource, such as a database. The parties involved in a JTA transaction include:

-   The Java application, responsible for starting the global transaction
-   The JTA transaction manager, responsible for opening, committing, and rolling back transactions
-   The transaction resource managers, including the Geode cache transaction manager and the JDBC resource manager, responsible for managing operations in the cache and database respectively

Using JTA, your application controls all transactions in the same standard way, whether the transactions act on the Geode cache, a JDBC resource, or both together. When a JTA global transaction is done, the Geode transaction and the database transaction are both complete.

When using JTA global transactions with Geode, you have three options:

1.  Coordinate with an external JTA transaction manager in a container (such as WebLogic or JBoss)
2.  Set Geode as the “last resource” while using a container (such as WebLogic or JBoss) as the JTA transaction manager
3.  Have Geode act as the JTA transaction manager

An application creates a global transaction by using `javax.transaction.UserTransaction` bound to the JNDI context `java:/UserTransaction` to start and terminate transactions. During the transaction, cache operations are done through Geode as usual as described in [Geode Cache Transactions](cache_transactions.html#topic_e15_mr3_5k).

**Note:**
See the Sun documentation for more information on topics such as JTA, `javax.transaction`, committing and rolling back global transactions, and the related exceptions.

-   **[Coordinating with External JTA Transactions Managers](#concept_cp1_zx1_wk)**

    Geode can work with the JTA transaction managers of several containers like JBoss, WebLogic, GlassFish, and so on.

-   **[Using Geode as the "Last Resource" in a Container-Managed JTA Transaction](#concept_csy_vfb_wk)**

    The "last resource" feature in certain 3rd party containers such as WebLogic allow the use one non-XAResource (such as Geode) in a transaction with multiple XAResources while ensuring consistency.

-   **[Using Geode as the JTA Transaction Manager](#concept_8567sdkbigige)**

    You can also use Geode as the JTA transaction manager.

-   **[Behavior of Geode Cache Writers and Loaders Under JTA](cache_plugins_with_jta.html)**

    When Geode participates in a global transactions, you can still have Geode cache writers and cache loaders operating in the usual way.

-   **[Turning Off JTA Transactions](turning_off_jta.html)**

    You can configure regions to not participate in any JTA global transaction.

<a id="concept_cp1_zx1_wk"></a>

# Coordinating with External JTA Transactions Managers

Geode can work with the JTA transaction managers of several containers like JBoss, WebLogic, GlassFish, and so on.

At startup Geode looks for a TransactionManager (`javax.transaction.TransactionManager`) that has been bound to its JNDI context. When Geode finds such an external transaction manager, all Geode region operations (such as get and put) will participate in global transactions hosted by this external JTA transaction manager.

This figure shows the high-level operation of a JTA global transaction whose resources include a Geode cache and a database.

<img src="../../images/transactions_jta_app_server.png" id="concept_cp1_zx1_wk__image_C2935E48415349659FC39BF5C7E75579" class="image" />

An externally coordinated JTA global transaction is run in the following manner:

1.  Each region operation looks up for presence of a global transaction. If one is detected, then a Geode transaction is started automatically, and we register a `javax.transaction.Synchronization` callback with the external JTA transaction manager.
2.  At transaction commit, Geode gets a `beforeCommit()` callback from the external JTA transaction manager. Geode does all locking and conflict detection at this time. If this fails, an exception is thrown back to JTA transaction manager, which then aborts the transaction.
3.  After a successful `beforeCommit()`callback, JTA transaction manager asks other data sources to commit their transaction.
4.  Geode then gets a `afterCommit()` callback in which changes are applied to the cache and distributed to other members.

You can disable JTA in any region that should not participate in JTA transactions. See [Turning Off JTA Transactions](turning_off_jta.html#concept_nw2_5gs_xk).

## <a id="task_j3g_3mn_1l" class="no-quick-link"></a>How to Run a JTA Transaction Coordinated by an External Transaction Manager

Use the following procedure to run a Geode global JTA transaction coordinated by an external JTA transaction manager.

1.  **Configure the external data sources in the external container.** Do not configure the data sources in cache.xml . They are not guaranteed to get bound to the JNDI tree.
2.  

    Configure Geode for any necessary transactional behavior in the `cache.xml` file. For example, enable `copy-on-read` and specify a transaction listener, as needed. See [Setting Global Copy on Read](working_with_transactions.html#concept_vx2_gs4_5k) and [Configuring Transaction Plug-In Event Handlers](working_with_transactions.html#concept_ocw_vf1_wk) for details. 
3.  

    Make sure that JTA transactions are enabled for the regions that will participate in the transaction. See [Turning Off JTA Transactions](turning_off_jta.html#concept_nw2_5gs_xk) for details. 
4.  

     Start the transaction through the external container. 
5.  

    Initialize the Geode cache. Geode will automatically join the transaction. 
6.  

     Execute operations in the cache and the database as usual. 
7.  

     Commit the transaction through the external container. 

<a id="concept_csy_vfb_wk"></a>

# Using Geode as the "Last Resource" in a Container-Managed JTA Transaction

The "last resource" feature in certain 3rd party containers such as WebLogic allow the use one non-XAResource (such as Geode) in a transaction with multiple XAResources while ensuring consistency.

In the previous two JTA transaction use cases, if the Geode member fails after the other data sources commit but before Geode receives the `afterCommit` callback, Geode and the other data sources may become inconsistent. To prevent this from occurring, you can use the container's "last resource optimization" feature, with Geode set as the "last resource". Using Geode as the last resource ensures that in the event of failure, Geode remains consistent with the other XAResources involved in the transaction.

To accomplish this, the application server container must use a JCA Resource Adapter to accomodate Geode as the transaction's last resource. The transaction manager of the container first issues a "prepare" message to the participating XAResources. If the XAResources all accept the transaction, then the manager issues a "commit" instruction to the non-XAResource (in this case, Geode). The non-XAResource (in this case, Geode) participates as a local transaction resource. If the non-XAResource fails, then the transaction manager can rollback the XAResources.

<img src="../../images/transactions_jca_adapter.png" id="concept_csy_vfb_wk__image_opb_sgb_wk" class="image" />

<a id="task_sln_x3b_wk"></a>

# How to Run JTA Transactions with Geode as a "Last Resource"

1.  Locate the `$GEMFIRE/lib/gemfire-jca.rar` file in your Geode installation. 
2.  Add your container-specific XML file to the `gemfire-jca.rar` file. 
<ol>
<li>Create a container-specific resource adapter XML file named &lt;container&gt;-ra.xml. For example, an XML file for a WebLogic resource adapter XML file might look something like this:

    ``` pre
    <?xml version="1.0"?>
    <!DOCTYPE weblogic-connection-factory-dd PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 9.0.0 Connector//EN' 
    'http://www.bea.com/servers/wls810/dtd/weblogic810-ra.dtd'>

    <weblogic-connection-factory-dd>
       <connection-factory-name>GFE JCA</connection-factory-name>
       <jndi-name>gfe/jca</jndi-name>
    </weblogic-connection-factory-dd>
    ```
</li>
<li>Create a folder named `META-INF`, and place the container-specific XML file inside the directory. For example, the folder structure would look like this:

    ``` pre
    META-INF/weblogic-ra.xml
    ```
</li>
<li>Navigate to the directory above the `META-INF` folder and execute the following command:

    ``` pre
    $ jar -uf <GEMFIRE_INSTALL_DIR>/lib/gemfire-jca.rar META-INF/weblogic-ra.xml
    ```
</li>
</ol>
3.  Make sure that `$GEMFIRE/lib/gemfire.jar` is accessible in the CLASSPATH of the JTA transaction coordinator container.
4.  Deploy `gemfire-jca.rar` file on the JTA transaction coordinator container . When deploying the file, you specify the JNDI name and so on. 
5.  Configure Geode for any necessary transactional behavior. Enable `copy-on-read` and specify a transaction listener, if you need one. See [Setting Global Copy on Read](working_with_transactions.html#concept_vx2_gs4_5k) and [Configuring Transaction Plug-In Event Handlers](working_with_transactions.html#concept_ocw_vf1_wk) for details.
6.  Get an initial context through `com.gemstone.cache.Cache.getJNDIContext`. For example:

    ``` pre
    Context ctx = cache.getJNDIContext();
    ```

    This returns `javax.naming.Context` and gives you the JNDI associated with the cache. The context contains the `TransactionManager`, `UserTransaction`, and any configured JDBC resource manager.

7.  Start and commit the global transaction using the `UserTransaction` object rather than with Geode's `CacheTransactionManager`. 

    ``` pre
    UserTransaction txManager = (UserTransaction)ctx.lookup("java:/UserTransaction");
    ```

8.  Obtain a Geode connection.

    ``` pre
    GFConnectionFactory cf = (GFConnectionFactory) ctx.lookup("gfe/jca");

    //This step of obtaining connection is what begins the
    //LocalTransaction.
    //If this is absent, GFE operations will not be part of any
    //transaction
    GFConnection gemfireConn = (GFConnection)cf.getConnection();
    ```

See [JCA Resource Adapter Example](jca_adapter_example.html#concept_swv_z2p_wk) for an example of how to set up a transaction using the JCA Resource Adapter.

## <a id="concept_8567sdkbigige" class="no-quick-link"></a>Using Geode as the JTA Transaction Manager

You can also use Geode as the JTA transaction manager.
As of Geode 1.2, Geode's JTA transaction manager is deprecated.

Geode ships with its own implementation of a JTA transaction manager. However, note that this implementation is not XA-compliant; therefore, it does not persist any state, which could lead to an inconsistent state after recovering a crashed member.

<img src="../../images/transactions_jta.png" id="concept_8567sdkbigige__image_C8D94070E55F4BCC8B5FF3D5BEBA99ED" class="image" />

The Geode JTA transaction manager is initialized when the Geode cache is initialized. Until then, JTA is not available for use. The application starts a JTA transaction by using the `UserTransaction.begin` method. The `UserTransaction` object is the application’s handle to instruct the JTA transaction manager on what to do.

The Geode JTA implementation also supports the J2EE Connector Architecture (JCA) `ManagedConnectionFactory`.

The Geode implementation of JTA has the following limitations:

{'fraction_non_alphanumeric': 0.062271356783919596, 'fraction_numerical': 0.009728643216080402, 'mean_word_length': 4.64593735814798, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 2, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '14759', 'n_tokens_mistral': 6789, 'n_tokens_neox': 6494, 'n_words': 3130}