繁体   English   中英

休眠会话工厂问题

[英]Hibernate Session Factory issue

我正在使用hibernate 4来构建应用程序。 运行该应用程序时,出现以下错误。

无法创建sessionFactory object.java.lang.NoSuchFieldError:线程“主”中的TRACE异常java.lang.NullPointerException

我的代码段是

try{
        session =  new DBConnection().getSession();
        tx= session.beginTransaction();

                ........

                ........

}catch(HibernateException ex)
    {
        if (tx!=null) tx.rollback();
        ex.printStackTrace();
        session.close();
        DBConnection.close();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        session.close();
    }
    finally{
        session.close(); // The error is shown in this line while run time
        DBConnection.close();
        }

DBConnection.java

public  DBConnection()
       {

                try
                { 
                    Configuration configuration = new Configuration();
                    configuration.configure();
                    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
                    factory = configuration.buildSessionFactory(serviceRegistry);

                }
                catch (Throwable ex) { 
                    System.err.println("Failed to create sessionFactory object." + ex);
                throw new ExceptionInInitializerError(ex); 
                }
       }

    public Session getSession() 
    {
          return factory.openSession();
    }



   // Call this during shutdown
   public static void close() {
        factory.close();
   }

我的配置文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">



<hibernate-configuration>


   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost:3306/test
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      test
   </property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">false</property>
   <property name="hibernate.connection.pool_size">40</property>  

</session-factory>


</hibernate-configuration>

请指导我代码或任何jar文件出了什么问题?

**

答:log4j是此问题的原因。 我删除了Log4j并添加了log4j-1.2.16.jar。 它得到修复。 谢谢!

**

在您的代码中,新的DBConnection()指的是什么。 我们需要从org.hibernate.SessionFactory.SessionFactory在Hibernate中获取/打开会话。 因此,请检查您是否正在使用sessionfactory。

请尝试

Configuration configuration = new AnnotationConfiguration();

在您的代码中,您要调用2次session.close(); 在2 catch子句和finally子句中:

catch(HibernateException ex)
{
    if (tx!=null) tx.rollback();
    ex.printStackTrace();
    session.close(); <--- 
    DBConnection.close();
}

和这里 :

finally{
    session.close(); <-----
    DBConnection.close();
    }

请记住,在java中的try-catch-finally语句中,无论是否引发异常,都将执行finally 在您的情况下,如果引发异常,则session.close()将被调用2次。 catch子句中一次,在finally子句中第二次。 我不知道当您调用2次session.close()时休眠状态如何

前缀中所述,您的show_sql,format_sql和use_sql_comments与hibernate一起使用。 喜欢

<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">false</property>

log4j是此问题的原因。 我删除了Log4j并添加了log4j-1.2.16.jar。 它得到修复。 谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM