黑靈客棧
黑靈的沒啥技術含量的技術博客! -> http://zjumty.iteye.com
BlogJava
|
首頁
|
發新隨筆
|
發新文章
|
聯系
|
聚合
|
管理
隨筆: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
發表于 2005-04-26 08:26
黑靈
閱讀(2245)
評論(0)
編輯
收藏
所屬分類:
ORM
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
Hibernate3中取得多層數據的所產生的n+1 selects問題的解決。
Hibernate3中的更新與刪除
Could not initialize proxy - the owning Session was closed
iBatis實戰小結!
一個困擾了我好幾天的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 黑靈
主站蜘蛛池模板:
久久久久亚洲AV无码永不
|
97人妻精品全国免费视频
|
亚洲国产精品国自产电影
|
亚洲成av人片天堂网老年人
|
曰批全过程免费视频播放网站
|
中文在线免费视频
|
免费人成网站永久
|
亚洲精品V天堂中文字幕
|
亚洲第一页在线视频
|
亚洲va久久久噜噜噜久久狠狠
|
国产精品亚洲玖玖玖在线观看
|
99久久国产亚洲综合精品
|
精品日韩亚洲AV无码一区二区三区
|
亚洲熟伦熟女新五十路熟妇
|
香蕉高清免费永久在线视频
|
国产免费久久精品99re丫y
|
三年片在线观看免费观看大全动漫
|
西西人体免费视频
|
一级一级一片免费高清
|
婷婷国产偷v国产偷v亚洲
|
亚洲精品国产suv一区88
|
亚洲成_人网站图片
|
亚洲AV无码国产精品色
|
亚洲国产日韩在线
|
亚洲码在线中文在线观看
|
亚洲黄色在线视频
|
亚洲欧洲日产韩国在线
|
亚洲精品无码久久毛片波多野吉衣
|
亚洲av无码一区二区三区不卡
|
亚洲精品你懂的在线观看
|
亚洲精品一品区二品区三品区
|
中文在线观看永久免费
|
青青操免费在线视频
|
免费一级毛片无毒不卡
|
久久久久久久久久国产精品免费
|
免费人成黄页在线观看日本
|
国产成人一区二区三区视频免费
|
国产永久免费高清在线
|
无码成A毛片免费
|
99久久免费精品高清特色大片
|
99re6免费视频
|