LATEST VERSION: 8.2.8 - CHANGELOG
Pivotal GemFire® v8.2

How to Run a GemFire Cache Transaction that Coordinates with an External Database

How to Run a GemFire Cache Transaction that Coordinates with an External Database

This topic describes how to coordinate a GemFire cache transaction with an external database by using CacheWriter/CacheListener and TransactionWriter/TransactionListener plug-ins. This provides an alternative to using JTA transactions.

There are a few things you should be careful about while working with GemFire cache transactions and external databases:
  • When you set up the JDBC connection, make sure that auto-commit is disabled. For example, in Java:
    Connection getConnection() throws SQLException {
        Connection con = ... // create the connection
        con.setAutoCommit(false);
        return con;
    }
  • The BEGIN statement, database operations and the PREPARE statement must all happen in the same connection session. In order to accomplish this, you will need to obtain the same JDBC connection session across multiple CacheWriter and TransactionWriter/TransactionListener invocations. One way to do this would be to lookup the connection (from a user managed Map) based on cacheTransactionManager.getTransactionId().
  • Make sure that the prepare transaction feature is enabled in your external database. For example, it is disabled in PostgreSQL by default. In PostgreSQL, the following property must be modified to enable it:
    max_prepared_transactions = 1 # 1 or more enables, zero (default) disables this feature.
Use the following procedure to write a GemFire cache transaction that coordinates with an external database:
  1. Configure GemFire regions as necessary as described inHow to Run a GemFire Cache Transaction.
  2. Begin the transaction.
    1. In GemFire :
      CacheTransactionManager txManager =
            cache.getCacheTransactionManager(); 
      
            try { 
                  txManager.begin();
                  // ... do GemFire operations
    2. If you have not previously committed a previous transaction in this connection, start a database transaction by issuing a BEGIN statement.
  3. Perform GemFire cache operations; each cache operation invokes the CacheWriter. Program your CacheWriter to do the corresponding database operations.
  4. Commit the transaction:
    txManager.commit();
         } catch (CommitConflictException conflict)

    At this point, the TransactionWriter is invoked. The TransactionWriter returns a TransactionEvent, which contains all the operations in the transaction. Call PREPARE TRANSACTION within your TransactionWriter code.

  5. After a transaction is successfully committed in GemFire , TransactionListener is invoked. Call COMMIT PREPARED in the TransactionListener to commit the database transaction.