package com.SessionFactory.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 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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计:2016毕业设计_航空订票系统的设计与实现.zip (90个子文件)
毕业设计:2016毕业设计_航空订票系统的设计与实现
SSH_FlightTicket
.classpath 5KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 364B
.jsdtscope 500B
org.eclipse.wst.common.component 486B
org.springframework.ide.eclipse.core.prefs 186B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.jsdt.ui.superType.container 49B
.mymetadata 327B
.gitattributes 395B
src
struts.xml 207B
hibernate.cfg.xml 926B
applicationContext.xml 553B
log4j.properties 212B
com
SessionFactory
Hibernate
HibernateSessionFactory.java 3KB
.git
index 9KB
HEAD 23B
refs
heads
master 41B
tags
remotes
origin
master 41B
objects
pack
pack-8d75f2b3d661a878a986abc156abb0441e98ed52.pack 13.38MB
pack-8d75f2b3d661a878a986abc156abb0441e98ed52.idx 4KB
info
FETCH_HEAD 123B
logs
HEAD 130B
refs
heads
master 130B
remotes
origin
master 144B
hooks
config 260B
branches
.myhibernatedata 583B
WebRoot
WEB-INF
classes
struts.xml 207B
hibernate.cfg.xml 926B
applicationContext.xml 553B
log4j.properties 212B
com
SessionFactory
Hibernate
HibernateSessionFactory.class 3KB
lib
org.springframework.aspects-3.0.5.RELEASE.jar 35KB
org.springframework.web.servlet-3.0.5.RELEASE.jar 409KB
org.springframework.orm-3.0.5.RELEASE.jar 326KB
commons-dbcp.jar 119KB
com.springsource.org.apache.commons.pool-1.5.3.jar 95KB
com.springsource.org.apache.log4j-1.2.15.jar 387KB
com.springsource.org.aopalliance-1.0.0.jar 5KB
dom4j-1.6.1.jar 307KB
mysql-connector-java-5.1.38-bin.jar 961KB
hibernate-entitymanager.jar 116KB
com.springsource.org.apache.commons.lang-2.4.0.jar 258KB
persistence.jar 69KB
ehcache-1.2.3.jar 203KB
org.springframework.jdbc-3.0.5.RELEASE.jar 377KB
org.springframework.web.struts-3.0.5.RELEASE.jar 31KB
ejb3-persistence.jar 49KB
org.springframework.expression-3.0.5.RELEASE.jar 166KB
org.springframework.instrument-3.0.5.RELEASE.jar 2KB
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 1.57MB
xapool.jar 90KB
org.springframework.oxm-3.0.5.RELEASE.jar 60KB
hibernate3.jar 2.31MB
slf4j-api-1.5.8.jar 23KB
com.springsource.net.sf.cglib-2.2.0.jar 319KB
com.springsource.org.apache.commons.httpclient-3.1.0.jar 301KB
org.springframework.beans-3.0.5.RELEASE.jar 542KB
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 603KB
org.springframework.instrument.tomcat-3.0.5.RELEASE.jar 6KB
org.springframework.core-3.0.5.RELEASE.jar 373KB
org.springframework.asm-3.0.5.RELEASE.jar 52KB
org.springframework.web-3.0.5.RELEASE.jar 386KB
jta-1.1.jar 13KB
log4j-1.2.14.jar 359KB
hibernate-validator.jar 61KB
org.springframework.web.portlet-3.0.5.RELEASE.jar 171KB
jotm.jar 121KB
commons-collections-3.1.jar 546KB
org.springframework.transaction-3.0.5.RELEASE.jar 226KB
hibernate-annotations.jar 273KB
antlr-2.7.6.jar 433KB
com.springsource.org.apache.commons.logging-1.1.1.jar 60KB
org.springframework.context-3.0.5.RELEASE.jar 653KB
javassist-3.9.0.GA.jar 583KB
com.springsource.org.codehaus.jackson.mapper-1.0.0.jar 264KB
org.springframework.aop-3.0.5.RELEASE.jar 314KB
cglib-2.2.jar 272KB
com.springsource.org.apache.commons.fileupload-1.2.0.jar 53KB
hibernate-commons-annotations.jar 65KB
slf4j-log4j12-1.5.8.jar 9KB
web.xml 1KB
spring-form.tld 72KB
spring.tld 17KB
index.jsp 834B
META-INF
MANIFEST.MF 39B
.springBeans 462B
.project 2KB
.gitignore 696B
.mystrutsdata 247B
共 90 条
- 1
资源评论
九转成圣
- 粉丝: 5382
- 资源: 2962
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 分享:qt的Yolo图像处理模块(pri)
- 施工人员检测54-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma数据集合集.rar
- 基于Java + Mysql 实现的在线考试系统课程设计
- java小游戏,飞机大战0.1版本.zip
- 深度学习 python 特征标记
- GSD-file-for-DME5000-PROFIBUS-DC0000868.ZIP Firmware V1.5, V1.7, V2.0 SICK069D
- google-chrome-stable-124.0.6367.118-1.x86-64.rpm
- 施工人员检测53-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- scratch简单小游戏(贪吃蛇)
- freeplane笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功