How to access my Local MySQL DB using Boot from Cloud Foundry?

How to access my Local MySQL DB from Cloud Foundry?

I have created a Spring Boot Rest Application from STS-3.8.2 to perform Crud operation which runs perfectly in local environment. When deployed into cloud foundry, the rest call with simple operation works fine. But when I call rest to perform Crud operation, it says

Could not open connection; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection

Please help me solve the issue.

Links on CloudFoundry where I deployed my Spring Boot Application

Above, one link for greeting message, 2nd for crud operation to get list from MySQL in my laptop.

SpringBootAndMySqlApplication.java:

@SpringBootApplication

public class SpringBootAndMySqlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAndMySqlApplication.class, args);
    }
}

HomeController.java:

@RestController

public class HomeController {

    @Autowired
    private TaskDao taskDao;

    @RequestMapping("/greeting")
    public String greeting() {
        return "Welcome to Boot + SQL";
    }

    @RequestMapping("/list")
    public List<TaskEntity> taskList() {
        return taskDao.findByTaskArchived(0);
    }
}

TaskDao.java:

public interface TaskDao extends CrudRepository<TaskEntity, Integer> {

    List<TaskEntity> findByTaskArchived(Integer tas);

}

TaskEntity.java:

@Entity

@Table(name = "task_list")

public class TaskEntity 

{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "task_id")
    private int id;

    @Column(name = "task_name")
    private String taskName;

    @Column(name = "task_description")
    private String taskDescription;

    @Column(name = "task_priority")
    private String taskPriority;

    @Column(name = "task_status")
    private String taskStatus;

    @Column(name = "task_archived")
    private int taskArchived = 0;

// getter & setter

}

application.properties:

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/taskmanager
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.driverClassName = com.mysql.jdbc.Driver

MySQL script:

create database taskmanager;

use taskmanager;

create table task_list(task_id int not null auto_increment, task_name varchar(100) not null, task_description text,task_priority varchar(20),task_status varchar(20),task_start_time datetime not null default CURRENT_TIMESTAMP,task_end_time datetime not null default CURRENT_TIMESTAMP,task_archived bool default false,primary key(task_id));

insert into task_list values(1,'Gathering Requirement','Requirement Gathering','MEDIUM','ACTIVE',curtime(),curtime() + INTERVAL 3 HOUR,0);

insert into task_list values(2,'Application Designing','Application Designing','MEDIUM','ACTIVE',curtime(),curtime() + INTERVAL 2 HOUR,0);

insert into task_list values(3,'Implementation','Implementation','MEDIUM','ACTIVE',curtime(),curtime() + INTERVAL 3 HOUR,0);

insert into task_list values(4,'Unit Testing','Unit Testing','LOW','ACTIVE',curtime(),curtime() + INTERVAL 4 HOUR,0);

insert into task_list values(5,'Maintanence','Maintanence','LOW','ACTIVE',curtime(),curtime() + INTERVAL 5 HOUR,0);

select * from task_list;

Created above spring boot application in STS and perform drag-drop in CloudFoundry from BootDashBoard in STS. And pushed successfully. Now I am able to access my application from defined host in YML file. But when I am trying to call my rest to perform crud operation, it's failing.

Any solution?

Upvotes: 3

Views: 713

Answers (1)

K.AJ
K.AJ

Reputation: 1292

You included your application.properties. What profile is that for? Do you have an application.properties for cloud profile?

Cloud Foundry usually comes with MySql as a service. To leverage it, you will need to create a service instance first.

If you want your app to use an external db (outside of cloud foundry), you will have to create a user provided service (CUPS) definition.

Check out the Spring Music app. It will give you details on local and cloud profiles.

https://github.com/cloudfoundry-samples/spring-music

Upvotes: 0

Related Questions