1,Hibernate分页
Hibernate中通过对不同数据库的统一接口设计,实现了透明化、通用化的分页实现机制。
Criteria criteria = session.createCriteria(TUser.class);
criteria.add(Exception.eq("age","20"));
//从检索结果中获取第100条记录开始的20条记录
criteria.setFirstResult(100);
criteria.setFetchSize(20);
2,Session管理
Hibernate在处理Session的时候已经内置了延迟加载机制,不必过于担心Session的共享会导致整个线程生命周期内数据库联接被持续占用。
SessionFactory是线程安全的,Session不是线程安全的,我们需要一个在线程范围内的Session共享,而且要避免在线程中频繁的创建和销毁Session实例。
public class TestServlet extends HttpServlet{
[b]private ThreadLocal localSession = new ThreadLocal();[/b]
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
[b]localSession.set(getSession());[/b]
doSomething();
session.flush();
public void doSomething(){
[b]Session session = (Session)localSession.get();[/b]
.......//基于session的存取操作
}
}
Hibernate提供了一个HibernateUtil类,在代码中止要借助这个工具类获取Session实例就可以实现线程范围内Session共享,注意线程结束时关闭Session。
借助Servlet2.3规范中的Filter机制,实现线程生命周期内的Session管理。Fliter在Servelt被调用之前执行,贯穿Servlet,在Servelt调用结束之后结束。---Web程序。
public class PersistenceFliter implements Fliter{
protected static ThreadLocal hibernateHolder = new ThreadLoacl();
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
hibernateHolder.set(getSession());
try{
...
chain.doFilter(request,response);
...
}
finally{
Session sess = (Session)hibernateHolder.get();
if(sess != null){
hibernateHolder.set(null);
try{
sess.close();
}
catch(HibernateException ex){
throw new ServletException(ex);
}
}
}
...
}
3,Hibernate 与 Spring Framework
Spring的参数化事务管理功能相当强大,在基于Spring Framework的应用开发中,尽量使用容器管理事务。
配置Hiberanate-Context.xml
注意类:HibernateDaoSupport,HibernateSupport,HibernateTemplate。
4,Hibernate性能优化
(1),性能监测
(2),P6SPY
(3),Hibernate常见优化策略
Hibernate中通过对不同数据库的统一接口设计,实现了透明化、通用化的分页实现机制。
Criteria criteria = session.createCriteria(TUser.class);
criteria.add(Exception.eq("age","20"));
//从检索结果中获取第100条记录开始的20条记录
criteria.setFirstResult(100);
criteria.setFetchSize(20);
2,Session管理
Hibernate在处理Session的时候已经内置了延迟加载机制,不必过于担心Session的共享会导致整个线程生命周期内数据库联接被持续占用。
SessionFactory是线程安全的,Session不是线程安全的,我们需要一个在线程范围内的Session共享,而且要避免在线程中频繁的创建和销毁Session实例。
public class TestServlet extends HttpServlet{
[b]private ThreadLocal localSession = new ThreadLocal();[/b]
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
[b]localSession.set(getSession());[/b]
doSomething();
session.flush();
public void doSomething(){
[b]Session session = (Session)localSession.get();[/b]
.......//基于session的存取操作
}
}
Hibernate提供了一个HibernateUtil类,在代码中止要借助这个工具类获取Session实例就可以实现线程范围内Session共享,注意线程结束时关闭Session。
借助Servlet2.3规范中的Filter机制,实现线程生命周期内的Session管理。Fliter在Servelt被调用之前执行,贯穿Servlet,在Servelt调用结束之后结束。---Web程序。
public class PersistenceFliter implements Fliter{
protected static ThreadLocal hibernateHolder = new ThreadLoacl();
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
hibernateHolder.set(getSession());
try{
...
chain.doFilter(request,response);
...
}
finally{
Session sess = (Session)hibernateHolder.get();
if(sess != null){
hibernateHolder.set(null);
try{
sess.close();
}
catch(HibernateException ex){
throw new ServletException(ex);
}
}
}
...
}
3,Hibernate 与 Spring Framework
Spring的参数化事务管理功能相当强大,在基于Spring Framework的应用开发中,尽量使用容器管理事务。
配置Hiberanate-Context.xml
注意类:HibernateDaoSupport,HibernateSupport,HibernateTemplate。
4,Hibernate性能优化
(1),性能监测
(2),P6SPY
(3),Hibernate常见优化策略
711

被折叠的 条评论
为什么被折叠?



