package ch12.dao.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码(第二版) (101个子文件)
UserDAOHibernateImpl.class 4KB
HibernateSessionFactory.class 2KB
SampleTest.class 2KB
LoginAction.class 2KB
RegisterAction.class 2KB
SessionFactory1.class 2KB
AbstractUser.class 2KB
LoginActionTest.class 2KB
AuthenticationInterceptor.class 1KB
TestUserDAO.class 1KB
RegisterActionTest.class 722B
User.class 706B
AllTests.class 637B
TestSuccess.class 451B
TestFailure.class 437B
IUserDAO.class 240B
.classpath 1KB
.classpath 422B
Entries 314B
Entries 291B
Entries 194B
Entries 168B
Entries 67B
Entries 64B
Entries 50B
Entries 45B
Entries 12B
login_zh_CN.ftl 507B
login_en_US.ftl 470B
struts2-core-2.0.9.jar 2.1MB
freemarker-2.3.8.jar 784KB
mysql-connector-java-5.0.8-bin.jar 528KB
xwork-2.0.4.jar 445KB
dbunit-2.2.jar 321KB
ognl-2.6.11.jar 164KB
easymock-2.0.jar 59KB
HibernateSessionFactory.java 3KB
LoginAction.java 3KB
UserDAOHibernateImpl.java 3KB
RegisterAction.java 3KB
SessionFactory1.java 3KB
SampleTest.java 2KB
AbstractUser.java 2KB
LoginActionTest.java 2KB
AuthenticationInterceptor.java 1KB
RegisterActionTest.java 1KB
TestUserDAO.java 645B
User.java 568B
AllTests.java 359B
IUserDAO.java 190B
TestSuccess.java 186B
TestFailure.java 176B
register.jsp 694B
index.jsp 602B
success.jsp 182B
welcome.jsp 144B
MANIFEST.MF 39B
MANIFEST.MF 39B
.myhibernatedata 401B
.mymetadata 321B
.mymetadata 297B
.project 1KB
.project 1018B
log4j.properties 886B
log4j.properties 886B
Repository 39B
Repository 34B
Repository 33B
Repository 30B
Repository 27B
Repository 26B
Repository 24B
Repository 18B
Repository 15B
Repository 14B
Root 43B
Root 43B
Root 43B
Root 43B
Root 43B
Root 43B
Root 43B
Root 43B
Root 43B
Root 43B
struts.xml 2KB
struts.xml 2KB
RegisterAction-validation.xml 2KB
RegisterAction-validation.xml 2KB
hibernate.cfg.xml 1002B
hibernate.cfg.xml 1002B
LoginAction-validation.xml 978B
LoginAction-validation.xml 978B
web.xml 728B
User.hbm.xml 693B
User.hbm.xml 693B
a.xml 338B
a.xml 338B
web.xml 286B
userdataset.xml 115B
共 101 条
- 1
- 2
资源评论
- zhongguoxiang22012-05-19还不错了,只是代码有错。
itcoco05
- 粉丝: 2
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功