1

I have the following data config:

@Configuration
@EnableJpaRepositories(DataConfig.repositoryPackage)
@EnableTransactionManagement
public class DataConfig {

   public static final String repositoryPackage = ...
   public static final String entitiesPackage = ...

   @Bean
   public File sqliteDatabaseFile() {
      File ans = new File("database/canada.sqlite");
      if( !ans.getParentFile().exists() ) {
         ans.getParentFile().mkdirs();
      }
      return ans;
   }

   @Bean
   public DataSource dataSource() {
      BasicDataSource ans = new BasicDataSource();
      ans.setDriverClassName("org.sqlite.JDBC");
      ans.setUrl("jdbc:sqlite:" + sqliteDatabaseFile().getAbsolutePath());
      //ans.setMaxTotal(4);
      //ans.setMaxTotal(1);
      return ans;
   }

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      LocalContainerEntityManagerFactoryBean ans =
         new LocalContainerEntityManagerFactoryBean();
      ans.setDataSource(dataSource());
      ans.setJpaVendorAdapter(jpaVendorAdapter());
      ans.setPackagesToScan(entitiesPackage);

      Properties props = new Properties();
      //props.put("hibernate.dialect", "com.enigmabridge.hibernate.dialect.SQLiteDialect");
      //props.put("hibernate.dialect", "org.hibernate.dialect.SQLiteDialect");
      props.put("hibernate.dialect", "com.beyondmap.preparator2.hibernate.SQLiteDialect");
      ans.setJpaProperties(props);



      return ans;
   }

   @Bean
   public JpaVendorAdapter jpaVendorAdapter() {
      HibernateJpaVendorAdapter ans = new HibernateJpaVendorAdapter();
      ans.setShowSql(true);
      ans.setGenerateDdl(false);
      ans.setDatabase(Database.DEFAULT);


      return ans;
   }

   @Bean
   public PlatformTransactionManager transactionManager() {
      JpaTransactionManager ans = new JpaTransactionManager();
      ans.setEntityManagerFactory(entityManagerFactory().getObject());

      return ans;
   }
}

Suppose I wish to switch to another database. Can I just Datasource#setUrl to another value? Or I need to close something first?

Can I set URL to null and temporary disconnect from any database? For example, suppose I wish to create SQLite file from scratch (it is automatically created on first access).

Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

1

You cannot flip the connection programmatically at runtime, Instead you can create 2 datasources when Spring Boot Auto Configurations are loaded into the JVM and marked the default DataSource as @Primary and other can be flipped and used as Secondary in the usage wherever you need them to be used respectively.

Take a look at the solution for creating 2 Data Sources here : Spring Boot Configure and Use Two DataSources

Community
  • 1
  • 1
  • So, I can't write my own database manager like DbVisualizer, which allows user to enter arbitrary database URL? – Dims Apr 30 '17 at 09:04
  • @Dims believe you can do that using JDBC template in spring. i have already done and created data layer my own and written a small level ORM using jdbc template in springboot. – Irfan Nasim Jun 12 '19 at 09:34