心情小站
勤學(xué)、勤思
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
posts - 193, comments - 520, trackbacks - 0
ibatis DAO 事務(wù)探索
ibatis DAO 框架提供了事務(wù)管理模塊。而這個事務(wù)管理可以應(yīng)用到很多場合,包括JDBC、Hibernate、JTA、SQLMAP等。
下面以最簡單的JDBC來分析一下其如何實現(xiàn)事務(wù)管理。
首先來看一段代碼:
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();
//
第一個事務(wù)
daoManager.startTransaction();
//
開始第二個事務(wù)
orderDao.method1();
orderDao.method2();
daoManager.commitTransaction();
//
提交第二個事務(wù)
}
finally
{
daoManager.endTransaction();
}
}
}
在method()方法里有著兩個事務(wù),如果在方法里不顯式的調(diào)用daoManager.startTransaction(),則每個DAO的一次方法調(diào)用就是一個獨立的事務(wù)。
ibatis DAO事務(wù),有兩個核心接口DaoTransactionManager和DaoTransaction
對應(yīng)著不同的數(shù)據(jù)庫持久層實現(xiàn),兩個接口分別對應(yīng)著不同實現(xiàn)
查看ibatis 代碼,可以發(fā)現(xiàn)這些manager實現(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的代碼,非常簡單,四個常量來表示事務(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();
那么實際程序中是如何控制事務(wù)的呢
在第一段代碼中,我們是這樣取得DAO
orderDao = (OrderDao) daoManager.getDao(OrderDao.class);
實際daoManager返回的并不是orderDao的具體實現(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的一次方法時,如果不顯式的調(diào)用daoManager.startTransaction(),就會成為單獨的一個事務(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控制多個DAO的事務(wù)實際是讓這些DAO共用了一個DaoTransaction(ThreadLocal),一個Connection
這里是一個事務(wù)源的情況,如果多個事務(wù)源之間要完成全局事務(wù),還是老老實實用分布式事務(wù)管理服務(wù)吧(jta)
http://www.tkk7.com/ronghao 榮浩原創(chuàng),轉(zhuǎn)載請注明出處:)
posted on 2006-01-20 17:50
ronghao
閱讀(7215)
評論(6)
編輯
收藏
所屬分類:
工作日志
FeedBack:
#
re: ibatis DAO 事務(wù)探索
2006-12-06 03:14 |
..
無聊
回復(fù)
更多評論
#
re: ibatis DAO 事務(wù)探索
2007-02-10 11:58 |
yidinghe
很棒!很有啟發(fā)性!謝謝!
回復(fù)
更多評論
#
re: ibatis DAO 事務(wù)探索
2007-07-25 23:43 |
zph
不錯,謝謝
回復(fù)
更多評論
#
re: ibatis DAO 事務(wù)探索
2008-03-26 10:37 |
屹礫
先看一下,我也遇到了這個問題,一個事務(wù)操縱多個行為,不過事務(wù)總是沒有原子性,比較嚴重。
回復(fù)
更多評論
#
re: ibatis DAO 事務(wù)探索
2009-10-23 13:19 |
w
@..
人家辛苦發(fā)表- -說無聊真欠揍
回復(fù)
更多評論
#
re: ibatis DAO 事務(wù)探索
2011-03-16 02:35 |
leekiang
這里的好幾個類在2.3版本里好像都去掉了
回復(fù)
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
少年P(guān)i的奇幻漂流-我們的后臺自動化發(fā)布方案
一個項目的自動化測試實踐
心理學(xué),再談好代碼
數(shù)據(jù)驅(qū)動測試
使用Selenium測試showModalDialog模態(tài)對話框
(Multi-stage Continuous Integration)多階段持續(xù)集成
基于memcached的SNA實現(xiàn)
SNA方案之session炒冷飯
一次性能調(diào)優(yōu)的實戰(zhàn)
從貧血到充血Domain Model
Copyright ©2025 ronghao Powered By:
博客園
模板提供:
滬江博客
<
2006年1月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
關(guān)注工作流和企業(yè)業(yè)務(wù)流程改進。現(xiàn)就職于ThoughtWorks。新浪微博:
http://weibo.com/ronghao100
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(38)
給我留言
查看公開留言
查看私人留言
隨筆分類
ajax相關(guān)(9)
cms(7)
Head First Process-深入淺出流程(15)
j2se基礎(chǔ)(6)
JbpmSide(6)
OOA/OOD(4)
SOA、BPM(26)
工作日志(24)
工作流jbpm3(10)
張小慶,在路上(42)
心情小站(24)
權(quán)限相關(guān)(12)
表現(xiàn)層相關(guān)(4)
轉(zhuǎn)載(4)
隨筆檔案
2013年8月 (1)
2012年12月 (1)
2012年1月 (3)
2011年12月 (2)
2011年11月 (2)
2011年10月 (3)
2011年9月 (3)
2011年8月 (7)
2011年7月 (4)
2011年6月 (3)
2011年5月 (5)
2011年4月 (6)
2011年3月 (4)
2011年2月 (2)
2010年9月 (1)
2010年6月 (1)
2010年5月 (1)
2010年3月 (4)
2010年1月 (2)
2009年11月 (5)
2009年10月 (4)
2009年9月 (1)
2009年7月 (1)
2009年6月 (2)
2009年5月 (2)
2009年4月 (1)
2009年3月 (4)
2009年2月 (2)
2008年12月 (1)
2008年11月 (1)
2008年10月 (1)
2008年9月 (2)
2008年8月 (2)
2008年7月 (2)
2008年6月 (3)
2008年5月 (4)
2008年4月 (1)
2008年3月 (2)
2008年2月 (2)
2008年1月 (4)
2007年11月 (3)
2007年10月 (3)
2007年9月 (2)
2007年8月 (4)
2007年7月 (1)
2007年6月 (12)
2007年5月 (2)
2007年4月 (1)
2007年3月 (8)
2007年2月 (6)
2007年1月 (4)
2006年12月 (4)
2006年11月 (3)
2006年10月 (1)
2006年8月 (2)
2006年7月 (3)
2006年6月 (3)
2006年4月 (1)
2006年3月 (2)
2006年2月 (2)
2006年1月 (4)
2005年12月 (7)
2005年11月 (12)
文章分類
Hibernate3
JSP標簽
工作流jbpm3(2)
文章檔案
2005年11月 (2)
常去的網(wǎng)站
JavaEye
JAVA研究組織
開源大全
搜索
最新評論
1.?re: 使用Handler來增強Web服務(wù)的功能
asdfasfd
--ads
2.?re: 使用solr搭建你的全文檢索
@木哥哥
你的分詞器用的是什么啊?mmseg貌似可以的
--陳冠馳
3.?re: 使用solr搭建你的全文檢索
@marten這是你的solr的schame.xml配置文件有問題。好好檢查下你的配置文件里面的字段什么的配置對著沒
--陳冠馳
4.?re: 討論一下你覺得一個工作流產(chǎn)品好的標準
評論內(nèi)容較長,點擊標題查看
--深圳非凡信息技術(shù)有限公司
5.?re: DisplayTag應(yīng)用
name="test"從哪里來的,千篇一律的到處使用test卻沒有test的定義,sb
--qige
閱讀排行榜
1.?使用solr搭建你的全文檢索(67360)
2.?工作流開發(fā)小結(jié)(10139)
3.?結(jié)合spring+hibernate與jdbc的事務(wù)(7741)
4.?jBPM4與Spring的集成(7372)
5.?ibatis DAO 事務(wù)探索(7215)
評論排行榜
1.?什么是JAVA內(nèi)容倉庫(Java Content Repository)(2)(25)
2.?DisplayTag應(yīng)用(25)
3.?高并發(fā)測試下的一些問題及解決(22)
4.?使用solr搭建你的全文檢索(14)
5.?開始開發(fā)CMS(12)
主站蜘蛛池模板:
免费电影在线观看网站
|
亚洲AV无码一区二区三区国产
|
全部免费a级毛片
|
亚洲成AV人片一区二区密柚
|
亚洲色欲色欲www
|
fc2成年免费共享视频18
|
国产一卡二卡四卡免费
|
亚洲国产成人五月综合网
|
亚洲欧洲精品久久
|
一级毛片免费播放视频
|
亚洲三级高清免费
|
亚洲综合色婷婷七月丁香
|
亚洲人成网站色在线观看
|
国产精品美女久久久免费
|
久久精品a一国产成人免费网站
|
久久亚洲国产精品123区
|
成人区精品一区二区不卡亚洲
|
两个人日本WWW免费版
|
波多野结衣久久高清免费
|
亚洲Aⅴ无码专区在线观看q
|
国产亚洲福利一区二区免费看
|
中文字幕免费视频
|
国产成人精品日本亚洲专区61
|
亚洲AV人无码激艳猛片
|
国产精品亚洲专区一区
|
国产福利在线观看免费第一福利
|
国产亚洲精品福利在线无卡一
|
亚洲午夜无码久久久久小说
|
无码精品人妻一区二区三区免费看
|
亚洲综合av一区二区三区不卡
|
日韩精品一区二区亚洲AV观看
|
欧洲美女大片免费播放器视频
|
青苹果乐园免费高清在线
|
香蕉蕉亚亚洲aav综合
|
a高清免费毛片久久
|
国产成人免费a在线视频色戒
|
亚洲成人福利网站
|
无码精品人妻一区二区三区免费看
|
亚洲国产成人久久综合一区77
|
亚洲另类无码专区丝袜
|
国产成在线观看免费视频
|