java.net.connect,java.net.ConnectException:连接被拒绝(连接被拒绝)

I have an AWS Elastic Beanstalk instance with Tomcat running a Java RESTful service installed.

ZzJdp.png

I then also have a MySQL database instance set up on AWS-RDS.

1GUIb.png

I have the following active security group that allows all inbound and outbound traffic.

FM6xT.png

I am able to connect to the database with MySQL Workbench:

sANDR.png

Which suggests the Database is okay. So I think the issue is with my Java code.

After reading this, I set up the following datasource in my Hibernate Configuration (the commented coded works on OpenShift servers).

@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;

}

However, when I try access a RESTful Service, I get the following:

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)

解决方案

Solution:

Need to use System.getProperty and not System.getenv.

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");

The Java service now connects to the database.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值