1

I am working on a Spring project that needs to connect to multiple databases.

The examples I have used are all connecting to one database using properties, set at the start of the application.

My current JPA configuration looks like this:

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] { "com.google.persistence.model" });

      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalProperties());

      return em;
   }

   @Bean
   public DataSource dataSource(){
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jpa");
      dataSource.setUsername( "user" );
      dataSource.setPassword( "password" );
      return dataSource;
   }

   @Bean
   public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(emf);

      return transactionManager;
   }

   @Bean
   public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
      return new PersistenceExceptionTranslationPostProcessor();
   }

   Properties additionalProperties() {
      Properties properties = new Properties();
      properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
      properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
      return properties;
   }
}

In this case, the datasource properties (driver, url, username, password) are already set. But is it possible to modify this current bean or create a new method that would allow me to modify those properties during runtime?

For example, while the application is running, is there a way for me to manually disconnect from the current database, modify the datasource properties, and reconnect to the new database instead?

3
  • A JPA provider will not allow you to change the EntityManagerFactory to persist to a different datastore during its lifetime; that is set at construction. Commented Nov 6, 2017 at 18:14
  • @DN1 Thanks for that info. But what if the url or the username of the database changes while my application is running? If none of the properties of the datasource can be changed, does that mean I have to stop the application before be able to make any changes? Commented Nov 6, 2017 at 19:15
  • You have to close the emf, and create a new one Commented Nov 7, 2017 at 6:00

2 Answers 2

0

You can't change this properties in runtime but if you need to use multiple databases you can create multiple persistence units. Here you can find example:

http://www.baeldung.com/spring-data-jpa-multiple-databases

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

Comments

0

The feature you're looking for is called multi-tenancy.

See, for example, this guide.

1 Comment

"The author made this story available to Medium members only. Sign up to read this one for free." Baeldung has this topic covered as well: baeldung.com/multitenancy-with-spring-data-jpa

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.