package com.ssh2.model;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SSH2.rar (60个子文件)
SSH
.project 2KB
.springBeans 462B
.mymetadata 283B
src
struts.xml 431B
applicationContext.xml 630B
hibernate.cfg.xml 918B
com
ssh2
action
StudentAction.java 2KB
model
Student.hbm.xml 668B
HibernateSessionFactory.java 3KB
Student.java 634B
dao
StudentDao.java 944B
WebRoot
student.jsp 989B
WEB-INF
classes
struts.xml 431B
applicationContext.xml 630B
hibernate.cfg.xml 918B
com
ssh2
action
StudentAction.class 3KB
model
Student.hbm.xml 668B
Student.class 839B
HibernateSessionFactory.class 3KB
dao
StudentDao.class 2KB
lib
org.springframework.expression-3.0.1.RELEASE-A.jar 152KB
hibernate-commons-annotations.jar 65KB
javassist-3.9.0.GA.jar 583KB
cglib-2.2.jar 272KB
org.springframework.core-3.0.1.RELEASE-A.jar 347KB
spring.jar 2.81MB
dom4j-1.6.1.jar 307KB
log4j-1.2.14.jar 359KB
xwork-core-2.1.6.jar 1.49MB
org.springframework.beans-3.0.1.RELEASE-A.jar 535KB
org.springframework.context-3.0.1.RELEASE-A.jar 642KB
slf4j-log4j12-1.5.8.jar 9KB
freemarker-2.3.15.jar 859KB
antlr-2.7.6.jar 433KB
org.springframework.asm-3.0.1.RELEASE-A.jar 52KB
commons-collections-3.1.jar 546KB
struts2-core-2.1.8.1.jar 738KB
ehcache-1.2.3.jar 203KB
slf4j-api-1.5.8.jar 23KB
commons-io-1.3.2.jar 86KB
hibernate3.jar 2.31MB
commons-fileupload-1.2.1.jar 56KB
ejb3-persistence.jar 49KB
hibernate-annotations.jar 273KB
mysql-connector-java-5.1.16-bin.jar 768KB
hibernate-entitymanager.jar 116KB
jta-1.1.jar 13KB
com.springsource.org.apache.commons.logging-1.1.1.jar 60KB
com.springsource.org.apache.log4j-1.2.15.jar 387KB
hibernate-validator.jar 61KB
ognl-2.7.3.jar 234KB
web.xml 680B
index.jsp 1KB
META-INF
MANIFEST.MF 36B
ok.jsp 820B
.myeclipse
.myhibernatedata 547B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
.classpath 3KB
共 60 条
- 1
资源评论
os_lee
- 粉丝: 683
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功