怎么進(jìn)行iBATIS DAO事務(wù)淺析,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司是一家專業(yè)的網(wǎng)站制作公司,提供的服務(wù)包括:品牌網(wǎng)站設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),我們是一家專業(yè)網(wǎng)站設(shè)計(jì)公司,做網(wǎng)站,我們是認(rèn)真的。我們是成都網(wǎng)站制作,成都網(wǎng)站建設(shè)的先行者,一切以客戶的利益為設(shè)計(jì)方向,能夠?yàn)椴煌袠I(yè)的客戶提供全面、長(zhǎng)期、深入的網(wǎng)絡(luò)解決方案。 創(chuàng)新互聯(lián)公司根據(jù)客戶的具體需求,提供從策劃、創(chuàng)意、制作、執(zhí)行等服務(wù)。
iBATIS DAO事務(wù)的理解要從iBATIS DAO 框架開(kāi)始,它提供了事務(wù)管理模塊。而這個(gè)事務(wù)管理可以應(yīng)用到很多場(chǎng)合,包括JDBC、Hibernate、JTA、SQLMAP等。
下面以最簡(jiǎn)單的JDBC來(lái)分析一下其如何實(shí)現(xiàn)iBATIS DAO事務(wù)管理。
首先來(lái)看一段代碼:
public class OrderService { private DaoManager daoManager; private OrderDao orderDao; public OrderService() { daoManager = DaoConfig.getDaoManager(); orderDao = (OrderDao) daoManager.getDao(OrderDao.class); } public void method() { try { //a separate transaction orderDao.method1(); //***個(gè)事務(wù) daoManager.startTransaction(); //開(kāi)始第二個(gè)事務(wù) orderDao.method1(); orderDao.method2(); daoManager.commitTransaction();//提交第二個(gè)事務(wù) } finally { daoManager.endTransaction(); } } }
在method()方法里有著兩個(gè)事務(wù),如果在方法里不顯式的調(diào)用daoManager.startTransaction(),則每個(gè)DAO的一次方法調(diào)用就是一個(gè)獨(dú)立的事務(wù)。
iBATIS DAO事務(wù),有兩個(gè)核心接口DaoTransactionManager和DaoTransaction
對(duì)應(yīng)著不同的數(shù)據(jù)庫(kù)持久層實(shí)現(xiàn),兩個(gè)接口分別對(duì)應(yīng)著不同實(shí)現(xiàn)
查看iBATIS 代碼,可以發(fā)現(xiàn)這些manager實(shí)現(xiàn)事務(wù),就是調(diào)用事務(wù)源的事務(wù)操作方法
JdbcDaoTransactionManager public void commitTransaction(DaoTransaction trans) { ((JdbcDaoTransaction) trans).commit(); } JdbcDaoTransaction public JdbcDaoTransaction(DataSource dataSource) { try { connection = dataSource.getConnection(); if (connection == null) { throw new DaoException("Could not start transaction.Cause: The DataSource returned a null connection."); } if (connection.getAutoCommit()) { connection.setAutoCommit(false); } if (connectionLog.isDebugEnabled()) { connection = ConnectionLogProxy.newInstance(connection); } } catch (SQLException e) { throw new DaoException("Error starting JDBC transaction.Cause: " + e); } } public void commit() { try { try { connection.commit(); } finally { connection.close(); } } catch (SQLException e) { throw new DaoException("Error committing JDBC transaction.Cause: " + e); } }
那么DaoTransactionManager以什么依據(jù)處理事務(wù)呢?DaoTransactionState看看DaoTransactionState的代碼,非常簡(jiǎn)單,四個(gè)常量來(lái)表示事務(wù)處于的不同的狀態(tài)
public static final DaoTransactionState ACTIVE = new DaoTransactionState();
public static final DaoTransactionState INACTIVE = new DaoTransactionState();
public static final DaoTransactionState COMMITTED = new DaoTransactionState();
public static final DaoTransactionState ROLLEDBACK = new DaoTransactionState();
那么實(shí)際程序中是如何控制事務(wù)的呢
在***段代碼中,我們是這樣取得DAO
orderDao = (OrderDao) daoManager.getDao(OrderDao.class);
實(shí)際daoManager返回的并不是orderDao的具體實(shí)現(xiàn)類,它返回的DaoProxy
DaoProxy
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; if (PASSTHROUGH_METHODS.contains(method.getName())) { try { result = method.invoke(daoImpl.getDaoInstance(), args); } catch (Throwable t) { throw ClassInfo.unwrapThrowable(t); } } else { StandardDaoManager daoManager = daoImpl.getDaoManager(); DaoContext context = daoImpl.getDaoContext(); if (daoManager.isExplicitTransaction()) { // Just start the transaction (explicit) try { context.startTransaction(); result = method.invoke(daoImpl.getDaoInstance(), args); } catch (Throwable t) { throw ClassInfo.unwrapThrowable(t); } } else { // Start, commit and end the transaction (autocommit) try { context.startTransaction(); result = method.invoke(daoImpl.getDaoInstance(), args); context.commitTransaction(); } catch (Throwable t) { throw ClassInfo.unwrapThrowable(t); } finally { context.endTransaction(); } } } return result; }
看到這段代碼就非常清楚了,每調(diào)用DAO的一次方法時(shí),如果不顯式的調(diào)用daoManager.startTransaction(),就會(huì)成為單獨(dú)的一個(gè)iBATIS DAO事務(wù)。再看看iBATIS為我們提供的摸板JdbcDaoTemplate
protected Connection getConnection() { DaoTransaction trans = daoManager.getTransaction(this); if (!(trans instanceof ConnectionDaoTransaction)) { throw new DaoException("The DAO manager of type " + daoManager.getClass().getName() + " cannot supply a JDBC Connection for this template, and is therefore not" + "supported by JdbcDaoTemplate."); } return ((ConnectionDaoTransaction) trans).getConnection(); }
iBATIS控制多個(gè)DAO的事務(wù)實(shí)際是讓這些DAO共用了一個(gè)DaoTransaction(ThreadLocal),一個(gè)Connection
這里是一個(gè)事務(wù)源的情況,如果多個(gè)事務(wù)源之間要完成全局事務(wù),還是老老實(shí)實(shí)用分布式事務(wù)管理服務(wù)吧(jta)。
關(guān)于怎么進(jìn)行iBATIS DAO事務(wù)淺析問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。