Reputation: 3070
I have a GWT web application project usually running on a tomcat, but I can't get it to connect to my MySQL database reliably.
It works in the following instances:
It doesn't work in the following cases
jettyRunWar
task (which first builds the war and then starts a built-in jetty to deploy it to)The web application starts up fine in all cases but the first database access fails with the following exception in the last two cases:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2575)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:443)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:141)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:111)
[snip: our code creating the connection to the database]
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.Buffer.<init>(Buffer.java:55)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1740)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
... 44 more
I also went back in our commit history a few months, but can't find a version that doesn't have the problem. However, I'm pretty sure that it used to run without problems using the jettyRunWar
command of gradle.
Here is the relevant code for the database connection:
public static Connection getConnection(String host, int port, String database, String user, String password) throws SQLException
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName(host);
dataSource.setPort(port);
dataSource.setDatabaseName(database);
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setConnectTimeout(Preferences.MYSQL_CONNECTION_TIMEOUT);
return dataSource.getConnection();
}
I tried it in several environments as you can see and tried several version of the code (e.g. with different GWT jars etc.) but I still have no clue why it works in some configurations but not in others.
Any ideas what the problem could be?
Upvotes: 0
Views: 1298
Reputation: 3818
This error occurs with mysql drivers starting with version 5.1.17. Downgrading to 5.1.16 solves this problem for me.
Edit: I filed a bug concerning that in the mysqlbug tracker (http://bugs.mysql.com/bug.php?id=72630). I got very fast response that the cause might be some unsupported encoding exception. That was true for me, so after fixing the file.encoding system property (running tomcat with -Dfile.encoding=UTF-8), all worked as expected.
Upvotes: 3