DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Spring Data: Data Auditing Using JaVers and MongoDB
  • JobRunr and Spring Data
  • Spring Boot - Microservice- JaxRS Jersey - HATEOAS - JerseyTest - Integration
  • Spring Boot Application With Spring REST and Spring Data MongoDB

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • Infrastructure as Code (IaC) Beyond the Basics
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  1. DZone
  2. Data Engineering
  3. Data
  4. Set up Multiple DataSources With Spring Boot and Spring Data in PCF

Set up Multiple DataSources With Spring Boot and Spring Data in PCF

This tutorial will guide you in the process to setup a Spring Data application to access multiple SQL services in PCF.

By 
Marcos Barbero user avatar
Marcos Barbero
DZone Core CORE ·
Jun. 27, 18 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
109.2K Views

Join the DZone community and get the full member experience.

Join For Free

What You Will Build

You will build an application that connects to multiple MySQL services in PCF.

Pre-Requisites

  • JDK 1.8
  • A text editor or your favorite IDE
  • Maven 3.0+
  • PCF
For this guide, I'm using a PCF Dev installation.

Spring Boot With Spring Data

Spring Boot with Spring Data makes it easy to access a database through repositories and Spring Boot auto-configuration. However, if your application needs to access multiple DataSource s, it's not something provided out of the box.

PCF Services

PCF offers a marketplace of services to be provisioned on-demand. To connect a Spring application to the PCF services, there's Spring Cloud connectors that make so-called auto-reconfiguration. However, auto-reconfiguration doesn't work if you have multiple services of the same type, e.g. multiple SQL database services. If that's the case, you will need to make a manual configuration.

Configuring Multiple DataSources

To connect to multiple  DataSources  in PCF, we'll need to use the manual configuration approach.

First, add the following dependency in the pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-cloud-connectors</artifactId>
</dependency>


Java Configuration

To connect to multiple  DataSources, we need to create a new class extending the  AbstractCloudConfig provided by Spring Cloud Connectors and, then, add two service @Beans.

src/main/java/com/marcosbarbero/wd/pcf/multidatasources/config 

import org.springframework.cloud.config.java.AbstractCloudConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class CloudConfig extends AbstractCloudConfig {

    @Primary
    @Bean(name = "first-db")
    public DataSource firstDataSource() {
        return connectionFactory().dataSource("first-db");
    }

    @Bean(name = "second-db")
    public DataSource secondDataSource() {
        return connectionFactory().dataSource("second-db");
    }
}


Java Package

Create a Java package for each  DataSource  with two nested packages:  domain  and  repository .

── com
    └── marcosbarbero
        └── wd
            └── pcf
                └── multidatasources
                    ├── first
                    │   ├── domain
                    │   └── repository
                    └── second
                        ├── domain
                        └── repository


Configuration Classes per Database

As we have two DataSources, it's needed to have a configuration class per database connection. In our example, it will be two configuration classes.

For the first database connection creates the following class:

src/main/java/com/marcosbarbero/wd/pcf/multidatasources/config

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import static java.util.Collections.singletonMap;

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "firstEntityManagerFactory",
        transactionManagerRef = "firstTransactionManager",
        basePackages = "com.marcosbarbero.wd.pcf.multidatasources.first.repository"
)
@EnableTransactionManagement
public class FirstDsConfig {

    @Primary
    @Bean(name = "firstEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory(final EntityManagerFactoryBuilder builder,
                                                                            final @Qualifier("first-db") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.marcosbarbero.wd.pcf.multidatasources.first.domain")
                .persistenceUnit("firstDb")
                .properties(singletonMap("hibernate.hbm2ddl.auto", "create-drop"))
                .build();
    }

    @Primary
    @Bean(name = "firstTransactionManager")
    public PlatformTransactionManager firstTransactionManager(@Qualifier("firstEntityManagerFactory")
                                                              EntityManagerFactory firstEntityManagerFactory) {
        return new JpaTransactionManager(firstEntityManagerFactory);
    }
}


For the second database connection, you will create the following class:

src/main/java/com/marcosbarbero/wd/pcf/multidatasources/config 

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import static java.util.Collections.singletonMap;

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "secondEntityManagerFactory",
        transactionManagerRef = "secondTransactionManager",
        basePackages = "com.marcosbarbero.wd.pcf.multidatasources.second.repository"
)
@EnableTransactionManagement
public class SecondDsConfig {

    @Bean(name = "secondEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(final EntityManagerFactoryBuilder builder,
                                                                             final @Qualifier("second-db") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.marcosbarbero.wd.pcf.multidatasources.second.domain")
                .persistenceUnit("secondDb")
                .properties(singletonMap("hibernate.hbm2ddl.auto", "create-drop"))
                .build();
    }

    @Bean(name = "secondTransactionManager")
    public PlatformTransactionManager secondTransactionManager(@Qualifier("secondEntityManagerFactory")
                                                               EntityManagerFactory secondEntityManagerFactory) {
        return new JpaTransactionManager(secondEntityManagerFactory);
    }
}


For this tutorial, I'm using the property value hibernate.hbm2ddl.auto=create-drop just to make it easier, in a production application it may not have this value and should have a proper way to initialize the database schemas using a proper framework for it.


Model and Repositories

Now, it's time to create the  model  and  repository  class that will  each be connected to the configuration class described above.

src/main/java/com/marcosbarbero/wd/pcf/multidatasources/first/domain

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "first")
public class First {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String text;

    public First(String text) {
        this.text = text;
    }

    public First() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return "First{" +
                "id=" + id +
                ", text='" + text + '\'' +
                '}';
    }
}


src/main/java/com/marcosbarbero/wd/pcf/multidatasources/first/repository 

import com.marcosbarbero.wd.pcf.multidatasources.first.domain.First;

import org.springframework.data.jpa.repository.JpaRepository;

public interface FirstRepository extends JpaRepository<First, Long> {
}


src/main/java/com/marcosbarbero/wd/pcf/multidatasources/second/domain 

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "second")
public class Second {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String text;

    public Second(String text) {
        this.text = text;
    }

    public Second() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return "Second{" +
                "id=" + id +
                ", text='" + text + '\'' +
                '}';
    }
}


src/main/java/com/marcosbarbero/wd/pcf/multidatasources/second/repository

import com.marcosbarbero.wd.pcf.multidatasources.second.domain.Second;

import org.springframework.data.jpa.repository.JpaRepository;

public interface SecondRepository extends JpaRepository<Second, Long> {
}


Running

To test the application, I just added the following code to the main class in the project:

src/main/java/com/marcosbarbero/wd/pcf/multidatasources

import com.marcosbarbero.wd.pcf.multidatasources.first.domain.First;
import com.marcosbarbero.wd.pcf.multidatasources.first.repository.FirstRepository;
import com.marcosbarbero.wd.pcf.multidatasources.second.domain.Second;
import com.marcosbarbero.wd.pcf.multidatasources.second.repository.SecondRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

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

    @Autowired
    private FirstRepository firstRepository;

    @Autowired
    private SecondRepository secondRepository;

    @Override
    public void run(String... args) throws Exception {
        First firstSaved = this.firstRepository.save(new First("first database"));
        Second secondSaved = this.secondRepository.save(new Second("second database"));

        logger.info(firstSaved.toString());
        logger.info(secondSaved.toString());
    }
}


Deploying to PCF

To deploy this application to PCF, I'm using the manifest.yml file approach.

applications:
- name: multiple-db
  memory: 512MB
  instance: 1
  path: ./target/your-jar-name.jar
  services:
   - first-db
   - second-db


For this sample, I've created in PCF two  p-mysql  instances named  first-db  and  second-db .

Build

$ ./mvnw clean package


Deploy

$ cf push


When the application is deployed, it will print the following output:

First{id=1, text='first database'}
Second{id=1, text='second database'}


Summary

Congratulations! You just created a Spring Boot application that connects to multiple Database instances in PCF using Spring Data.

Footnote

  • The code used for this tutorial can be found on GitHub.
Spring Framework Spring Boot Spring Data Data (computing) application Spring Cloud Database connection Datasource

Published at DZone with permission of Marcos Barbero, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Data: Data Auditing Using JaVers and MongoDB
  • JobRunr and Spring Data
  • Spring Boot - Microservice- JaxRS Jersey - HATEOAS - JerseyTest - Integration
  • Spring Boot Application With Spring REST and Spring Data MongoDB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!