Local Transactions
Local transactional caches (with the transactionalMode
attribute
set to "local") write to a local store using an API that is part
of the Ehcache core API. Local transactions have the following characteristics:
- Recovery occurs at the time an element is accessed.
- Updates are written to the underlying store immediately.
- Get operations on the underlying store may block during commit operations.
To use local transactions, instantiate a TransactionController instance instead of a transaction manager instance:
TransactionController txCtrl = cacheManager.getTransactionController();
...
txCtrl.begin();
Cache fooCache = cacheManager.getCache("Foo");
fooCache.put("1", "Bar");
txCtrl.commit();
...
You can use rollback()
to roll
back the transaction bound to the current thread.
Tip: You can find out if a transaction is in process
on the current thread by calling
TransactionController.getCurrentTransactionContext()
and
checking its return value. If the value isn't null, a transaction
has started on the current thread.Commit Failures and Timeouts
Commit operations can fail if the transaction times out. If the default timeout requires tuning, you can get and set its current value:
int currentDefaultTransactionTimeout = txCtrl.getDefaultTransactionTimeout();
...
txCtrl.setDefaultTransactionTimeout(30); // in seconds -- must be greater than zero.
You can also bypass the commit timeout
using the following version of commit()
:
txCtrl.commit(true); // "true" forces the commit to ignore the timeout.