package com.demo;
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设计模式每章源代码.zip (1751个子文件)
202fad98a1540011186fae81caa5b03f 974B
204da898a1540011186fae81caa5b03f 974B
20e8f18ba1540011186fae81caa5b03f 681B
302ded8ba1540011186fae81caa5b03f 681B
307b3dad505400111a64fdedc02706e2 3KB
307c9f3fa1540011186fae81caa5b03f 231B
40669c98a1540011186fae81caa5b03f 1002B
5046e18ba1540011186fae81caa5b03f 229B
5077983fa1540011186fae81caa5b03f 231B
606b1bc34b540011110591437a34fefd 987B
90eb4a5ba1540011186fae81caa5b03f 252B
c05ceb2b995400111af3a16a75aefce0 395B
HibernateSessionFactory.class 4KB
DESEncrypt.class 3KB
LoginBean.class 2KB
LoginValidatorB.class 2KB
LoginValidatorA.class 2KB
AbstractState.class 2KB
MainClass.class 2KB
Student.class 2KB
HighState.class 2KB
XMLUtilPen.class 2KB
UserInfoDTO.class 2KB
MainClass.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
ChatGroup.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
XMLUtil.class 2KB
MiddleState.class 2KB
ForumAccount.class 2KB
Calculator.class 2KB
Member.class 2KB
Email.class 1KB
XMLUtilTV.class 1KB
Client.class 1KB
PrimaryState.class 1KB
SkyworthTelevision$SkyworthIterator.class 1KB
Client.class 1KB
Client.class 1KB
Client.class 1KB
DeviceFactory.class 1KB
DeviceFactory.class 1KB
DynamicProxy.class 1KB
NewCollection$NewIterator.class 1KB
PermissionProxy.class 1KB
Client.class 1KB
Client.class 1KB
Client.class 1KB
IteratorDemo.class 1KB
QuickSort.class 1KB
TCLTelevision$TCLIterator.class 1KB
Switch.class 1KB
Client.class 1KB
Client.class 1KB
AdvancedCipher.class 1KB
GeneralManager.class 1KB
ViceGeneralManager.class 1KB
CalculatorForm.class 1KB
GeneralSwitchFacade.class 1KB
ComplexCipher.class 1KB
Client.class 1KB
Director.class 1KB
Manager.class 1KB
Client.class 1KB
Client.class 1KB
Memento.class 1KB
MainClass$BtListener.class 1KB
BuyBasket.class 1KB
IdentityCardNo.class 1023B
SimpleCipher.class 1019B
Client.class 1005B
Client.class 990B
Caesar.class 962B
Email.class 956B
Client.class 949B
PrototypeManager.class 944B
DiamondMember.class 942B
Plate.class 934B
Client.class 930B
TVFactory.class 930B
CommonMember.class 918B
Saler.class 915B
RealPermission.class 915B
SubFrame.class 912B
Client.class 908B
Switch.class 905B
Light.class 902B
PrintSpoolerSingleton.class 894B
Hub.class 893B
Client.class 889B
NewCipher.class 883B
Client.class 881B
共 1751 条
- 1
- 2
- 3
- 4
- 5
- 6
- 18
资源评论
等天晴i
- 粉丝: 3390
- 资源: 10万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功