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).