黑靈客棧
黑靈的沒啥技術含量的技術博客! -> 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
黑靈
閱讀(2244)
評論(0)
編輯
收藏
所屬分類:
ORM
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
Hibernate3中取得多層數據的所產生的n+1 selects問題的解決。
Hibernate3中的更新與刪除
Could not initialize proxy - the owning Session was closed
iBatis實戰(zhàn)小結!
一個困擾了我好幾天的Hibernate+Spring的問題,算是解決了吧!
對于昨天User -- Friend關系的解決!
這樣的表結構Hibernate映射應該是什么樣子?
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)
客戶端技術(1)
(rss)
正則表達式
(rss)
腳本語言(22)
(rss)
文章分類
(1)
Tomcat(1)
(rss)
博客同道
GENOW
(rss)
遠離塵囂
常用資源
Martin Fowler
Martin Fowler的文章
最新評論
1.?re: 關于spring-mvc的InitBinder注解的參數
這個里面User與User_是兩個不同的類
--mmocake
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 黑靈
主站蜘蛛池模板:
日韩亚洲产在线观看
|
毛片免费观看网站
|
污视频网站免费在线观看
|
久久精品九九亚洲精品
|
亚洲伊人成无码综合网
|
黄色a三级免费看
|
亚洲国产精品成人综合色在线婷婷
|
亚洲欧洲日产国码高潮αv
|
成人毛片免费视频
|
很黄很色很刺激的视频免费
|
人人揉揉香蕉大免费不卡
|
一级做受视频免费是看美女
|
亚洲精品色播一区二区
|
精品亚洲AV无码一区二区
|
亚洲视频一区调教
|
亚洲电影一区二区
|
国产AV无码专区亚洲精品
|
亚洲一区无码精品色
|
亚洲国产精品毛片av不卡在线
|
国产精品无码一二区免费
|
成人免费毛片观看
|
最近中文字幕mv免费高清视频7
|
亚洲人成毛片线播放
|
老司机亚洲精品影院
|
亚洲国产精品一区二区久久hs
|
中文字幕亚洲日韩无线码
|
国产成人精品久久亚洲
|
亚洲偷自拍拍综合网
|
亚洲中文字幕丝袜制服一区
|
吃奶摸下高潮60分钟免费视频
|
日韩电影免费在线
|
国产精品成人四虎免费视频
|
四虎在线播放免费永久视频
|
免费看男女下面日出水视频
|
啊v在线免费观看
|
亚洲国产精品尤物YW在线观看
|
亚洲高清成人一区二区三区
|
亚洲日本在线观看视频
|
相泽亚洲一区中文字幕
|
亚洲精品无码国产
|
久久亚洲AV午夜福利精品一区
|