package org.easybooks.test.factory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* 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 final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry;
static {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} 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();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} 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 hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JavaEE基础实例源程序
共473个文件
jar:87个
xml:84个
java:56个
需积分: 10 3 下载量 120 浏览量
2015-10-26
10:08:54
上传
评论
收藏 34.28MB ZIP 举报
温馨提示
本资料包括Java EE的一些项目源程序代码,可供参考,欢迎下载!
资源推荐
资源详情
资源评论
收起资源包目录
JavaEE基础实例源程序 (473个子文件)
MainServlet.class 3KB
MainAction.class 3KB
AddServlet.class 3KB
MainAction.class 3KB
MainServlet.class 3KB
HibernateSessionFactory.class 3KB
MainAction.class 3KB
MainAction.class 3KB
MainAction.class 3KB
MainAction.class 3KB
HibernateSessionFactory.class 3KB
HibernateSessionFactory.class 3KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
UploadAction.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
SqlSrvDBConn.class 2KB
RegisterServlet.class 2KB
Student.class 2KB
Test.class 2KB
Kcb.class 2KB
Test.class 2KB
DBConn.class 2KB
Xsb.class 1KB
LyTable.class 1KB
LyTable.class 1KB
Login.class 1KB
Detail.class 1KB
_2_1hello.class 1KB
SaveAction.class 1KB
Person.class 1KB
_2_4hello.class 1KB
Course.class 1KB
UserTable.class 1KB
Room.class 1KB
MyInterceptor.class 1KB
_2_3hello.class 1KB
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
UserTable.class 1011B
_2_2hello.class 927B
Student.class 552B
TestBean.class 538B
Scope.class 496B
.classpath 3KB
.classpath 3KB
.classpath 3KB
.classpath 3KB
.classpath 3KB
.classpath 3KB
.classpath 2KB
.classpath 2KB
.classpath 1KB
.classpath 806B
.classpath 806B
.classpath 803B
.classpath 803B
.classpath 803B
.classpath 803B
.classpath 803B
org.eclipse.wst.common.component 534B
org.eclipse.wst.common.component 534B
org.eclipse.wst.common.component 534B
org.eclipse.wst.common.component 534B
org.eclipse.wst.common.component 534B
org.eclipse.wst.common.component 534B
org.eclipse.wst.common.component 510B
org.eclipse.wst.common.component 501B
org.eclipse.wst.common.component 498B
org.eclipse.wst.common.component 495B
org.eclipse.wst.common.component 489B
org.eclipse.wst.common.component 489B
org.eclipse.wst.common.component 483B
org.eclipse.wst.common.component 483B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
共 473 条
- 1
- 2
- 3
- 4
- 5
资源评论
starain_fxg
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功