欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Ehcache 事務管理源碼探析

系統 2283 0

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

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

歡迎大家批評指正!

?
?

Ehcache 事務管理源碼探析


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 清纯唯美综合网 | 日本黄色a视频 | 99久久99久久免费精品蜜桃 | 日韩在线观看视频免费 | jyzzjyzzjyzz日本在线观看 | 四虎影视免费在线观看 | 国产视频在| 婷婷玖玖| 夜干夜夜| 精品免费久久久久欧美亚一区 | 日韩a无吗一区二区三区 | 国产成人短视频 | 国产激情视频 | 国产精品久久久久久久久久久搜索 | 国产免费观看一区二区三区 | 羞羞的视频在线免费观看 | 成年人免费网 | 欧美另类色图 | 性色欧美| 国产一区二区精品在线观看 | 国产精品久久久久久久久久久久 | 亚洲精品久久久久无码AV片软件 | 久久久久成人精品亚洲国产 | 欧美精品99毛片免费高清观看 | 欧美精品日韩一区二区三区 | 欧美四虎 | 国产美女高清片免费观看 | 欧美成人免费高清网站 | 国产精品视频免费观看 | 欧美不卡一区二区三区在线观看 | 一区二区欧美视频 | 日韩福利视频导航 | 亚洲欧美国产视频 | 日韩视频第二页 | 亚洲视频免费 | 欧美一区二区三区免费高 | 一区二区在线不卡 | 欧美两性| 欧美综合久久 | 成人久久久久久久久 | 国产在线不卡 |