亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Ehcache 事務(wù)管理源碼探析

系統(tǒng) 2217 0

可能與大家關(guān)注點有不同,有考慮不周處,請大家指出...

Ehcache獲取分布式事務(wù)支持可從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")};

默認(rèn)獲取JNDI名“java:/TransactionManager”。JBoss JTA事務(wù)。

現(xiàn)跟蹤Ehcache PUT操作時是如何加入事務(wù)的。

當(dāng)Ehcache配置成xa或者xa-strict時,內(nèi)部使用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)));
}

紅色部分是事務(wù)關(guān)鍵操作:初始化事務(wù)上下文

      
        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);
}
}
net.sf.ehcache.transaction.xa.EhcacheXAResourceImpl為Ehcache XAResource的實現(xiàn)。
研究一下XAResource都干了些啥。無非是該事務(wù)源相關(guān)屬性及提交、回滾等操作吧。具體看一下實現(xiàn)代碼:
關(guān)鍵屬性:
        
          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;
......
關(guān)鍵方法:
        
          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;
前兩個為XAResource接口抽象方法,完成事務(wù)的基本操作,在JTA事務(wù)提交或是回滾時會被調(diào)用;后面是Ehcache抽象出來的接口方法。
接著關(guān)心這里的rollback()方法都做了啥。關(guān)鍵代碼如下:
        ?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;
}
可以看到進(jìn)行了底部存儲邏輯的回滾處理。
實例化好XAResource后回到初始化事務(wù)上下文方法getOrCreateTransactionContext中,可以知道通過該XAResource直接取得XATransactionContext。并且給XAResource注冊了兩個監(jiān)聽器UnregisterXAResource和CleanupXAResource(在事務(wù)提交或是混回滾時啟動)。
至此事務(wù)的初始化工作完成。
?
理一下put過程。外部調(diào)用 XATransactionStore 的put方法,向當(dāng)前XID的XATransactionContext中添加addCommand(封裝了針對指定cache具體的提交與回滾操作),接著控制權(quán)到事務(wù)管理中,事務(wù)管理器管理 EhcacheXAResourceImpl 對象,提交與回滾操作通過取得XATransactionContext中的Commands對象并執(zhí)行來完成。

歡迎大家批評指正!

?
?

Ehcache 事務(wù)管理源碼探析


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 这里只有精品99re在线 | 在线免费观看毛片 | 免费播放毛片 | 免费看美女吃男生私人部位 | 日本免费久久 | 亚洲乱码国产乱码精品精98 | 欧美一级毛片片aa视频 | 色艺网 | 99在线免费 | 国产男女xoxo在线视频 | 国产成人亚洲精品大帝 | 日韩精品一区二区三区中文 | 午夜影院私人 | 国产精品香蕉在线观看不卡 | 精品综合久久久久久98 | 欧美成人一区二区三区在线电影 | 一区二区三区四区免费视频 | 亚洲精品高清国产一线久久97 | 不卡国产 | 丁香婷婷成人 | 亚洲第一欧美 | 亚洲综合色就色手机在线观看 | 欧美日韩高清一区二区三区 | 九九视频精品在线 | 亚洲成a人片在线观看精品 亚洲成a人一区二区三区 | 波多野结衣三区 | 欧美成视频一theporn | 99re66热这里只有精品17 | 草草草在线观看 | 青草青草久热精品视频99 | 精品煌色视频网站在线观看 | 久热网 | 日本精品中文字幕在线不卡 | 亚洲精品99久久久久久 | 久久草精品视频 | 99国产超薄丝袜足j在线播放 | 欧美国产亚洲一区二区三区 | 1级黄色毛片 | 99精品在线免费 | 一级黄色免费网站 | 欧美成人一区亚洲一区 |