package hl.util;
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();
}
}
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 {
System.out.println("------->");
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();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
EShop.rar (77个子文件)
eshop.sql 5KB
EShop
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 995B
lib
jboss-transaction-api_1.1_spec-1.0.0.Final.jar 11KB
struts2-core-2.3.4.1.jar 765KB
dom4j-1.6.1.jar 307KB
javassist-3.11.0.GA.jar 600KB
antlr-2.7.7.jar 435KB
lib.rar 11.12MB
commons-io-2.0.1.jar 156KB
freemarker-2.3.19.jar 909KB
jboss-logging-3.1.0.GA.jar 59KB
hibernate-core-4.1.9.Final.jar 4.3MB
ognl-3.0.5.jar 222KB
xwork-core-2.3.4.1.jar 614KB
javassist-3.17.1-GA.jar 695KB
commons-fileupload-1.2.2.jar 58KB
hibernate-jpa-2.0-api-1.0.1.Final.jar 100KB
jstl-1.2.jar 405KB
hibernate-commons-annotations-4.0.1.Final.jar 79KB
commons-lang3-3.1.jar 308KB
mysql-connector-java-5.1.22-bin.jar 813KB
hibernate3.jar 1.6MB
index.jsp 108B
jsp
cart.jsp 2KB
login.jsp 848B
index.jsp 2KB
classinfo.jsp 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 469B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.ws.service.policy.prefs 87B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
src
struts.xml 1KB
hibernate.cfg.xml 1KB
hl
dao
UserDao.java 303B
ProductsDaoImp.java 2KB
ProductsDao.java 311B
UserDaoImp.java 2KB
action
Login-validation.xml 481B
Login.java 2KB
Detail.java 640B
Logout.java 432B
CartAction.java 2KB
domain
Items.java 582B
Products.hbm.xml 621B
Cart.java 946B
Users.java 809B
Users.hbm.xml 589B
Products.java 966B
test
userDao.java 586B
productDao.java 835B
util
HibernateSessionFactory.java 2KB
.project 1KB
.classpath 765B
build
classes
struts.xml 1KB
hibernate.cfg.xml 1KB
hl
dao
UserDaoImp.class 3KB
UserDao.class 352B
ProductsDaoImp.class 3KB
ProductsDao.class 416B
action
Login-validation.xml 481B
CartAction.class 3KB
Logout.class 816B
Detail.class 1KB
Login.class 2KB
domain
Cart.class 2KB
Products.hbm.xml 621B
Users.hbm.xml 589B
Users.class 1KB
Items.class 966B
Products.class 1KB
test
userDao.class 1KB
productDao.class 1KB
util
HibernateSessionFactory.class 2KB
共 77 条
- 1
资源评论
- 京都浪人2014-04-16一直也没配置好,估计我方法问题。
- Y_recollect2015-05-04还可以吧,可以帮到自己一些
teabottleF
- 粉丝: 1
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功