Amith Mendis Amith Mendis - 3 months ago 23
MySQL Question

java.lang.ExceptionInInitializerError in Spring + Hibernate + jersey

I have tried to build a web service using Spring + Hibernate + jersey.
It compiles and deploys without any errors, but when I try to access the web service it gives an error.

pom.xml

<properties>
<jersey.version>1.19</jersey.version>
<spring.version>4.2.7.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>


<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>

</dependencies>


web.xml





<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.erp.platform.ws</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/erp/*</url-pattern>
</servlet-mapping>




hibernate.cfg.xml in src/main/resources

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/erp?‌​useSSL=false;</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="connection.pool_size">1</property>
<mapping class="com.erp.platform.beans.Policy"></mapping>
<mapping class="com.erp.platform.beans.Role"></mapping>
<mapping class="com.erp.platform.beans.User"></mapping>
<mapping class="com.erp.platform.beans.RolePolicy"></mapping>
<mapping class="com.erp.platform.beans.UserRole"></mapping>
<mapping class="com.erp.platform.beans.RolePolicyId"></mapping>
<mapping class="com.erp.platform.beans.UserRoleId"></mapping>
</session-factory>
</hibernate-configuration>


HibernateUtil.java in com.erp.platform.util

package com.erp.platform.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}

}


Error is returned when trying to run this web service:

package com.erp.platform.ws;
import java.util.Date;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import com.sun.jersey.api.core.InjectParam;
import com.erp.platform.beans.User;
import com.erp.platform.dao.BeanDao;
import com.erp.platform.dao.UserDao;

@Path("userManager")
@Service
public class UserManagerWS {

@GET
@Path("/getUserList")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUserList() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
UserDao userDao = (UserDao) context.getBean("userDaoImpl");
return (List<User>) userDao.getUserList();

}

}


The error is like this:

type Exception report

message java.lang.ExceptionInInitializerError

description The server encountered an internal error that prevented it from fulfilling this request.

exception
javax.servlet.ServletException: java.lang.ExceptionInInitializerError
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause
java.lang.ExceptionInInitializerError
com.erp.platform.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
com.erp.platform.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
com.erp.platform.daoImpl.UserDaoImpl.getUserList(UserDaoImpl.java:26)
com.erp.platform.ws.UserManagerWS.getUserList(UserManagerWS.java:51)

root cause
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)


root cause
org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)

root cause
java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:676)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:638)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:606)
com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1857)

root cause
java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:434)
java.util.Properties.load0(Properties.java:353)
java.util.Properties.load(Properties.java:341)
com.mysql.cj.jdbc.util.TimeUtil.loadTimeZoneMappings(TimeUtil.java:163)
com.mysql.cj.jdbc.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:109)
com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:308)
com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:2474)

Answer

It is a bug in mysql-connector Version 6.0.2

Change to a newer Version of mysql connector. There the bug should be solved.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.3</version>
</dependency>
Comments