Sunday, October 20, 2013

How the JTA Transaction Manager helps to GemFire

 

JTA provides direct coordination between the GemFire cache and another transactional resource,

such as a database. Using JTA, your application controls all transactions in the same standard way, whether the transactions act on the GemFire cache, a JDBC resource, or both together.

By the time a JTA global transaction is done, the GemFire transaction and the database transaction are both complete.


imageContributed by Gemstone.

Using GemFire with JTA transactions requires these general steps.

During configuration:
1. Configure the JDBC data sources in the cache.xml file.
2. Include the jar file for any data sources in your CLASSPATH.


At run-time:
3. Initialize the GemFire cache.
4. Get the JNDI context and look up the data sources.
5. Start a JTA transaction.
6. Execute operations in the cache and database as usual.
7. Commit the transaction.


The transactional view exists until the transaction completes. If the commit fails or the transaction is rolled
back, the changes are dropped. If the commit succeeds, the changes recorded in the transactional view are
merged into the cache. At any given time, the cache reflects the cumulative effect of all operations on the
cache, including committed transactions and non-transactional operations.

JTA Transaction Limitations:

• Only one JDBC database instance per transaction is allowed, although you can have multiple connections
to that database.
• Multiple threads cannot participate in a transaction.
• Transaction recovery after a crash is not supported.
In addition, JTA transactions are subject to the limitations of GemFire cache transactions, which is discussed
in the previous section. When a global transaction needs to access the GemFire cache, JTA silently
starts a GemFire cache transaction.

JTA Sync Up :

JTA syncs up multiple transactions under one umbrella by enabling transactional resources to enlist with a
global transaction. The GemFire cache can register as a JTA resource through JTA synchronizations. This
allows the JTA transaction manager to call methods like commit and rollback on the GemFire resource and
manage the GemFire transactions. You can bind in JDBC resources so you can look them up in the JNDI
context. When bound in, XAPooledDataSource resources will automatically enlist if called within the context
of a transaction.

Example :

Using GemFire JTA Transaction Manager

The external data sources used in this transaction are configured in cache.xml.

Region r = ...; // the gemfire regionDataSource 
ds = ...; // other datasource

try {
Context ctx = cache.getJNDIContext();
Connection conn = null;
UserTransaction
tx = (UserTransaction)
ctx.lookup("java:/UserTransaction");
tx.begin();
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String sqlSTR = "insert into " + tableName + " values (........ )";
stmt.executeUpdate(sqlSTR);
r.put("key", "value");
stmt.close();
tx.commit();
conn.close();
} catch (NamingException e) {
// handle the exception
}

No comments :