1。TreadLocal是管理Hibernate的session
这个session与http里面的session是两个概念所以没有你这种说法2。我觉得Hibernate的事务可以满足O/R Mapping那块的事务管理
如果你要使用Weblogic的JTA来管理业务逻辑上的事务也是可以的
总之,我说的是两种事务,各负其职而已

解决方案 »

  1.   

    thx 楼上
    1.我当然知道是hibernate的session,只是有人说session交给app server容器管理,由容器来保证线程对象的安全?我不敢确认.我以前没用app server做hibernate,用的是tomcat,session用的threadlocal.2.能否具体点,比如JTA怎么来管理?谢谢
      

  2.   

    1。我不太清楚能不能由Weblogic管理hibernate的 session,从感觉上来说是不行的
    因为我也说了,这就是两回事嘛!2。JTA其实也很简单,就是用
    context.lookup("javax.transaction.UserTransaction");
    得到事务。
    然后就是beginTransaction()
    commit()
    rollback()
    之类的方法,说白了也没什么我们用这个就可以管理所有逻辑功能的事务了
      

  3.   

    如果想要非常有效安全而且易用的使用hibernate,应该使用ThreadLocal(本地线程变量)。
    下边给出一个标准的HibernateSessionFactory,在程序中需要使用到Hibernate的session时,通过currentSession()方法获得,然后完成你的持久化操作,而且使用完成以后你不必也不应该手动去session.close()。在不关闭session的情况下,hibernate的lazy(延迟加载)才能发挥最大的作用,这也是使用ThreadLocal的首要原因。
    那在哪里关闭session呢?
    写一个filter类,在filter中调用下边这个工厂的closeSession()方法,注意是在服务器处理之后,反馈给用户信息之前关闭。package com.unida.egov.demo.hibernate;import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.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.
         * NOTICE: Location should be on the classpath as Hibernate uses
         * #resourceAsStream style lookup for its configuration file. That
         * is place the config file in a Java package - the default location
         * is the default Java package.<br><br>
         * Examples: <br>
         * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
         * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> 
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";    /** Holds a single instance of Session */
        private static final ThreadLocal threadLocal = new ThreadLocal();    /** The single instance of hibernate configuration */
        private static final Configuration cfg = new Configuration();    /** The single instance of hibernate SessionFactory */
        private static net.sf.hibernate.SessionFactory sessionFactory;    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session currentSession() throws HibernateException {
            Session session = (Session) threadLocal.get();        if (session == null) {
                if (sessionFactory == null) {
                    try {
                        cfg.configure(CONFIG_FILE_LOCATION);
                        sessionFactory = cfg.buildSessionFactory();
                    }
                    catch (Exception e) {
                        System.err.println("%%%% Error Creating SessionFactory %%%%");
                        e.printStackTrace();
                    }
                }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }        return session;
        }    /**
         *  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();
            }
        }    /**
         * Default constructor.
         */
        private HibernateSessionFactory() {
        }}
      

  4.   

    至于管理事物,建议你也使用Spring来负责业务层。把事物管理都放在业务层,而不是hibernate的持久层,Spring可以有效的管理这些事务处理。
    另外,你的这些问题和应用服务器并没有太大的关系。
    第一个问题完全是hibernate自身的内容,和weblogic没有任何关系。
    第二个问题,事物是有Spring来控制,和应用服务器有关的一点就是看应用服务器支不支持JTA,像Tomcat是不支持的(至少5.0.28以前),而Jboss和weblogic是支持的。所以开发的时候可以基于Jboss服务器(支持热部署),这样会提高开发效率;程序发布的时候就在weblogic上(如果你的客户能够提供weblogic服务器的话),而我们的程序不用做任何修改。你唯一要做的就是在weblogic内配置所有用到的连接池和数据源即可。
      

  5.   

    非常感谢楼上这位兄弟
    只是我以前也是一直用threadlocal做session的管理,但有人说:在用weblogic的情况下用threadlocal管理,不如直接交给容器管理好.所以有此疑问(SB+DAO+Hibernate)是的,lazy只能在session存活的情况下有用,我想考虑在web层和业务层之间抽象一层delegate来处理事物(仅有事物处理),这样做怎么样?(不使用filter来关闭session)麻烦赐教