henry1451 的專欄
BlogJava
首頁
新隨筆
新文章
聯(lián)系
聚合
管理
posts - 60,comments - 71,trackbacks - 0
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
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
5
6
7
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(6)
給我留言
查看公開留言
查看私人留言
我參與的團隊
架構(gòu)師之家(0/0)
隨筆檔案
2009年9月 (1)
2009年6月 (1)
2009年5月 (2)
2009年4月 (3)
2009年3月 (2)
2009年1月 (1)
2008年12月 (3)
2008年11月 (2)
2008年10月 (3)
2008年9月 (7)
2008年8月 (9)
2008年7月 (23)
2008年6月 (1)
2008年5月 (2)
文章分類
Hibernate技術(shù)(5)
Java技術(shù)(15)
Jsp,Js,Ajax,Html技術(shù)(8)
Linux技術(shù)(2)
Oracle技術(shù)(9)
Spring技術(shù)
Struts,Webwork,Xwork技術(shù)(3)
其他相關(guān)(1)
開源技術(shù)(7)
文章檔案
2008年6月 (27)
2008年5月 (27)
2008年4月 (3)
博客集錦
hk2000c技術(shù)專欄
即興的靈感
和風(fēng)細雨
小方的Java博客
小飛龍
急死我了
每日一得
資源與技術(shù)網(wǎng)站
BlogJava熱點分類
BlogJava隨筆
JavaEye
J道
Matrix
Open-open
SourceForge
搜索
最新評論
1.?re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題
謝了, 很受用!
--碼農(nóng)C
2.?re: ClientAbortException 異常解決辦法
換瀏覽器后可以了
--換瀏覽器后可以了
3.?re: eclipse 下環(huán)境變量設(shè)置[未登錄]
請問 MAVEN_REPO在哪定義的?
--a
4.?re: 圖形統(tǒng)計工具amCharts體驗
無語
--EE
5.?re: ClientAbortException 異常解決辦法
。。。。。。。
--q
閱讀排行榜
1.?ClientAbortException 異常解決辦法(14199)
2.?Eclipse下安裝TomcatPlugin插件(8431)
3.?圖形統(tǒng)計工具amCharts體驗(6263)
4.?PL/pgSQL - SQL過程語言(轉(zhuǎn))(5581)
5.?如何修改存儲過程(4484)
評論排行榜
1.?取得單選按鈕中顯示的內(nèi)容(9)
2.?ClientAbortException 異常解決辦法(7)
3.?圖形統(tǒng)計工具amCharts體驗(4)
4.?10.1快樂!(4)
5.?重復(fù)提交、重復(fù)刷新、防止后退的問題以及處理方式(轉(zhuǎn))(4)
Hibernate中session的管理
session是hibernate運做的核心,是有SessionFactory所創(chuàng)建,sessionFactory是線程安全的,你可以讓多個線程同時存取SessionFactory,而不會有資源共用的問題,然而session不是設(shè)計為線程安全的,所以讓多個線程共用一個session,將發(fā)生資料共用而發(fā)生混亂的問題.下面是一個標準類.
import
java.io.Serializable;
import
net.sf.hibernate.HibernateException;
import
net.sf.hibernate.Session;
import
net.sf.hibernate.SessionFactory;
import
net.sf.hibernate.Transaction;
public
class
HibernateSessionUtil
implements
Serializable
{
//
創(chuàng)建線程局部變量 tLocalsess
public
static
final
ThreadLocal tLocalsess
=
new
ThreadLocal();
//
創(chuàng)建線程局部變量 tLocaltx
public
static
final
ThreadLocal tLocaltx
=
new
ThreadLocal();
//
取得session
public
static
Session currentSession()
{
//
從線程變量tLocalsess中,取得當前session
Session session
=
(Session) tLocalsess.get();
//
判斷session是否為空,如果為空,將創(chuàng)建一個session,并付給線程變量tLocalsess
try
{
if
(session
==
null
)
{
session
=
openSession();
tLocalsess.set(session);
}
}
catch
(HibernateException e)
{
throw
new
InfrastructureException(e);
}
return
session;
}
//
關(guān)閉當前session
public
static
void
closeSession()
{
//
從線程變量tLocalsess中,取得當前session
Session session
=
(Session) tLocalsess.get();
//
設(shè)置線程變量tLocalsess為空
tLocalsess.set(
null
);
try
{
//
關(guān)閉session
if
(session
!=
null
&&
session.isOpen())
{
session.close();
}
}
catch
(HibernateException e)
{
throw
new
InfrastructureException(e);
}
}
//
事物處理
public
static
void
beginTransaction()
{
//
從線程變量tLocaltx中取得事物管理對象Transaction
Transaction tx
=
(Transaction) tLocaltx.get();
try
{
//
如果為空就從session中創(chuàng)建一個tx
if
(tx
==
null
)
{
tx
=
currentSession().beginTransaction();
tLocaltx.set(tx);
}
}
catch
(HibernateException e)
{
throw
new
InfrastructureException(e);
}
}
//
提交事物
public
static
void
commitTransaction()
{
//
取得事物
Transaction tx
=
(Transaction) tLocaltx.get();
try
{
//
如果不為空就提交
if
(tx
!=
null
&&
!
tx.wasCommitted()
&&
!
tx.wasRolledBack())
tx.commit();
tLocaltx.set(
null
);
}
catch
(HibernateException e)
{
throw
new
InfrastructureException(e);
}
}
//
事物回滾
public
static
void
rollbackTransaction()
{
//
取得tx事物
Transaction tx
=
(Transaction) tLocaltx.get();
try
{
//
將變量清空
tLocaltx.set(
null
);
if
(tx
!=
null
&&
!
tx.wasCommitted()
&&
!
tx.wasRolledBack())
{
//
事物回滾
tx.rollback();
}
}
catch
(HibernateException e)
{
throw
new
InfrastructureException(e);
}
}
//
取得session
private
static
Session openSession()
throws
HibernateException
{
return
getSessionFactory
().openSession();
}
//
取得sessionFactory
private
static
SessionFactory
getSessionFactory
()
throws
HibernateException
{
return
SingletonSessionFactory.getInstance();
}
}
filter的代碼:
public
class
HibernateSessionCloser
implements
Filter
{
protected
FilterConfig filterConfig
=
null
;
public
void
init(FilterConfig filterConfig)
throws
ServletException
{
this
.filterConfig
=
filterConfig;
}
public
void
destroy()
{
this
.filterConfig
=
null
;
}
public
void
doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws
IOException, ServletException
{
try
{
chain.doFilter(request, response);
}
finally
{
try
{
HibernateSessionUtil.commitTransaction();
}
catch
(InfrastructureException e)
{
HibernateSessionUtil.rollbackTransaction();
}
finally
{
HibernateSessionUtil.closeSession();
}
}
}
}
然后在web.xml配置filter就可以使用了.
posted on 2008-05-18 20:36
henry1451
閱讀(630)
評論(0)
編輯
收藏
所屬分類:
Hibernate技術(shù)
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
Hibernate的cache管理(轉(zhuǎn))
Hibernate多表關(guān)聯(lián)
Hibernate使用criteria進行查詢
Hibernate中session的管理
在Hibernate應(yīng)用中如何處理批量更新和批量刪除(轉(zhuǎn))
Copyright ©2025 henry1451 Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
mm1313亚洲精品无码又大又粗
|
99精品在线免费观看
|
免费看成人AA片无码视频羞羞网
|
无码精品一区二区三区免费视频
|
亚洲成a人片在线观看无码
|
亚洲xxxx视频
|
免费看黄视频网站
|
亚洲妇女熟BBW
|
午夜毛片不卡免费观看视频
|
亚洲国产精品无码专区影院
|
成人影片一区免费观看
|
亚洲色婷婷一区二区三区
|
亚洲熟妇久久精品
|
国产色爽免费视频
|
一级毛片a免费播放王色电影
|
亚洲综合无码一区二区痴汉
|
成人免费毛片观看
|
羞羞的视频在线免费观看
|
国产亚洲精品无码拍拍拍色欲
|
亚洲视频一区二区在线观看
|
日韩免费无码一区二区三区
|
亚洲欧洲精品一区二区三区
|
亚洲精品免费网站
|
日本免费一本天堂在线
|
jizz免费在线观看
|
免费一级毛片正在播放
|
亚洲影院天堂中文av色
|
国产一级做a爱免费视频
|
亚洲第一第二第三第四第五第六
|
一级黄色片免费观看
|
日韩一级免费视频
|
一级毛片在线免费视频
|
亚洲国产精品lv
|
日韩插啊免费视频在线观看
|
亚洲最大福利视频
|
亚洲国产精品嫩草影院久久
|
久久精品国产亚洲av成人
|
蜜臀AV免费一区二区三区
|
精品亚洲国产成人av
|
免费a级黄色毛片
|
99爱在线精品视频免费观看9
|