I have 2 amazon ec2 instances. On the first one I have my application running on a Glassfish4 application server, while on the second instance I have mysql installed. I have to connect the to tier. I have configured mysql as a remote server, setting the bind-address=0.0.0.0 in etc/mysql/my.conf and restarting mysql. Then I create a user@ in mysql and granted all the privilegies on the created database to this user.
On the application tier I create the jdbc connection pool using the following glassfish command:
create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlDataSource --restype javax.sql.DataSource --property portNumber=3306:password=<PASSWORD>:user=<USER>:serverName=<MYSQL-SERVER-IP>:databaseName=<DATABASE-NAME> <CONNECTION-POOL-NAME>
Then I deploy my application on Glassfish4.
In my application I use the following method:
public Connection getConnection() {
Connection c = null;
try {
Class.forName("com.mysql.jdbc.Driver");
c = (Connection) DriverManager.getConnection(connectionString,username,password);
} catch (SQLException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
return c;
}
Also my application has a particular feature. I handle some database entities using JPA 2.0 while some others directly using SQL. So I also have a persistence.xml in which I specify the following property:
<property name="javax.persistence.url" value="jdbc:mysql://<MYSQL-SERVER-IP>:3306/<DATABASE-NAME>"/>
The url in the persistence.xml property is the same connectionString user in the getConnection() method. In the persistence.xml I also specify the username and password to access the database.
My application home shows two button, one to initialize the database with the entities managed by the JPA and another to inizialize the database with the remaining entities using SQL. The first one works successfully and the database tables are created on the remote mysql server. But when I click the second button it throws the CommunicationsException.
I read a lot on the web and also on stackoverflow about this problem and I see there may be multiple causes. In particupar I read the following discussion:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
and tried to fix almost all the possible causes, but anything seems to work.
In my opinion it should be something with the JDBC driver. I also tried to put the mysql JDBC connector in the glassfish4/glassfish/lib folder but doesn't work. Also putting it in the application classpath doesn't work.
Excuse me for my english and if I forgot to specify something in the question. I hope my question is in accordance with the rules of stackoverflow since I have just signed up and this is my first post.
Thanks in advance for your help.