package hibernate3;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAVA WEB从入门到精通 随书源码 (716个子文件)
UserAction.class 3KB
UploadListAction.class 3KB
UploadArrayAction.class 3KB
UploadAction.class 3KB
HibernateSessionFactory.class 3KB
DataBaseMock.class 2KB
JasperAction.class 2KB
LogonAction.class 2KB
TimerFilter.class 2KB
FirstHibernate.class 2KB
CreateUserAction2.class 2KB
SendmailResult.class 2KB
RegiserAction.class 2KB
LoginAction.class 2KB
User.class 1KB
Person.class 1KB
User.class 1KB
HelloServlet.class 1KB
TelConverter.class 1KB
Person.class 1KB
LogonInterceptor.class 1KB
SimpleTag.class 1KB
Logon.class 1KB
CreateUserAction1.class 1KB
QuizAction.class 991B
CreateUserAction.class 944B
ChatAction.class 944B
CheckLoginInterceptor.class 904B
Address.class 882B
CheckLoginInterceptor.class 869B
TutorialSupport.class 834B
TutorialSupport.class 834B
HelloReader.class 786B
HelloReader.class 785B
ChatServiceImpl.class 759B
ExportDB.class 743B
OnlineCounterListener.class 705B
TelAction.class 698B
Tel.class 698B
S_Action.class 609B
S_Action.class 606B
OnlineCounter.class 592B
SimpleBean.class 582B
TestComparator.class 547B
Action.class 521B
LoginAction.class 474B
ChatService.class 215B
.classpath 3KB
.classpath 2KB
.classpath 2KB
.classpath 1KB
.classpath 915B
.classpath 773B
.classpath 757B
.classpath 757B
.classpath 755B
.classpath 755B
.classpath 508B
.classpath 496B
.classpath 451B
.classpath 437B
.classpath 437B
.classpath 437B
.classpath 437B
.classpath 437B
.classpath 415B
.classpath 362B
org.eclipse.wst.common.component 555B
org.eclipse.wst.common.component 555B
org.eclipse.wst.common.component 549B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
styles.css 1KB
styles.css 1KB
styles.css 1KB
optiontransferselect.ftl 14KB
form-close-validate.ftl 6KB
doubleselect.ftl 5KB
autocompleter.ftl 5KB
form-close.ftl 4KB
tree.ftl 4KB
form-close.ftl 4KB
inputtransferselect.ftl 4KB
autocompleter.ftl 4KB
checkbox.ftl 4KB
共 716 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
漫笔闲秋
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页