1

I have an application that is not working on windows. But it is working fine on linux(Ubuntu) with absolutely the same code. MySQL is throwing an exception and I could not figure out how to solve it.

The exception is:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection

Below is the full stack trace.

May 25, 2019 4:26:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08001
May 25, 2019 4:26:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Could not create connection to database server.
May 25, 2019 4:26:40 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/SpringMVCAnnotationShoppingCart] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
java.lang.NullPointerException
    at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2983)
    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1873)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1802)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
  
// FROM THE WEB PAGE
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection  

Some configurations:

@Bean(name = "dataSource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
 
        // See: ds-hibernate-cfg.properties
        dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
        dataSource.setUrl(env.getProperty("ds.url"));
        dataSource.setUsername(env.getProperty("ds.username"));
        dataSource.setPassword(env.getProperty("ds.password"));
         
        System.out.println("## getDataSource: " + dataSource);
         
        return dataSource;
    }
 
    @Autowired
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
        Properties properties = new Properties();
 
        // See: ds-hibernate-cfg.properties
        properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        properties.put("current_session_context_class", env.getProperty("current_session_context_class"));
         

The hibernate properties file

# DataSource
 
ds.database-driver=com.mysql.jdbc.Driver
ds.url=jdbc:mysql://localhost:3306/my_db
ds.username=root
ds.password=my_password
 
 
# Hibernate Config
 
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
current_session_context_class=thread
hibernate.hbm2ddl.auto=update

My MySQL workbench is showing me that the MySQL server is up and actively running. The credentials are also fine. Tested them with a different application. So what could the problem be and how can I solve this problem? Let me know if there's some missing information that you need.

None of the solutions listed in previous posts such as this one worked for me.

guthik
  • 404
  • 1
  • 7
  • 15
  • @john16384 it has nothing to do with that. If that were the case it would not work on linux either. Besides. Trying to use the new $MySQL$ connector did not resolve the issue. – guthik May 25 '19 at 14:25
  • It has the same exception, NPE when doing `getServerCharset` (just not wrapped in another one). This kinda sounds like one of the first things the MySQL driver would want to do (so it can actually interpret responses correctly). That would point to an issue with the server itself (or perhaps what is running there isn't a MySQL server at all). – john16384 May 25 '19 at 14:29
  • 1
    Which version of MySQL Connector/J are you using? The stacktrace definitely is not of the latest version. – Mark Rotteveel May 25 '19 at 14:57
  • @MarkRotteveel ok. before it was `5.1.34`. Now changed it to `8.0.11` – guthik May 25 '19 at 15:33
  • 1
    which version of MySQL are you using? Try the release at the time (5.1.44). – gordon May 25 '19 at 23:13
  • @gordon thanks. This actually solved it. Whats the main cause? I have checked version `5.1.34` and `8.0.11` as suggested but the both give the exception. Can anyone explain to me why this is so? Putting `5.1.44` actually solved it. Put this as an answer so that I can accept it. – guthik May 26 '19 at 22:10

1 Answers1

0

Make sure that there is no other application running on the port:3306. And also configure different port for the server whichever you are using to run java.

Athish Murugan
  • 331
  • 1
  • 7
  • hello. running `netstat -aon | findstr 3306` shows that indeed only the MySQL server is running on port 3306. and the app is configured to run on 8080. so I guess thats not the issue – guthik May 26 '19 at 22:04