0

I am trying to connect 2 different schemas within my spring boot application.

To do this I have got 2 different data sources. How should I configure this within my properties files?

I seen this answer which gave me an idea of how to do so. I currently have the following 3 property files in my application:

1. application.properties
2. hibernate.properties
3. multiple-db.properties

application.properties is currently empty. Below are the other 2 files:

hibernate.properties:

# Connection configuration
hibernate.connection.username= my_uname1
hibernate.connection.password= my_pword1

multiple-db.properties:

# Schema 1-Data source configuration
oracle.db.username1= my_uname1
oracle.db.password1= my_pword1
oracle.db.url1= my_url1

# Schema 2-Data source configuration
oracle.db.username2= my_uname2
oracle.db.password1= my_pword2
oracle.db.url2= my_url2

# JPA configuration
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

# Hibernate configuration
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=my_url

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider

Is this the correct approach? Do I need 3 properties files, or could I do this all in one?

Community
  • 1
  • 1
java123999
  • 6,974
  • 36
  • 77
  • 121

2 Answers2

2

The Spring documentation suggests a way to create primary and secondary data sources: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-two-datasources

Each data source can be configured as described here: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-configure-a-datasource

You can access them by using @Autowire from other beans. You can associate a prefix to each data source so you can configure it in your application.properties or application.yml file.

You can also set one as primary.

pastafarian
  • 1,010
  • 3
  • 16
  • 30
0

With Spring you can do this easily.

It would be something like this:

<bean id="dataSource_1"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/northwind" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="dataSource_2"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/northwind_dup" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

You could also use your properties files and do something like this:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>

And you could use only one file, or three. It is really up to you.