黑灵客栈
黑灵的没啥技术含量的技术博客! -> 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
黑灵
阅读(2239)
评论(0)
编辑
收藏
所属分类:
ORM
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
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