title: How to change database connection in Spring Data JPA?
body: <p>I have the following data config:</p>
  
  <pre><code>@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;
     }
  }
  </code></pre>
  
  <p>Suppose I wish to switch to another database. Can I just <code>Datasource#setUrl</code> to another value? Or I need to close something first?</p>
  
  <p>Can I set URL to <code>null</code> and temporary disconnect from any database? For example, suppose I wish to create <code>SQLite</code> file from scratch (it is automatically created on first access).</p>