简体   繁体   English

休眠会话工厂问题

[英]Hibernate Session Factory issue

I am using hibernate 4 for building an application. 我正在使用hibernate 4来构建应用程序。 While running the application I am getting the following error. 运行该应用程序时,出现以下错误。

Failed to create sessionFactory object.java.lang.NoSuchFieldError: TRACE Exception in thread "main" java.lang.NullPointerException 无法创建sessionFactory object.java.lang.NoSuchFieldError:线程“主”中的TRACE异常java.lang.NullPointerException

My code snippet is, 我的代码段是

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 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();
   }

My configuration file is : 我的配置文件是:

<?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>

Please guide me what is wrong with the code or any jar file? 请指导我代码或任何jar文件出了什么问题?

** **

Answer: The log4j is the cause of this issue. 答:log4j是此问题的原因。 I removed the Log4j and added log4j-1.2.16.jar. 我删除了Log4j并添加了log4j-1.2.16.jar。 It gets fixed. 它得到修复。 Thanks! 谢谢!

** **

in your code what is new DBConnection() refers. 在您的代码中,新的DBConnection()指的是什么。 We need to get/open session in Hibernate from org.hibernate.SessionFactory.SessionFactory. 我们需要从org.hibernate.SessionFactory.SessionFactory在Hibernate中获取/打开会话。 So please check whether you are using sessionfactory or not. 因此,请检查您是否正在使用sessionfactory。

请尝试

Configuration configuration = new AnnotationConfiguration();

In your code, you are calling 2 times session.close(); 在您的代码中,您要调用2次session.close(); . In 2 catch clause and in the finally clause Here : 在2 catch子句和finally子句中:

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

and here : 和这里 :

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

Remember, in java in the try-catch-finally statement, the finally will be executed whether an exception is thrown or not. 请记住,在java中的try-catch-finally语句中,无论是否引发异常,都将执行finally In your case, if an exception is raised, session.close() will be called 2 times. 在您的情况下,如果引发异常,则session.close()将被调用2次。 One time in the catch clause and the second in the finally clause. catch子句中一次,在finally子句中第二次。 I don't know how hibernate behave when you are calling 2 times session.close() 我不知道当您调用2次session.close()时休眠状态如何

As stated in this prefix your show_sql, format_sql and use_sql_comments with hibernate. 前缀中所述,您的show_sql,format_sql和use_sql_comments与hibernate一起使用。 like 喜欢

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

The log4j is the cause of this issue. log4j是此问题的原因。 I removed the Log4j and added log4j-1.2.16.jar. 我删除了Log4j并添加了log4j-1.2.16.jar。 It gets fixed. 它得到修复。 Thanks! 谢谢!

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

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