package com.dzy.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 final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Struts2+Hibernate实现登陆注册增删改查.rar (64个子文件)
Struts2+Hibernate登陆注册增删改查
admin
a.sql 1KB
.project 2KB
.settings
.jsdtscope 500B
org.eclipse.wst.common.component 453B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 288B
WebRoot
zhuce.jsp 2KB
WEB-INF
web.xml 685B
lib
hibernate-commons-annotations.jar 65KB
hibernate-annotations.jar 273KB
jta-1.1.jar 13KB
hibernate3.jar 2.31MB
slf4j-log4j12-1.5.8.jar 9KB
cglib-2.2.jar 272KB
log4j-1.2.14.jar 359KB
ehcache-1.2.3.jar 203KB
ejb3-persistence.jar 49KB
commons-collections-3.1.jar 546KB
hibernate-entitymanager.jar 116KB
javassist-3.9.0.GA.jar 583KB
dom4j-1.6.1.jar 307KB
slf4j-api-1.5.8.jar 23KB
hibernate-validator.jar 61KB
antlr-2.7.6.jar 433KB
classes
com
dzy
dao
UserDAO.class 2KB
EmDAO.class 2KB
action
Showaction.class 2KB
Modifyaction.class 2KB
model
Emmodel.class 2KB
Usermodel.class 992B
Emmodel.hbm.xml 807B
Usermodel.hbm.xml 641B
service
Emservice.class 1KB
Userservice.class 740B
util
Hibernateutil.class 1KB
HibernateSessionFactory.class 3KB
struts.xml 1KB
hibernate.cfg.xml 981B
index.jsp 1KB
META-INF
MANIFEST.MF 36B
add.jsp 2KB
error.jsp 926B
show.jsp 2KB
update.jsp 2KB
.myeclipse
src
com
dzy
dao
UserDAO.java 873B
EmDAO.java 1KB
action
Showaction.java 1KB
Modifyaction.java 937B
model
Emmodel.java 903B
Usermodel.java 534B
Emmodel.hbm.xml 807B
Usermodel.hbm.xml 641B
service
Userservice.java 298B
Emservice.java 458B
util
Hibernateutil.java 642B
HibernateSessionFactory.java 3KB
struts.xml 1KB
hibernate.cfg.xml 981B
.mystrutsdata 240B
.classpath 2KB
.myhibernatedata 555B
共 64 条
- 1
资源评论
LY文库
- 粉丝: 152
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功