我有一个AWS Elastic Beanstalk
实例,Tomcat
运行了一个Java RESTful服务。
然后我还在MySQL
上设置了AWS-RDS
数据库实例。
我有以下主动安全组,允许所有入站和出站流量。
我可以使用MySQL Workbench连接数据库:
这表明数据库没问题。所以我认为问题出在我的Java代码上。
在阅读this后,我在Hibernate配置中设置了以下数据源(OpenShift
服务器上的注释编码工程)。
@Bean
public DataSource dataSource() {
// Openshift
// String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
// String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
// String username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
// String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
// AWS
String host = System.getenv("RDS_HOSTNAME");
String port = System.getenv("RDS_PORT");
String username = System.getenv("RDS_USERNAME");
String password = System.getenv("RDS_PASSWORD");
String dbname = System.getenv("RDS_DB_NAME");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); // jdbc.driverClassName=com.mysql.jdbc.Driver
if (host == null) {
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
} else {
//dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/");
// Openshift
//dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + environment.getRequiredProperty("jdbc.dbname"));
// AWS
dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + dbname);
dataSource.setUsername(username);
dataSource.setPassword(password);
System.out.println("jdbc:mysql://" + host + ":" + port + "/" + dbname+", username: "+username+", password: "+password);
}
return dataSource;
}
但是,当我尝试访问RESTful服务时,我得到以下内容:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
java.net.ConnectException: Connection refused (Connection refused)
答案 0 :(得分:1)
<强>解决方案:强>
需要使用 String host = System.getProperty("RDS_HOSTNAME");
String port = System.getProperty("RDS_PORT");
String username = System.getProperty("RDS_USERNAME");
String password = System.getProperty("RDS_PASSWORD");
String dbname = System.getProperty("RDS_DB_NAME");
而不是$(function() {
var value= $('select[name=type]').val();
var pattern = $('select[name=type] option:selected').data('pattern');
$('input[name=hostInput]').attr('pattern', pattern);
$('input[name=hostInput]').val(value);
});
。
{{1}}
Java服务现在连接到数据库。