I am using hibernate 4 for building an application. 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
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
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?
**
Answer: The log4j is the cause of this issue. I removed the Log4j and added log4j-1.2.16.jar. It gets fixed. Thanks!
**
in your code what is new DBConnection() refers. We need to get/open session in Hibernate from org.hibernate.SessionFactory.SessionFactory. So please check whether you are using sessionfactory or not.
请尝试
Configuration configuration = new AnnotationConfiguration();
In your code, you are calling 2 times session.close();
. In 2 catch
clause and in the finally
clause Here :
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. In your case, if an exception is raised, session.close()
will be called 2 times. One time in the catch
clause and the second in the finally
clause. I don't know how hibernate behave when you are calling 2 times session.close()
As stated in this prefix your show_sql, format_sql and use_sql_comments with 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. I removed the Log4j and added log4j-1.2.16.jar. It gets fixed. Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.