黑靈客棧
黑靈的沒啥技術(shù)含量的技術(shù)博客! -> http://zjumty.iteye.com
BlogJava
|
首頁
|
發(fā)新隨筆
|
發(fā)新文章
|
聯(lián)系
|
聚合
|
管理
隨筆:204 文章:2 評論:243 引用:0
caveatemptor中的HibernateUtil
昨天看hibernate in action是找到的,很值得借鑒啊
1
package org.hibernate.auction.persistence;
2
3
import net.sf.hibernate.
*
;
4
import net.sf.hibernate.cfg.Configuration;
5
import org.apache.commons.logging.
*
;
6
import org.hibernate.auction.exceptions.InfrastructureException;
7
8
import javax.naming.
*
;
9
10
/**/
/*
*
11
* Basic Hibernate helper class, handles SessionFactory, Session and Transaction.
12
* <p>
13
* Uses a static initializer for the initial SessionFactory creation
14
* and holds Session and Transactions in thread local variables. All
15
* exceptions are wrapped in an unchecked InfrastructureException.
16
*
17
* @author christian@hibernate.org
18
*/
19
public
class
HibernateUtil
{
20
21
private
static
Log log
=
LogFactory.getLog(HibernateUtil.
class
);
22
23
private
static
Configuration configuration;
24
private
static
SessionFactory sessionFactory;
25
private
static
final ThreadLocal threadSession
=
new
ThreadLocal();
26
private
static
final ThreadLocal threadTransaction
=
new
ThreadLocal();
27
private
static
final ThreadLocal threadInterceptor
=
new
ThreadLocal();
28
29
//
Create the initial SessionFactory from the default configuration files
30
static
{
31
try
{
32
configuration
=
new
Configuration();
33
sessionFactory
=
configuration.configure().buildSessionFactory();
34
//
We could also let Hibernate bind it to JNDI:
35
//
configuration.configure().buildSessionFactory()
36
}
catch
(Throwable ex)
{
37
//
We have to catch Throwable, otherwise we will miss
38
//
NoClassDefFoundError and other subclasses of Error
39
log.error(
"
Building SessionFactory failed.
"
, ex);
40
throw
new
ExceptionInInitializerError(ex);
41
}
42
}
43
44
/**/
/*
*
45
* Returns the SessionFactory used for this static class.
46
*
47
* @return SessionFactory
48
*/
49
public
static
SessionFactory getSessionFactory()
{
50
/**/
/*
Instead of a static variable, use JNDI:
51
SessionFactory sessions = null;
52
try {
53
Context ctx = new InitialContext();
54
String jndiName = "java:hibernate/HibernateFactory";
55
sessions = (SessionFactory)ctx.lookup(jndiName);
56
} catch (NamingException ex) {
57
throw new InfrastructureException(ex);
58
}
59
return sessions;
60
*/
61
return
sessionFactory;
62
}
63
64
/**/
/*
*
65
* Returns the original Hibernate configuration.
66
*
67
* @return Configuration
68
*/
69
public
static
Configuration getConfiguration()
{
70
return
configuration;
71
}
72
73
/**/
/*
*
74
* Rebuild the SessionFactory with the static Configuration.
75
*
76
*/
77
public
static
void
rebuildSessionFactory()
78
throws InfrastructureException
{
79
synchronized(sessionFactory)
{
80
try
{
81
sessionFactory
=
getConfiguration().buildSessionFactory();
82
}
catch
(Exception ex)
{
83
throw
new
InfrastructureException(ex);
84
}
85
}
86
}
87
88
/**/
/*
*
89
* Rebuild the SessionFactory with the given Hibernate Configuration.
90
*
91
* @param cfg
92
*/
93
public
static
void
rebuildSessionFactory(Configuration cfg)
94
throws InfrastructureException
{
95
synchronized(sessionFactory)
{
96
try
{
97
sessionFactory
=
cfg.buildSessionFactory();
98
configuration
=
cfg;
99
}
catch
(Exception ex)
{
100
throw
new
InfrastructureException(ex);
101
}
102
}
103
}
104
105
/**/
/*
*
106
* Retrieves the current Session local to the thread.
107
* <p/>
108
* If no Session is open, opens a new Session for the running thread.
109
*
110
* @return Session
111
*/
112
public
static
Session getSession()
113
throws InfrastructureException
{
114
Session s
=
(Session) threadSession.
get
();
115
try
{
116
if
(s
==
null
)
{
117
log.debug(
"
Opening new Session for this thread.
"
);
118
if
(getInterceptor()
!=
null
)
{
119
log.debug(
"
Using interceptor:
"
+
getInterceptor().getClass());
120
s
=
getSessionFactory().openSession(getInterceptor());
121
}
else
{
122
s
=
getSessionFactory().openSession();
123
}
124
threadSession.
set
(s);
125
}
126
}
catch
(HibernateException ex)
{
127
throw
new
InfrastructureException(ex);
128
}
129
return
s;
130
}
131
132
/**/
/*
*
133
* Closes the Session local to the thread.
134
*/
135
public
static
void
closeSession()
136
throws InfrastructureException
{
137
try
{
138
Session s
=
(Session) threadSession.
get
();
139
threadSession.
set
(
null
);
140
if
(s
!=
null
&&
s.isOpen())
{
141
log.debug(
"
Closing Session of this thread.
"
);
142
s.close();
143
}
144
}
catch
(HibernateException ex)
{
145
throw
new
InfrastructureException(ex);
146
}
147
}
148
149
/**/
/*
*
150
* Start a new database transaction.
151
*/
152
public
static
void
beginTransaction()
153
throws InfrastructureException
{
154
Transaction tx
=
(Transaction) threadTransaction.
get
();
155
try
{
156
if
(tx
==
null
)
{
157
log.debug(
"
Starting new database transaction in this thread.
"
);
158
tx
=
getSession().beginTransaction();
159
threadTransaction.
set
(tx);
160
}
161
}
catch
(HibernateException ex)
{
162
throw
new
InfrastructureException(ex);
163
}
164
}
165
166
/**/
/*
*
167
* Commit the database transaction.
168
*/
169
public
static
void
commitTransaction()
170
throws InfrastructureException
{
171
Transaction tx
=
(Transaction) threadTransaction.
get
();
172
try
{
173
if
( tx
!=
null
&&
!
tx.wasCommitted()
174
&&
!
tx.wasRolledBack() )
{
175
log.debug(
"
Committing database transaction of this thread.
"
);
176
tx.commit();
177
}
178
threadTransaction.
set
(
null
);
179
}
catch
(HibernateException ex)
{
180
rollbackTransaction();
181
throw
new
InfrastructureException(ex);
182
}
183
}
184
185
/**/
/*
*
186
* Commit the database transaction.
187
*/
188
public
static
void
rollbackTransaction()
189
throws InfrastructureException
{
190
Transaction tx
=
(Transaction) threadTransaction.
get
();
191
try
{
192
threadTransaction.
set
(
null
);
193
if
( tx
!=
null
&&
!
tx.wasCommitted()
&&
!
tx.wasRolledBack() )
{
194
log.debug(
"
Tyring to rollback database transaction of this thread.
"
);
195
tx.rollback();
196
}
197
}
catch
(HibernateException ex)
{
198
throw
new
InfrastructureException(ex);
199
}
finally
{
200
closeSession();
201
}
202
}
203
204
/**/
/*
*
205
* Reconnects a Hibernate Session to the current Thread.
206
*
207
* @param session The Hibernate Session to be reconnected.
208
*/
209
public
static
void
reconnect(Session session)
210
throws InfrastructureException
{
211
try
{
212
session.reconnect();
213
threadSession.
set
(session);
214
}
catch
(HibernateException ex)
{
215
throw
new
InfrastructureException(ex);
216
}
217
}
218
219
/**/
/*
*
220
* Disconnect and return Session from current Thread.
221
*
222
* @return Session the disconnected Session
223
*/
224
public
static
Session disconnectSession()
225
throws InfrastructureException
{
226
227
Session session
=
getSession();
228
try
{
229
threadSession.
set
(
null
);
230
if
(session.isConnected()
&&
session.isOpen())
231
session.disconnect();
232
}
catch
(HibernateException ex)
{
233
throw
new
InfrastructureException(ex);
234
}
235
return
session;
236
}
237
238
/**/
/*
*
239
* Register a Hibernate interceptor with the current thread.
240
* <p>
241
* Every Session opened is opened with this interceptor after
242
* registration. Has no effect if the current Session of the
243
* thread is already open, effective on next close()/getSession().
244
*/
245
public
static
void
registerInterceptor(Interceptor interceptor)
{
246
threadInterceptor.
set
(interceptor);
247
}
248
249
private
static
Interceptor getInterceptor()
{
250
Interceptor interceptor
=
251
(Interceptor) threadInterceptor.
get
();
252
return
interceptor;
253
}
254
255
}
256
257
發(fā)表于 2005-04-26 08:26
黑靈
閱讀(2250)
評論(0)
編輯
收藏
所屬分類:
ORM
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
Hibernate3中取得多層數(shù)據(jù)的所產(chǎn)生的n+1 selects問題的解決。
Hibernate3中的更新與刪除
Could not initialize proxy - the owning Session was closed
iBatis實(shí)戰(zhàn)小結(jié)!
一個困擾了我好幾天的Hibernate+Spring的問題,算是解決了吧!
對于昨天User -- Friend關(guān)系的解決!
這樣的表結(jié)構(gòu)Hibernate映射應(yīng)該是什么樣子?
Hibernate用Mysql的中文問題
caveatemptor中的HibernateUtil
<
2005年4月
>
日
一
二
三
四
五
六
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
1
2
3
4
5
6
7
公告
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
隨筆分類
(175)
AJAX(6)
(rss)
DataBase(5)
(rss)
Do everything with Groovy(4)
(rss)
ExtJS
(rss)
J2EE(29)
(rss)
JSF(4)
(rss)
MStar Utility(2)
(rss)
ORM(9)
(rss)
RIA(3)
(rss)
Server配置(2)
(rss)
Struts(1)
(rss)
Tapestry(1)
(rss)
Unix&Linux(32)
(rss)
wap(1)
(rss)
WebWork(4)
(rss)
不編不知道,一編嚇一跳(17)
(rss)
亂七八糟(32)
(rss)
客戶端技術(shù)(1)
(rss)
正則表達(dá)式
(rss)
腳本語言(22)
(rss)
文章分類
(1)
Tomcat(1)
(rss)
博客同道
GENOW
(rss)
遠(yuǎn)離塵囂
常用資源
Martin Fowler
Martin Fowler的文章
最新評論
1.?re: 關(guān)于spring-mvc的InitBinder注解的參數(shù)
這個里面User與User_是兩個不同的類
--mmocake
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 黑靈
主站蜘蛛池模板:
免费黄色网址入口
|
色偷偷亚洲女人天堂观看欧
|
免费AA片少妇人AA片直播
|
特级毛片爽www免费版
|
亚洲mv国产精品mv日本mv
|
亚洲人JIZZ日本人
|
在线播放免费播放av片
|
伊人久久免费视频
|
久久精品免费大片国产大片
|
亚洲av无码无线在线观看
|
亚洲另类古典武侠
|
亚洲高清资源在线观看
|
亚洲精品午夜国产VA久久成人
|
国产一级淫片免费播放
|
成人a免费α片在线视频网站
|
最近免费中文字幕大全免费
|
国产麻豆一精品一AV一免费
|
中文字幕在线免费观看视频
|
日韩免费高清一级毛片
|
国产精品成人亚洲
|
亚洲欧美日韩中文无线码
|
最新亚洲精品国偷自产在线
|
亚洲丰满熟女一区二区v
|
亚洲国产成人在线视频
|
亚洲经典在线中文字幕
|
亚洲精品偷拍无码不卡av
|
亚洲一区二区在线免费观看
|
久久久无码精品亚洲日韩蜜桃
|
亚洲无人区视频大全
|
亚洲视频中文字幕
|
亚洲尹人香蕉网在线视颅
|
亚洲日本在线看片
|
亚洲色成人网一二三区
|
亚洲美女人黄网成人女
|
亚洲国产电影在线观看
|
亚洲国产精品综合久久20
|
亚洲色偷偷色噜噜狠狠99网
|
亚洲AV无码之国产精品
|
黄色a三级免费看
|
国产成人精品免费视频大全
|
日韩高清在线免费看
|