java-17spring-boot-2

Enabling HicariCP manually in Spring Boot


I have manually set up Spring Boot with multiple databases. Only problem is that I do not use properties file, but a YAML file, and I manually setup the dual DB connectivity.

@AllArgsConstructor
public abstract class PersistenceConfiguration {

    private final String driverClassName;
    private final String url;
    private final String username;
    private final String password;
    private final String[] packagesToScan;
    private final String hbm2ddl;
    private final String dialect;

    public DataSource dataSource() {

        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }

    public LocalContainerEntityManagerFactoryBean entityManager() {

        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(packagesToScan);

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        final HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
        properties.put("hibernate.dialect", dialect);
        em.setJpaPropertyMap(properties);

        return em;
    }

    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "adjustmentEntityManagerFactory",
        transactionManagerRef = "adjustmentTransactionManager",
        basePackages = {"com.gtn.ca_adjustments.repository.adjustment"}
)
public class AdjustmentPersistenceConfiguration extends PersistenceConfiguration {

    @Autowired
    public AdjustmentPersistenceConfiguration(Environment env) {
        super(
                env.getProperty("spring.datasource.driverClassName"),
                env.getProperty("spring.datasource.url"),
                env.getProperty("spring.datasource.username"),
                env.getProperty("spring.datasource.password"),
                new String[]{"com.gtn.ca_adjustments.entity.adjustment"},
                "none",
                env.getProperty("hibernate.dialect")
        );
    }

    @Bean(name = "adjustmentDataSource")
    @Primary
    @Override
    public DataSource dataSource() {
        return super.dataSource();
    }

    @Primary
    @Bean(name = "adjustmentEntityManagerFactory")
    @Override
    public LocalContainerEntityManagerFactoryBean entityManager() {
        return super.entityManager();
    }

    @Bean(name = "adjustmentTransactionManager")
    @Primary
    @Override
    public PlatformTransactionManager transactionManager(
            @Qualifier("adjustmentEntityManagerFactory") EntityManagerFactory entityManagerFactory
    ) {
        return super.transactionManager(entityManagerFactory);
    }
}

I have seen examples to configure HicariCP via properties file:

first.datasource.jdbc-url=jdbc:oracle:thin:@*a host*
first.datasource.username=user1
first.datasource.password=xxxxxxxxx
first.datasource.poolName=FirstPool
first.datasource.maximumPoolSize=100
first.datasource.maxLifetime=10000

How can I enable HicariCP manually via java code. I do not see methods to configure HicariCP like other properties such as username, password etc. (I use spring boot 2.7 with java 17)


Solution

  • Based on your question it looks like you need to configure 2 datasources manually via java code then this is what I am suggesting-- Using Qualifier for identification purpose & returning 2 different beans based on different database properties --

    public class DataSource {
              
        @Bean
        @Qualifier("firstDataSource")
        public DataSource firstDataSource(){
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl( "jdbc_url" );
            config.setUsername( "database_username" );
            config.setPassword( "database_password" );
            ....
            return new HikariDataSource( config );
        }
    
        @Bean
        @Qualifier("secondDataSource")
        public DataSource secondDataSource(){
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl( "jdbc_url" );
            config.setUsername( "database_username" );
            config.setPassword( "database_password" );
            ....
            return new HikariDataSource( config );
        }
    }
    

    You can put these bean definitions in your configuration class as well.