下面是小弟学习Hibernate 的一些体会:
package
com.wosdman.test;
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 {
首选对
ThreadLocal
作一个简单的说明
:
它的出现是为更好的服务的多线程
,
使基多线程的应用程序更加完美
.
如果有这样一种需要
:
每当我们开起一个新的线程
,
都需要访问一个变量
,
但这个变量可能会在其它的线程中被改变
,
这时
ThreadLocal
最有效来解这个问题
.
其实
ThreadLocal
并不是一个
Thread
而是一个
Thread local var ,
我觉得应该访问的是本地的私有的静态的
(private static)
变量
,
但下面的这个应用不是的
,session
并不是
private static
的
Session session = (Session)
threadLocal.get();
当每一次调用时
,session
为
null
if (session == null || !session.isOpen())
{
if (sessionFactory == null)
{
//
似乎没有必要
,
因为在前面的
static
代码块中就对它
sessionfactory
初始化了
rebuildSessionFactory();
//
多重判断后来构建一个
sessionfactory,
其实是再次调用
configruation
的
buildsessionfactory() method
完成的
}
session = (sessionFactory
!= null) ? sessionFactory.openSession()
: null;
//
经过上面的判断以后
,
其实我觉得
sessionfactory
不可能为
null,
为了安全起见
,
与程序的逻辑性,这样写真有必要的
,
来构建一个
session
对象
threadLocal.set(session);
//
把这个
session
对象装入
threadlocal
中,其实它是放入一个
Map
中,你可以查看
ThreadLocal
原代码。
}
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();
//
因为
session
只是方法局部变量,在
threadlocal
中有
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;
}
}
分享到:
相关推荐
hiberante3 注解帮助文档hiberante3 注解帮助文档hiberante3 注解帮助文档hiberante3 注解帮助文档hiberante3 注解帮助文档
DAO层中对Hiberante实例,大家交流一下
Hiberante3.jar + API
最新springboot2基础hiberante5完整项目,打包jar,运行jsp,包括后台与前台,拦截器,登录,后台下载就可以使用,注意不是jpa,里面有完整Dao,千万级数据项目分离的代码,为了适合老项目开发特意集成hiberante5....
hiberante查询方式使用详解,有hql,sql,qbc,以及存储过程,hiberante的基础查询
hibernate 源码 和hibernate配置的html文档。。非常全面的介绍了hibernate配置。分别在src和zh-cn文件中
hibernate 是SSH框架中的一个。 是数据持久层的 重要框架
Hibernate Tools是一套全新而且完整的面向Hibernate3的工具集合,它包含了Eclipse插件和Ant编译流程。Hibernate Tools是JBoss Tools的核心组件,所以他也是JBoss Developer Studio的一部分
spring整合
Hiberante HTML 帮助文档 Hiberante HTML 帮助文档 Hiberante HTML 帮助文档
Struts2+hiberante+Spring+Extjs+OA办公系统,完整包正常运行
hibernate5.0.7jar包,主要是jar包,方便大家下载使用。主要是jar包,方便大家下载使用。主要是jar包,方便大家下载使用
开发工具:MyEclipse 6....Struts+Spring+Hiberante框架整合的简单登录系统 无需配置任何文件、只需在mysql中创建一个空数据库 如:create database test; 注:mysql数据库用户名:root 密码:root
spring4+hiberante4+struts2+maven最新框架
hiberante4.2.3-part01
hiberante4.2.3-part2
hiberante annotations reference_cn
Hiberante3_HQL,Hibernate3_缓存机制,Hibernate3_数据加载
这个小项目使用jsf+richfaces+hiberante设计,作为学习jsf和hibernate的新手学习参考.里面由基本上有常规项目所需一般功能:比如ajax.分页.hibernate级联操作等等
hiberante-4.2.3-part3