可能與大家關注點有不同,有考慮不周處,請大家指出...
Ehcache獲取分布式事務支持可從net.sf.ehcache.transaction.manager. DefaultTransactionManagerLookup 類中知曉:
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事務。
現跟蹤Ehcache PUT操作時是如何加入事務的。
當Ehcache配置成xa或者xa-strict時,內部使用net.sf.ehcache.transaction.xa.XATransactionStore存儲邏輯,put操作如下:
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)));
}
紅色部分是事務關鍵操作:初始化事務上下文
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);
}
}
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;
?int rc = prepareInternal(xid);
?if (rc == XA_RDONLY) {
? ?return;
? ?}
public
int
prepareInternal(Xid xid)
throws
XAException {
fireBeforePrepare();
XATransactionContext twopcTransactionContext = xidToContextMap.get(xid);
if
(twopcTransactionContext ==
null
) {
throw
new
EhcacheXAException("transaction never started: " + xid, XAException.XAER_NOTA);
}
XidTransactionID xidTransactionID = transactionIDFactory.createXidTransactionID(xid);
List<Command> commands = twopcTransactionContext.getCommands();
List<Command> preparedCommands =
new
LinkedList<Command>();
boolean
prepareUpdated =
false
;
LOG.debug("preparing {} command(s) for [{}]", commands.size(), xid);
for
(Command command : commands) {
try
{
prepareUpdated |= command.prepare(underlyingStore, softLockFactory, xidTransactionID, comparator);
preparedCommands.add(0, command);
}
catch
(OptimisticLockFailureException ie) {
for
(Command preparedCommand : preparedCommands) {
preparedCommand.rollback(underlyingStore);
}
preparedCommands.clear();
throw
new
EhcacheXAException(command + " failed because value changed between execution and 2PC",
XAException.XA_RBINTEGRITY, ie);
}
}
xidToContextMap.remove(xid);
if
(!prepareUpdated) {
rollbackInternal(xid);
}
LOG.debug("prepared xid [{}] read only? {}", xid, !prepareUpdated);
return
prepareUpdated ? XA_OK : XA_RDONLY;
}
歡迎大家批評指正!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

