package com.etoak.factory;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
struts2+ajax 发送jquery请求分页,返回josn对象,引用js自动解析
共29个文件
jar:7个
xml:7个
java:4个
4星 · 超过85%的资源 需积分: 9 129 下载量 157 浏览量
2013-01-18
11:22:33
上传
评论 2
收藏 3.69MB RAR 举报
温馨提示
struts2+ajax 发送jquery请求分页,返回josn对象,引用js自动解析,有jar包
资源推荐
资源详情
资源评论
收起资源包目录
page.rar (29个子文件)
page
.project 1KB
.mymetadata 285B
src
struts.xml 389B
hibernate.cfg.xml 882B
com
etoak
action
PageAction.java 2KB
factory
HibernateSessionFactory.java 3KB
dao
DaoImpl.java 873B
bean
News.hbm.xml 917B
News.java 1KB
WebRoot
WEB-INF
classes
struts.xml 389B
hibernate.cfg.xml 882B
com
etoak
action
PageAction.class 2KB
factory
HibernateSessionFactory.class 2KB
dao
DaoImpl.class 1KB
bean
News.hbm.xml 917B
News.class 1KB
lib
ognl-2.6.11.jar 164KB
struts2-core-2.0.14.jar 2.22MB
mysql.jar 409KB
xwork-2.0.7.jar 449KB
struts2-jsonplugin-0.30.jar 43KB
commons-logging-1.0.4.jar 37KB
freemarker-2.3.8.jar 784KB
web.xml 643B
index.jsp 3KB
js
jquery-1.4.3.js 176KB
META-INF
MANIFEST.MF 39B
.myeclipse
.myhibernatedata 482B
.classpath 1016B
共 29 条
- 1
晓哥的马子
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页