1
votes

I have a Java web application (REST API) that talks to a mysql database.

It works perfectly on my localhost, and on a standalone EC2 server running Tomcat and MySql, however when I deploy on AWS Elastic Beanstalk it cannot connect to Amazon Relational Data Service.

I get the following error message:

22:09:18,757 DEBUG BasicResourcePool:1831 - An exception occurred while acquiring a poolable resource. Will retry.
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:289)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:224)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPerTaskAsynchronousRunner$TaskThread.run(ThreadPerTaskAsynchronousRunner.java:255)

I'm running Mysql 5.5, and it is a Java 7 application.

To get the Java 7 WAR file to run I have a config file in .ebextensions as per: http://qslack.com/2012/12/using-java-1-7-on-amazon-aws-elastic-beanstalk/

I have reviewed http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.rds.html, and the only thing I haven't done is

To connect to Tomcat RDS environments you must load the driver explicitly using Class.forName() prior to the call to DriverManager.getConnection() in the Java code.

as I'm using Spring (3.2.0) and Hibernate (4.1.9) to make the DB connection. Having said that I did try to add this to one of my methods as a test but it didn't work.

I have connected successfully from the command line on the elastic beanstalk ec2 instance, and also from my personal computer, so it's not a permissions/access issue.

I have the mysql connector/j jar file (mysql-connector-java-5.1.25-bin.jar) in my WebContent/WEB-INF/lib directory. I have also tried manually copying this jar file to /usr/share/tomcat7/lib but this did not help.

It seems to be a problem that several people have come across but none of the proposed solutions seem to work. e.g:

https: //forums.aws.amazon.com/thread.jspa?messageID=413533 https: //forums.aws.amazon.com/thread.jspa?messageID=285923&tstart=0#285923

1

1 Answers

1
votes

The issue was that Elastic Beanstalk was adding quotes around the JDBC_CONNECTION_STRING environment variable that I was passing in.

The issue and solution is described in this thread: https://forums.aws.amazon.com/thread.jspa?threadID=115043

(Adding the following to my .ebextensions config fixed the issue):

commands: 
     patch_tomcat:
         command: sed -i.bak s#\"/usr/sbin/tomcat7\"#\"/usr/sbin/tomcat7-elasticbeanstalk\"#g /etc/init.d/tomcat7