package 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 threadLocal = new ThreadLocal();
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
用ssh框架实现的简单的登录系统 (117个子文件)
HibernateSessionFactory.class 2KB
LoginAction.class 2KB
UserDAOImpl.class 1KB
LoginForm.class 1KB
Test.class 1KB
Users.class 951B
UsersDAO.class 148B
.classpath 433B
hibernate3.jar 2.15MB
jasperreports-2.0.5.jar 1.87MB
aspectjweaver.jar 1.82MB
iText-2.0.7.jar 1.15MB
xerces-2.6.2.jar 987KB
poi-3.0.1.jar 888KB
freemarker.jar 843KB
jxl.jar 704KB
struts.jar 537KB
spring-beans.jar 473KB
javassist.jar 460KB
spring-context.jar 458KB
antlr-2.7.6.jar 433KB
jboss-archive-browsing.jar 404KB
spring-webmvc.jar 393KB
velocity-1.5.jar 383KB
log4j-1.2.15.jar 383KB
spring-orm.jar 364KB
log4j-1.2.11.jar 342KB
spring-aop.jar 318KB
cglib-nodep-2.1_3.jar 317KB
spring-jdbc.jar 310KB
dom4j-1.6.1.jar 307KB
commons-httpclient.jar 298KB
spring-core.jar 279KB
msbase.jar 278KB
msbase.jar 278KB
cglib-2.1.3.jar 276KB
hibernate-annotations.jar 259KB
spring-tx.jar 222KB
jaxen-1.1-beta-7.jar 222KB
ehcache-1.2.3.jar 203KB
spring-web.jar 183KB
commons-collections-2.1.1.jar 171KB
spring-webmvc-portlet.jar 146KB
xml-apis.jar 121KB
hibernate-entitymanager.jar 114KB
velocity-tools-view-1.4.jar 113KB
aspectjrt.jar 112KB
jaas.jar 102KB
commons-io.jar 82KB
persistence.jar 69KB
hibernate-commons-annotations.jar 65KB
mssqlserver.jar 63KB
mssqlserver.jar 63KB
hibernate-validator.jar 60KB
commons-logging.jar 59KB
msutil.jar 58KB
msutil.jar 58KB
commons-fileupload.jar 52KB
ejb3-persistence.jar 49KB
commons-codec.jar 46KB
commons-logging-1.0.4.jar 37KB
spring-webmvc-struts.jar 36KB
commons-attributes-api.jar 35KB
asm-util-2.2.3.jar 34KB
asm-2.2.3.jar 34KB
commons-attributes-compiler.jar 29KB
asm.jar 26KB
spring-aspects.jar 20KB
asm-attrs.jar 16KB
portlet-api.jar 15KB
asm-commons-2.2.3.jar 15KB
jta.jar 9KB
spring-tomcat-weaver.jar 8KB
aspectjlib.jar 8KB
jdbc2_0-stdext.jar 7KB
spring-agent.jar 5KB
aopalliance.jar 4KB
HibernateSessionFactory.java 3KB
LoginForm.java 2KB
LoginAction.java 1KB
Test.java 603B
UserDAOImpl.java 598B
Users.java 516B
UsersDAO.java 95B
index.jsp 834B
fail.jsp 822B
login.jsp 534B
succ.jsp 465B
.struts-config.mex 1KB
MANIFEST.MF 39B
.myhibernatedata 556B
.mymetadata 288B
.mystrutsdata 250B
org.eclipse.jdt.core.prefs 334B
.project 2KB
ApplicationResources.properties 76B
ApplicationResources.properties 76B
.springBeans 420B
user.sql 63B
struts-html.tld 72KB
共 117 条
- 1
- 2
hchjjun
- 粉丝: 48
- 资源: 45
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页