session = HibernateSessionFactory.currentSession();
   tx = session.beginTransaction();
   session.save(admin);
   tx.commit();   
  }catch(HibernateException e){
   throw e;
  }finally{
   if (tx!=null) {
    tx.rollback();
   } 
   HibernateSessionFactory.closeSession()
  }
commit 已经成功了,为什么老提示Transaction not successfully started
而且单独写类测试都通过的,加上struts的action就问题很多

解决方案 »

  1.   

    把这个类贴出来
       HibernateSessionFactory
      

  2.   

    HibernateSessionFactory自动创建的啊
    package com;import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    public class HibernateSessionFactory {    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 org.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 || !session.isOpen()) {
    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 != null) ? sessionFactory.openSession()
    : null;
    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() {
        }}
      

  3.   

    if (tx!=null) {
        tx.rollback();
       } 
    这个放这干吗的?每次commit完还要rollback么?
      

  4.   

    session = HibernateSessionFactory.currentSession();
       tx = session.beginTransaction();
       session.save(admin);
       tx.commit();   
      }catch(HibernateException e){
       throw e;
       if (tx!=null) {
        tx.rollback();
       }
      }finally{
        
       HibernateSessionFactory.closeSession()
      }