Spring bootマルチデータソース


接続プール用はヒカリ
       (   jasypt  ):
spring.datasource.primary.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=UTF-8
spring.datasource.primary.username=root
spring.datasource.primary.password=ENC(hZ1oe0u6d7jAbgH/KEbVH8yQiVs6Iee+)

spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/work?useUnicode=true&characterEncoding=UTF-8
spring.datasource.secondary.username=user_pro
spring.datasource.secondary.password=ENC(hZ1oe0u6d7jAbgH/KEbVH8yQiVs6Iee+)

jasypt.encryptor.password=qwer1234tyu

Hikari構成:
@Configuration
@CommonsLog
public class DataSourcesConfig {

    @Value("${spring.datasource.primary.url}")
    private String primaryUrl;
    @Value("${spring.datasource.primary.username}")
    private String primaryUsername;
    @Value("${spring.datasource.primary.password}")
    private String primaryPassword;
    @Value("${spring.datasource.secondary.url}")
    private String secondaryUrl;
    @Value("${spring.datasource.secondary.username}")
    private String secondaryUsername;
    @Value("${spring.datasource.secondary.password}")
    private String secondaryPassword;

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public HikariDataSource primaryDataSource() {
        log.info("      !");
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(primaryUrl);
        hikariDataSource.setUsername(primaryUsername);
        hikariDataSource.setPassword(primaryPassword);
        hikariConfig(hikariDataSource);
        return hikariDataSource;
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public HikariDataSource secondaryDataSource() {
        log.info("  (  )    !");
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(secondaryUrl);
        hikariDataSource.setUsername(secondaryUsername);
        hikariDataSource.setPassword(secondaryPassword);
        hikariConfig(hikariDataSource);
        return hikariDataSource;
    }

    private void hikariConfig(HikariDataSource hikariDataSource) {
        hikariDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        hikariDataSource.setAutoCommit(true);
        hikariDataSource.setReadOnly(false);
        hikariDataSource.setMaximumPoolSize(100);
        hikariDataSource.setMaxLifetime(1800000L);
        hikariDataSource.setIdleTimeout(600000L);
        hikariDataSource.setMinimumIdle(5);
        hikariDataSource.setConnectionTimeout(60000L);
        hikariDataSource.setValidationTimeout(60000L);
        hikariDataSource.setConnectionTestQuery("SELECT 1");
    }
}

マスターライブラリの構成:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= {"com.demo.dao.dao1"})
public class PrimaryConfig {

    @Autowired @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder){
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties(primaryDataSource))
                //         
                .packages("com.demo.model1")
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired(required=false)
    private JpaProperties jpaProperties;

    private Map getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }

ライブラリからの構成:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= {"com.demo.dao2"})
public class SecondaryConfig {

    @Autowired(required = false)
    private JpaProperties jpaProperties;

    @Autowired @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .properties(getVendorProperties(secondaryDataSource))
                //         
                .packages("com.demo.model2")
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }


    private Map getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }
}

 
このように配置すればよい.コードを書くときは、daoは異なるフォルダで区別されます.トランザクションを使用するときは、daoを呼び出すサービスメソッドでプライマリ・ライブラリのトランザクションか、ライブラリからのトランザクションかを宣言する必要があります.