1

Good evening, what is the correct and common approach of handling two or more databases?

Consider this HibernateConfiguration class configuring only one datasource:

@Configuration @EnableTransactionManagement
@PropertySource(value = { "classpath:hibernate.properties" })
public class HibernateConfiguration {

    @Autowired 
    private Environment env;

    @Bean 
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        // ... setting data source
        return dataSource;
    }

    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        // ... setting Hibernate properties
        return properties;
    }

    @Bean 
    public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setPackagesToScan(new String[] { "POJOs'" });
        sessionFactory.setHibernateProperties(getHibernateProperties());
        return sessionFactory;
    }

    @Bean public HibernateTransactionManager transactionManager(SessionFactory sf) {
        HibernateTransactionManager htm = new HibernateTransactionManager();
        htm.setSessionFactory(sf);
        return htm;
    }
}

Is recommended to let one class configure one datasource? Or is enough to configure all at once? How do I specify in Dao class which SessionFactory will be used and what is the recommended approach in case of switching two exact same databases on two different hosting servers?

The example DAOs. First I need to switch between Foo and Bar.

@Repository
public class RepositoryImpl implements RepositoryDao {

@Autowired // Here I need to switch between databases "foo" and "bar"
private SessionFactory sessionFactory;

...

The second one I need fixed on example database Foo.

@Repository
public class FooImpl implements FooDao {

@Autowired // Here I need fixed on "Foo"
private SessionFactory sessionFactory;
5
  • Two ore more databases basically just mean two or more datastores. Will you use spring-data? Commented Jul 13, 2017 at 15:53
  • I use only Spring Core, Spring ORM and Hibernate as the tags below the question say :) Commented Jul 13, 2017 at 15:56
  • You can add @Qualifier to sessionFactory injection to distinguish between 2 factories. So I suggest 2 config classes for each datasource with 2 sessions and 2 transaction managers. Commented Jul 13, 2017 at 16:13
  • With your comments, you answered me. Thank you :) Commented Jul 13, 2017 at 16:56
  • 1
    You are most welcome! Commented Jul 13, 2017 at 16:57

1 Answer 1

2

One approach

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.foo")
    public DataSourceProperties fooDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.foo")
    public DataSource fooDataSource() {
        return fooDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    @ConfigurationProperties("app.datasource.bar")
    public BasicDataSource barDataSource() {
        return (BasicDataSource) DataSourceBuilder.create()
                .type(BasicDataSource.class).build();
    }

Spring multiple datasources config

Other approach could be : loading different mapping (orm.xml) from persistence.xml or refer to different schemas in Entity classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.