package com.test.commons;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Flex+Blazeds+Spring+Hibernet+Mysql整合工程,导入eclipse即可 (119个子文件)
.actionScriptProperties 2KB
HibernateSessionFactory.class 3KB
HelloACT.class 2KB
Hello.class 1KB
.classpath 588B
org.eclipse.wst.common.component 463B
org.eclipse.wst.jsdt.ui.superType.container 49B
history.css 371B
history.css 371B
.flexProperties 202B
index.template.html 6KB
loginpage.html 6KB
Test.html 6KB
historyFrame.html 827B
historyFrame.html 827B
xalan.jar 3MB
hibernate3.jar 2.15MB
hibernate-3.2.1.ga.jar 2.1MB
aspectjweaver.jar 1.47MB
mysql-connector-java-5.1.16-bin.jar 768KB
flex-messaging-core.jar 669KB
org.springframework.context-3.0.1.RELEASE.jar 642KB
javassist-3.11.0.GA.jar 600KB
c3p0-0.9.1.2.jar 596KB
commons-collections-3.2.jar 558KB
org.springframework.beans-3.0.1.RELEASE.jar 535KB
org.springframework.web.struts-3.0.1.RELEASE.jar 524KB
quartz-all-1.8.4.jar 450KB
antlr-2.7.6.jar 433KB
org.springframework.web.servlet-3.0.1.RELEASE.jar 389KB
spring-webmvc-3.0.1.RELEASE.jar 389KB
standard-1.1.2.jar 384KB
org.springframework.jdbc-3.0.1.RELEASE.jar 369KB
log4j-1.2.14.jar 359KB
org.springframework.web-3.0.1.RELEASE.jar 352KB
org.springframework.core-3.0.1.RELEASE.jar 347KB
org.springframework.orm-3.0.1.RELEASE.jar 324KB
com.springsource.net.sf.cglib-2.2.0.jar 319KB
backport-util-concurrent-2.2.jar 319KB
cglib-nodep-2.1_3.jar 317KB
org.springframework.aop-3.0.1.RELEASE.jar 313KB
spring-integration-core-1.0.3.RELEASE.jar 311KB
dom4j-1.6.1.jar 307KB
spring-security-core-3.0.2.RELEASE.jar 298KB
commons-httpclient-3.1.jar 298KB
commons-lang-2.5.jar 273KB
spring-security-web-3.0.2.RELEASE.jar 230KB
org.springframework.transaction-3.0.1.RELEASE.jar 223KB
spring-tx-3.0.1.RELEASE.jar 223KB
org.springframework.test-3.0.1.RELEASE.jar 200KB
commons-beanutils.jar 184KB
org.springframework.jms-3.0.1.RELEASE.jar 179KB
org.springframework.web.portlet-3.0.1.RELEASE.jar 169KB
jackson-core-asl-1.5.2.jar 168KB
spring-security-config-3.0.2.RELEASE.jar 157KB
org.springframework.expression-3.0.1.RELEASE.jar 152KB
json-lib-2.3-jdk15.jar 148KB
javax.servlet-5.1.12.jar 129KB
spring-flex-1.0.3.RELEASE.jar 125KB
commons-dbcp.jar 119KB
aspectjrt-1.5.0.jar 106KB
org.springframework.context.support-3.0.1.RELEASE.jar 98KB
flex-messaging-common.jar 94KB
ezmorph-1.0.3.jar 76KB
flex-messaging-proxy.jar 65KB
commons-pool-1.3.jar 61KB
commons-logging-1.1.1.jar 59KB
org.springframework.oxm-3.0.1.RELEASE.jar 56KB
flex-rds-server.jar 53KB
org.springframework.asm-3.0.1.RELEASE.jar 52KB
org.springframework.asm-3.0.0.RELEASE.jar 52KB
commons-codec-1.3.jar 46KB
com.springsource.org.objectweb.asm-3.1.0.jar 43KB
commons-logging.jar 37KB
org.springframework.aspects-3.0.1.RELEASE.jar 29KB
slf4j-api-1.6.1.jar 25KB
jstl-1.1.2.jar 20KB
flex-messaging-opt.jar 20KB
flex-messaging-remoting.jar 18KB
cfgatewayadapter.jar 15KB
jta.jar 13KB
slf4j-log4j12-1.6.1.jar 10KB
org.springframework.instrument.tomcat-3.0.1.RELEASE.jar 5KB
aopalliance-1.0.jar 4KB
org.springframework.instrument-3.0.1.RELEASE.jar 2KB
HibernateSessionFactory.java 3KB
HelloACT.java 858B
Hello.java 668B
swfobject.js 26KB
swfobject.js 26KB
history.js 25KB
history.js 25KB
.jsdtscope 503B
index.jsp 570B
MANIFEST.MF 39B
Test.mxml 1KB
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 395B
org.eclipse.core.resources.prefs 88B
.project 1KB
共 119 条
- 1
- 2
资源评论
- 很菜的老2015-02-10不错,经过调试,可以运行,值得参考学习。
- zhangershan2014-03-24程序经过调试,能跑起来 建议内附一个说明文档 谢谢分享者
rudylau
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功