Reputation: 366
I’m new and try to make my first RESTful services. I made it with Spring Boot, JPA mapped from MySQL database and deployed to Tomcat as war artifact. A MySQL database I also deployed to AWS in RDC service with all default settings. All worked well at local Tomcat. When tried same war artifact at AWS tomcat: deployment with all default settings: web API worked: RESTful services worked with returning java resources, but connection to database not worked when tried to retrieve data from database. In logs I could see this: Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
My settings: application.properties:
spring.datasource.url=jdbc:mysql://nz4.czzj8lfy5a3f.eu-central-1.rds.amazonaws.com/webshop
# I tryed here also with (and no difference)…com:3306/webshop
spring.datasource.username=… (secret)
spring.datasource.password=… (secret)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=none
In build:gradle I have dependences:
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
compile('org.springframework.boot:spring-boot-starter-data-rest')
example of web api code:
@Entity
@Table (name ="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "customer_id")
private Integer customerId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
...
//getters setters
public interface CustomerRepository extends JpaRepository<Customer, Integer>
{}
@RestController
@CrossOrigin
@RequestMapping(value = "/api")
public class CustomerResource {
@Autowired
CustomerRepository customerRepository;
@GetMapping(value = "/customer")
public List< Customer> > getAll() {return customerRepository.findAll();}
...
@SpringBootApplication
public class WebshopApplication extends SpringBootServletInitializer {
public static void main(String[] args)
{SpringApplication.run(WebshopApplication.class, args);}
}
Upvotes: 1
Views: 1025
Reputation: 366
I just want to add a comment: the confusion that I've got was caused by AWS' default setting in database which automatically whitelistes my IP in swequrities settings (Security group rules). Thus it allowed access to AWS hosted database from my computer when I run it at local Tomcat. I've changed it to access to "anyone" in "inbound" tab. That make access to AWS hosted Restful services to my AWS hosted database so it make it working.
Upvotes: 1
Reputation: 2575
A security group acts as a virtual firewall that controls the traffic for one or more instances. When you launch an instance, you associate one or more security groups with the instance. You add rules to each security group that allow traffic to or from its associated instances. You can modify the rules for a security group at any time; the new rules are automatically applied to all instances that are associated with the security group. When we decide whether to allow traffic to reach an instance, we evaluate all the rules from all the security groups that are associated with the instance.
Refer https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html
Add an inbound rule to this security group will solve your issue. I hope this will help you.
Upvotes: 2