package util;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
分页代码 重分页代码
共64个文件
jar:31个
xml:7个
class:5个
需积分: 6 49 下载量 147 浏览量
2008-10-05
11:36:58
上传
评论
收藏 8.75MB RAR 举报
温馨提示
分页 ssh分页 重用分页代码 一个工程里只需要一个公用的分页类
资源推荐
资源详情
资源评论
收起资源包目录
ssh_page.rar (64个子文件)
ssh_page
.project 1KB
.mymetadata 297B
WebRoot
WEB-INF
struts-tiles.tld 8KB
.struts-config.mex 490B
web.xml 1KB
struts-config.xml 591B
struts-bean.tld 9KB
struts-nested.tld 70KB
struts-html.tld 72KB
struts-logic.tld 14KB
validator-rules.xml 13KB
lib
connector.jar 18KB
oscache-2.1.jar 112KB
jboss-cache.jar 364KB
log4j-1.2.11.jar 343KB
c3p0-0.9.0.jar 477KB
antlr-2.7.6rc1.jar 427KB
jta.jar 8KB
hibernate3.jar 1.87MB
jaxen-1.1-beta-7.jar 223KB
commons-logging-1.0.4.jar 37KB
concurrent-1.3.2.jar 166KB
xml-apis.jar 121KB
msbase.jar 280KB
jdbc2_0-stdext.jar 7KB
commons-collections-2.1.1.jar 170KB
mssqlserver.jar 66KB
jboss-common.jar 573KB
jaas.jar 101KB
ehcache-1.1.jar 46KB
swarmcache-1.0rc2.jar 30KB
dom4j-1.6.1.jar 303KB
proxool-0.8.3.jar 459KB
asm.jar 26KB
jacc-1_0-fr.jar 24KB
jgroups-2.2.8.jar 1.53MB
msutil.jar 58KB
xerces-2.6.2.jar 972KB
jboss-system.jar 218KB
cglib-2.1.3.jar 276KB
jboss-jmx.jar 578KB
asm-attrs.jar 17KB
classes
dao
UserDao.class 684B
com
yourcompany
struts
action
SearchAction.class 2KB
ApplicationResources.properties 91B
util
SplitData.class 2KB
HibernateSessionFactory.class 3KB
hibernate.cfg.xml 852B
pojo
UserInfo.class 2KB
UserInfo.hbm.xml 1KB
index.jsp 829B
META-INF
MANIFEST.MF 39B
userinfo.jsp 2KB
.myeclipse
src
dao
UserDao.java 301B
com
yourcompany
struts
action
SearchAction.java 1KB
ApplicationResources.properties 91B
util
SplitData.java 2KB
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 852B
pojo
UserInfo.java 1KB
UserInfo.hbm.xml 1KB
.mystrutsdata 262B
.classpath 3KB
.myhibernatedata 505B
共 64 条
- 1
资源评论
jiedamu
- 粉丝: 8
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功