我有一個AWS Elastic Beanstalk
實例,其中Tomcat
運行安裝的Java RESTful服務。java.net.ConnectException:連接被拒絕(連接被拒絕)
然後我也有一個MySQL
數據庫實例設立AWS-RDS
。
我有以下活動安全小組,讓所有入站和出站流量。
我能夠連接到與MySQL Workbench中的數據庫:
這表明數據庫是好的。所以我認爲問題出在我的Java代碼上。
讀完this後,我在Hibernate Configuration(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)
看起來像昨天你問了[同樣的事情](http://stackoverflow.com/questions/42371803/aws-rds-how-to-set-up-a-mysql-database) – inquisitive