Spring and Tomcat: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

1.8k views Asked by At

I have an application that uses the Spring Repository framework and a MySQL database. I currently have it deployed on Tomcat 7.

Once I deploy and attempt to log into the application (the first call to the database) I receive the following exception:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/opt/apache-tomcat-7.0.62/webapps/waypoint/WEB-INF/classes/repository-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

I have enabled query logging on the database and can confirm that a query is being made to the database for a user object. However, on the login page itself, I see the following message:

Your login attempt was not successful, try again.

Reason: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

To make things a bit more confusing, this only happens when I try deploying on a remote server. When I start a local instance of MySQL and run an instance of Tomcat on localhost, everything works as expected.

Below is my repository-context.xml:

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://localhost:3306/waypoint_dev" />
    <beans:property name="username" value="root" />
</beans:bean>

<beans:bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="jpaVendorAdapter">
        <beans:bean
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </beans:property>
    <!-- spring based scanning for entity classes> -->
    <beans:property name="packagesToScan"
        value="com.cigna.waypoint.repository, com.cigna.waypoint.core" />
</beans:bean>

<beans:bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean>

Does anyone have any ideas what could be causing the problem? Is there a configuration that is needed with Tomcat that I am not aware of, or have forgotten?

Thanks for your help.

=== UPDATE 1 ====

After specifying the dialect, I am receiving the following stack trace:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 161,752 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_45]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_45]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_45]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3427) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3327) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2030) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) ~[commons-dbcp-1.4.jar:1.4]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) ~[commons-dbcp-1.4.jar:1.4]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
    ... 80 common frames omitted
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2914) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3337) ~[mysql-connector-java-5.1.33.jar:5.1.33]
    ... 90 common frames omitted
20:48:32.455 [http-bio-8080-exec-34] DEBUG o.s.s.w.a.UsernamePasswordAuthenticationFilter - Authentication request failed: org.springframework.security.authentication.InternalAuthenticationServiceException: could not extract ResultSet; nested exception is org.hibernate.exception.JDBCConnectionException: could not extract ResultSet
2

There are 2 answers

0
Mac On BEST ANSWER

I was able to resolve this issue by working through the steps listed in @Soheil 's answer found here:

Solving a "communications link failure" with JDBC and MySQL

It turns out that all I needed was add the hibernate dialect, as @Dhanush Gopinath suggested, and add the following to my my.cnf file:

interactive_timeout=180
wait_timeout=180
connect_timeout=180
max_connect_errors=9999
skip-name-resolve
3
Dhanush Gopinath On

Hibernate needs to know the dialect so that it can connect to the underlying database. You need to set the dialect as org.hibernate.dialect.MySQL5Dialect for MySQL along with the Datasource definition.

Here is a link which says why you need dialect.