private final JndiSelector defaultJndiSelector = new JndiSelector("genericJNDI", "java:/TransactionManager");
private final Selector[] transactionManagerSelectors = new Selector[] {defaultJndiSelector,
new JndiSelector("Weblogic", "javax.transaction.TransactionManager"),
new FactorySelector("Bitronix", "bitronix.tm.TransactionManagerServices"),
new ClassSelector("Atomikos", "com.atomikos.icatch.jta.UserTransactionManager")};
默认获取JNDI名“java:/TransactionManager”。JBoss JTA事务。如果需要其他类型的事务管理获取方式,
通过配置TransactionManagerLookup来替代默认的。
配置如下:
public boolean put(Element element) throws CacheException {
LOG.debug("cache {} put {}", cache.getName(), element);
// this forces enlistment so the XA transaction timeout can be propagated to the XA resource
getOrCreateTransactionContext();
Element oldElement = getQuietFromUnderlyingStore(element.getObjectKey());
return internalPut(new StorePutCommand(oldElement, copyElementForWrite(element)));
}
红色部分是事务关键操作:初始化事务上下文。事务上下文维护着该事务的所有cache操作、remove操作的keys、put操作的keys和underlyingStore。
private XATransactionContext getOrCreateTransactionContext() {
try {
EhcacheXAResourceImpl xaResource = getOrCreateXAResource();
XATransactionContext transactionContext = xaResource.getCurrentTransactionContext();
if (transactionContext == null) {
transactionManagerLookup.register(xaResource);
LOG.debug("creating new XA context");
transactionContext = xaResource.createTransactionContext();
xaResource.addTwoPcExecutionListener(new UnregisterXAResource());
} else {
transactionContext = xaResource.getCurrentTransactionContext();
}
LOG.debug("using XA context {}", transactionContext);
return transactionContext;
} catch (SystemException e) {
throw new TransactionException("cannot get the current transaction", e);
} catch (RollbackException e) {
throw new TransactionException("transaction rolled back", e);
}
}
构建事务上下文的操作:
public XATransactionContext createTransactionContext() throws SystemException, RollbackException {
XATransactionContext ctx = getCurrentTransactionContext();
if (ctx != null) {
return ctx;
}
Transaction transaction = txnManager.getTransaction();
LOG.debug("enlisting {} in {}", this, transaction);
transaction.enlistResource(this);//关键操作:注册当前XAResource到事务中
// currentXid is set by a call to start() which itself is called by transaction.enlistResource(this)
if (currentXid == null) {
throw new CacheException("enlistment of XAResource of cache named '" + getCacheName() +
"' did not end up calling XAResource.start()");
}
ctx = xidToContextMap.get(currentXid);
if (ctx == null) {
LOG.debug("creating new context for XID [{}]", currentXid);
ctx = new XATransactionContext(underlyingStore);
xidToContextMap.put(currentXid, ctx);
}
return ctx;
}
private final Ehcache cache;
private final Store underlyingStore;
private final TransactionIDFactory transactionIDFactory;
private final TransactionManager txnManager;
private final SoftLockFactory softLockFactory;
private final ConcurrentMap<Xid, XATransactionContext> xidToContextMap = new ConcurrentHashMap<Xid, XATransactionContext>();
private final XARequestProcessor processor;
private volatile Xid currentXid;
private volatile int transactionTimeout;
private final List<XAExecutionListener> listeners = new ArrayList<XAExecutionListener>();
private final ElementValueComparator comparator;
......
关键方法:
public void commit(Xid xid, boolean onePhase)
public void rollback(Xid xid) throws XAException
/**
* Add a listener which will be called back according to the 2PC lifecycle
* @param listener the XAExecutionListener
*/
void addTwoPcExecutionListener(XAExecutionListener listener);
/**
* Obtain the already associated {@link XATransactionContext} with the current Transaction,
* or create a new one should none be there yet.
* @return The associated Transaction associated {@link XATransactionContext}
*/
XATransactionContext createTransactionContext() throws SystemException, RollbackException;