业务层使用到事务操作时,使用ThreadLocal方法。
在Hibernate工具类中添加静态变量TheadLocal
private static TheadLocal session = new TheadLocal();

public static Session getTheadLocalSession() {
        Session s = (Session)session.get();
        if(s!=null) {
                s = getSession();
                session.set(s);
        }
        return s;
}
 
这些操作在web中应用较多。
 
session context 和事务边界:
用current_session_context_class属性来定义context(用sessionFactory.getCurrentSession()来获得session),其值为
 
1:thread:TheadLocal来管理Session实现多个操作共享一个session,避免反复获取session,并控制事务边界,此时session不能调用cloase,当commit或rollback的时候,session会自动open session in view:在生成(渲染)页面时保持session打开。
 
2.jta:由jta事务管理器来管理事务
    (connection.release_mode;after_statement)
 
悲观锁由数据库来实现;乐观锁Hibernate用version和timestamp来实现。