Java 会话在Hibernate 4.3上关闭

Java 会话在Hibernate 4.3上关闭,java,hibernate,session,Java,Hibernate,Session,我正在使用HIbernate 4.3,并且在会话方面遇到了问题。。我正在尝试对数据库执行一些操作,但没有正常工作。我正在接收会话已关闭的消息 创建会话工厂的My HibernateUtil类: public class HibernateUtil { private static SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFacto

我正在使用HIbernate 4.3,并且在会话方面遇到了问题。。我正在尝试对数据库执行一些操作,但没有正常工作。我正在接收会话已关闭的消息

创建会话工厂的My HibernateUtil类:

public class HibernateUtil
{
   private static SessionFactory sessionFactory = buildSessionFactory(); 
   private static SessionFactory buildSessionFactory()
   {
      try
      {
         if (sessionFactory == null)
         {
            Configuration configuration = new Configuration().configure(HibernateUtil.class.getResource("/hibernate.cfg.xml"));
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
         }
         return sessionFactory;
      } catch (Throwable ex)
      {
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static SessionFactory getSessionFactory()
   {
      return sessionFactory;
   }

   public static void shutdown()
   {
      getSessionFactory().close();
   }
}
My hibernate.cfg.xml:

<hibernate-configuration>  
    <session-factory>  
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/db?autoReconnect=true</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">root</property> 

        <!-- Use the C3P0 connection pool. -->  
        <property name="c3p0.min_size">3</property>  
        <property name="c3p0.max_size">10</property>  
        <property name="c3p0.timeout">1800</property>  

        <!-- Disable second-level cache. -->  
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  
        <property name="cache.use_query_cache">false</property>  
        <property name="cache.use_minimal_puts">false</property>  
        <property name="max_fetch_depth">3</property>  

        <!-- Print SQL to stdout. -->  
        <property name="show_sql">true</property>  
        <property name="format_sql">true</property>  

        <!-- Drop and then re-create schema on SessionFactory build, for testing. -->  
        <property name="hbm2ddl.auto">create</property>  

        <!-- Bind the getCurrentSession() method to the thread. -->  
        <property name="current_session_context_class">thread</property>  

        <mapping class="model.User"/>
        <mapping class="model.Point"/>
        <mapping class="model.Layer"/>
        <mapping class="model.AddressDatabase"/>

    </session-factory>  

</hibernate-configuration>
我的Hibernate工厂:

public class HibernateDAOFactory extends DAOFactory {  
    public UserDao getUserDao() {  
        return (UserDao)instantiateDAO(UserDaoImpl.class);  
    }  

    private GenericHibernateDao instantiateDAO(Class daoClass) {  
        try {  
            GenericHibernateDao dao = (GenericHibernateDao)daoClass.newInstance();  
            dao.setSession(getCurrentSession());  
            return dao;  
        } catch (Exception ex) {  
            throw new RuntimeException("Can not instantiate DAO: " + daoClass, ex);  
        }  
    }  

    // You could override this if you don't want HibernateUtil for lookup  
    protected Session getCurrentSession() {  
        return HibernateUtil.getSessionFactory().getCurrentSession();  
    }      
}
还有我的HibernateDAO工厂,它实例化了DAO:

public class HibernateDAOFactory extends DAOFactory {  
     public UserDao getUserDao() {  
        return (UserDao)instantiateDAO(UserDaoImpl.class);  
    }  
    private GenericHibernateDao instantiateDAO(Class daoClass) {  
        try {  
            GenericHibernateDao dao = (GenericHibernateDao)daoClass.newInstance();  
            dao.setSession(getCurrentSession());t
            return dao;  
        } catch (Exception ex) {  
            throw new RuntimeException("Can not instantiate DAO: " + daoClass, ex);  
        }  
    }  
    protected Session getCurrentSession() {  
        return HibernateUtil.getSessionFactory().getCurrentSession();  
    }  

}  
通用DAO的相关部分:

public abstract class GenericHibernateDao<T, ID extends Serializable> implements GenericDao<T, ID> {

    private Class<T> persistentClass;
    private Session session;

    @SuppressWarnings("unchecked")
    public GenericHibernateDao() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
    }

    public void setSession(Session s) {
        this.session = s;
    }

    protected Session getSession() {
        if (session == null)
            session = HibernateUtil.getSessionFactory().getCurrentSession();
        return session;
    }
    public T create(T entity) {
        getSession().save(entity);
        return entity;
    }
...
公共抽象类genericBernateDao实现GenericDao{
私有类persistentClass;
非公开会议;
@抑制警告(“未选中”)
公共genericibernatedao(){
this.persistentClass=(Class)((ParameteredType)getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
公开作废设置会话(会话s){
this.session=s;
}
受保护会话getSession(){
if(会话==null)
session=HibernateUtil.getSessionFactory().getCurrentSession();
返回会议;
}
公共T创建(T实体){
getSession().save(实体);
返回实体;
}
...
当我对数据库执行一系列操作时,我发现了一个错误。下面的代码是一个servlet,显示了我如何实例化DAO并尝试运行一些操作:

DAOFactory factory = DAOFactory.instance(DAOFactory.HIBERNATE);
userDao = factory.getUserDao();

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction;

User user = new User();
user.setEmail("xxx@gmail.com");
user.setName("xxx");
user = userDao.create(user);
tx.commit(); <-- WORKS FINE

session = HibernateUtil.getSessionFactory().getCurrentSession();
tx = session.beginTransaction;
User user2 = userDao.findByEmail("xxx@gmail.com"); <-- HERE HAPPENS THE ERROR.
DAOFactory=DAOFactory.instance(DAOFactory.HIBERNATE);
userDao=factory.getUserDao();
会话会话=HibernateUtil.getSessionFactory().getCurrentSession();
事务tx=session.beginTransaction;
用户=新用户();
user.setEmail(“xxx@gmail.com");
user.setName(“xxx”);
user=userDao.create(用户);

tx.commit();在
getCurrentSession()之后调用
userDao.findByEmail
时会发生什么样的错误
?很抱歉,我没有注意到。会话关闭时出现异常。我在那里遇到异常。您遇到的错误看起来与我遇到的问题类似。我认为,当您提交事务时,这也会导致会话关闭,因此,当您使用同一会话启动新事务时,错误为cre我还没有制定出我需要做什么来防止这种情况发生,但我正在试图找到在我的代码中,以前在哪里关闭了一个事务,以及打开一个新事务时使用的同一个会话。这对你来说是不是同样的问题?
DAOFactory factory = DAOFactory.instance(DAOFactory.HIBERNATE);
userDao = factory.getUserDao();

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction;

User user = new User();
user.setEmail("xxx@gmail.com");
user.setName("xxx");
user = userDao.create(user);
tx.commit(); <-- WORKS FINE

session = HibernateUtil.getSessionFactory().getCurrentSession();
tx = session.beginTransaction;
User user2 = userDao.findByEmail("xxx@gmail.com"); <-- HERE HAPPENS THE ERROR.