心情小站
勤學、勤思
BlogJava
首頁
新隨筆
聯系
聚合
管理
posts - 193, comments - 520, trackbacks - 0
ibatis DAO 事務探索
ibatis DAO 框架提供了事務管理模塊。而這個事務管理可以應用到很多場合,包括JDBC、Hibernate、JTA、SQLMAP等。
下面以最簡單的JDBC來分析一下其如何實現事務管理。
首先來看一段代碼:
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();
//
第一個事務
daoManager.startTransaction();
//
開始第二個事務
orderDao.method1();
orderDao.method2();
daoManager.commitTransaction();
//
提交第二個事務
}
finally
{
daoManager.endTransaction();
}
}
}
在method()方法里有著兩個事務,如果在方法里不顯式的調用daoManager.startTransaction(),則每個DAO的一次方法調用就是一個獨立的事務。
ibatis DAO事務,有兩個核心接口DaoTransactionManager和DaoTransaction
對應著不同的數據庫持久層實現,兩個接口分別對應著不同實現
查看ibatis 代碼,可以發現這些manager實現事務,就是調用事務源的事務操作方法
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以什么依據處理事務呢?DaoTransactionState看看DaoTransactionState的代碼,非常簡單,四個常量來表示事務處于的不同的狀態
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();
那么實際程序中是如何控制事務的呢
在第一段代碼中,我們是這樣取得DAO
orderDao = (OrderDao) daoManager.getDao(OrderDao.class);
實際daoManager返回的并不是orderDao的具體實現類,它返回的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;
}
看到這段代碼就非常清楚了,每調用DAO的一次方法時,如果不顯式的調用daoManager.startTransaction(),就會成為單獨的一個事務。再看看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的事務實際是讓這些DAO共用了一個DaoTransaction(ThreadLocal),一個Connection
這里是一個事務源的情況,如果多個事務源之間要完成全局事務,還是老老實實用分布式事務管理服務吧(jta)
http://www.tkk7.com/ronghao 榮浩原創,轉載請注明出處:)
posted on 2006-01-20 17:50
ronghao
閱讀(7228)
評論(6)
編輯
收藏
所屬分類:
工作日志
FeedBack:
#
re: ibatis DAO 事務探索
2006-12-06 03:14 |
..
無聊
回復
更多評論
#
re: ibatis DAO 事務探索
2007-02-10 11:58 |
yidinghe
很棒!很有啟發性!謝謝!
回復
更多評論
#
re: ibatis DAO 事務探索
2007-07-25 23:43 |
zph
不錯,謝謝
回復
更多評論
#
re: ibatis DAO 事務探索
2008-03-26 10:37 |
屹礫
先看一下,我也遇到了這個問題,一個事務操縱多個行為,不過事務總是沒有原子性,比較嚴重。
回復
更多評論
#
re: ibatis DAO 事務探索
2009-10-23 13:19 |
w
@..
人家辛苦發表- -說無聊真欠揍
回復
更多評論
#
re: ibatis DAO 事務探索
2011-03-16 02:35 |
leekiang
這里的好幾個類在2.3版本里好像都去掉了
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
少年Pi的奇幻漂流-我們的后臺自動化發布方案
一個項目的自動化測試實踐
心理學,再談好代碼
數據驅動測試
使用Selenium測試showModalDialog模態對話框
(Multi-stage Continuous Integration)多階段持續集成
基于memcached的SNA實現
SNA方案之session炒冷飯
一次性能調優的實戰
從貧血到充血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
關注工作流和企業業務流程改進。現就職于ThoughtWorks。新浪微博:
http://weibo.com/ronghao100
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(38)
給我留言
查看公開留言
查看私人留言
隨筆分類
ajax相關(9)
cms(7)
Head First Process-深入淺出流程(15)
j2se基礎(6)
JbpmSide(6)
OOA/OOD(4)
SOA、BPM(26)
工作日志(24)
工作流jbpm3(10)
張小慶,在路上(42)
心情小站(24)
權限相關(12)
表現層相關(4)
轉載(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)
常去的網站
JavaEye
JAVA研究組織
開源大全
搜索
最新評論
1.?re: 使用Handler來增強Web服務的功能
asdfasfd
--ads
2.?re: 使用solr搭建你的全文檢索
@木哥哥
你的分詞器用的是什么啊?mmseg貌似可以的
--陳冠馳
3.?re: 使用solr搭建你的全文檢索
@marten這是你的solr的schame.xml配置文件有問題。好好檢查下你的配置文件里面的字段什么的配置對著沒
--陳冠馳
4.?re: 討論一下你覺得一個工作流產品好的標準
評論內容較長,點擊標題查看
--深圳非凡信息技術有限公司
5.?re: DisplayTag應用
name="test"從哪里來的,千篇一律的到處使用test卻沒有test的定義,sb
--qige
閱讀排行榜
1.?使用solr搭建你的全文檢索(67390)
2.?工作流開發小結(10146)
3.?結合spring+hibernate與jdbc的事務(7749)
4.?jBPM4與Spring的集成(7382)
5.?ibatis DAO 事務探索(7228)
評論排行榜
1.?什么是JAVA內容倉庫(Java Content Repository)(2)(25)
2.?DisplayTag應用(25)
3.?高并發測試下的一些問題及解決(22)
4.?使用solr搭建你的全文檢索(14)
5.?開始開發CMS(12)
主站蜘蛛池模板:
国产免费久久精品99re丫y
|
成年女人午夜毛片免费看
|
好男人看视频免费2019中文
|
亚洲精品美女久久久久99小说
|
亚洲a在线视频视频
|
亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片
|
亚洲AⅤ永久无码精品AA
|
久久精品国产亚洲av四虎
|
亚洲欧美aⅴ在线资源
|
97人妻精品全国免费视频
|
成人A级毛片免费观看AV网站
|
在线亚洲精品福利网址导航
|
亚洲国产成人精品青青草原
|
日韩大片在线永久免费观看网站
|
性生大片视频免费观看一级
|
久久久久久国产a免费观看黄色大片
|
亚洲AV天天做在线观看
|
亚洲国产精品成人AV在线
|
日本黄色动图免费在线观看
|
国产高清在线精品免费软件
|
久久亚洲AV成人无码
|
igao激情在线视频免费
|
好爽…又高潮了毛片免费看
|
亚洲大片在线观看
|
免费一级特黄特色大片
|
久久久久久免费视频
|
亚洲成年轻人电影网站www
|
日韩久久无码免费毛片软件
|
A在线观看免费网站大全
|
亚洲AV无码久久
|
日韩在线视频线视频免费网站
|
免费毛片a在线观看67194
|
亚洲毛片基地日韩毛片基地
|
一级做a爱片特黄在线观看免费看
|
免费一本色道久久一区
|
亚洲四虎永久在线播放
|
国产无遮挡色视频免费观看性色
|
成年人在线免费看视频
|
亚洲网站免费观看
|
国产日韩AV免费无码一区二区
|
免费一级毛片不卡在线播放
|