1

I'm new with spring specification. I've worked with JEE for a long time. So I learned that we define datasources on application.properties file using spring.

How I define two or more datasources on application.properties and how I chose the second datasource? If I try define a second datasource using spring.datasourceB.datasource.meta-datas the file show some flags saying unknown properties.

My current case I built a project like this:

application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xx.xx.xx.xx:3306/dbA?useSSL=false
spring.datasource.username=username
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

spring.DSB.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

# Enable to auto identify each datasource
spring.jpa.database=default

My repository class look like this:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

@Repository
public class ClassDaoDSA {

    @PersistenceContext
    private EntityManager manager;

    public List<T> Records() {

        String stmt = "SELECT d FROM Data d ORDER BY d.id DESC";

        return manager.createQuery(stmt).getResultList();
    }

}

When I've worked with JEE projects I could used @PersistenceUnit tag and chose my datasource by there. How I achieve the same result using spring boot?

  • Refer this [link](https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources) if you want to configure multiple datasources – abhi3232 Apr 07 '18 at 19:55

1 Answers1

0

The naming convention you use isn't compulsory, it just spares you the need for configuration. But you can also use a configuration class. This is an example from one of my projects:

@Configuration
@PropertySource("app.properties")
public class DataConfig {
    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        Resource config = new ClassPathResource("hibernate.cfg.xml");
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setConfigLocation(config);
        sessionFactory.setPackagesToScan(env.getProperty("myapp.entity.package"));
        sessionFactory.setDataSource(dataSource());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(env.getProperty("myapp.db.driver"));
        ds.setUrl(env.getProperty("myapp.db.url"));
        ds.setUsername(env.getProperty("myapp.db.username"));
        ds.setPassword(env.getProperty("myapp.db.password"));

        return ds;
    }
}

You can have different properties files and specify the one you want to use in the @PropertySource annotation, or you could have additional properties like myapp.prod.db.url and myapp.test.db.url and set up your data source as needed.

lgaleazzi
  • 163
  • 1
  • 9
  • Hello @lgaleazzi could you exemplify how I'm supposed to use then? I tryied implement using your class example but creating two classes for each data source. Now when I run my project I get an output saying that I have a unsatisfied dependency in my repository class. But I'm autowiring SessionFactory in my repository class. Is that the way that I need to use? –  Apr 08 '18 at 15:59
  • If using SessionFactory, you need to define two LocalSessionFactoryBean (one for each data source) and you can name them like this: @Bean(name="sessionFactory1"). Then when you inject SessionFactory you specify which one should be used with @Qualifier("sessionFactory1") – lgaleazzi Apr 09 '18 at 06:29
  • If you use Spring JPA, refer to this article, it explains the configuration pretty well: http://www.baeldung.com/spring-data-jpa-multiple-databases Both configurations should work. – lgaleazzi Apr 09 '18 at 06:30