我有一个与mysql数据库对话的Java Web应用程序(REST API)。
它完全适用于我的localhost,以及运行Tomcat和MySql的独立EC2服务器,但是当我在AWS Elastic Beanstalk上部署时,它无法连接到Amazon Relational Data Service。
我收到以下错误消息:
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)
我正在运行Mysql 5.5,它是一个Java 7应用程序。
要运行Java 7 WAR文件,我在.ebextensions
中有一个配置文件:
http://qslack.com/2012/12/using-java-1-7-on-amazon-aws-elastic-beanstalk/
我已经审核了http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.rds.html,我唯一没做过的就是
要连接到Tomcat RDS环境,您必须加载驱动程序 在调用之前显式使用Class.forName() Java代码中的DriverManager.getConnection()。
因为我使用Spring(3.2.0)和Hibernate(4.1.9)来建立数据库连接。说过我确实尝试将其添加到我的一个方法中作为测试,但它没有用。
我已经从弹性beanstalk ec2实例的命令行成功连接,也从我的个人计算机成功连接,因此它不是权限/访问问题。
我的WebContent / WEB-INF / lib目录中有mysql connector / j jar文件(mysql-connector-java-5.1.25-bin.jar)。我也尝试手动将此jar文件复制到/usr/share/tomcat7/lib
,但这没有帮助。
这似乎是一个问题,有几个人遇到过,但提议的解决方案似乎都没有效果。 e.g:
https://forums.aws.amazon.com/thread.jspa?messageID=413533 https://forums.aws.amazon.com/thread.jspa?messageID=285923&tstart=0#285923
答案 0 :(得分:1)
问题是Elastic Beanstalk在我传入的JDBC_CONNECTION_STRING环境变量周围添加了引号。
此主题中描述了问题和解决方案: https://forums.aws.amazon.com/thread.jspa?threadID=115043
(在我的.ebextensions配置中添加以下内容修复了问题):
commands:
patch_tomcat:
command: sed -i.bak s#\"/usr/sbin/tomcat7\"#\"/usr/sbin/tomcat7-elasticbeanstalk\"#g /etc/init.d/tomcat7