【发布时间】:2017-04-26 14:10:48
【问题描述】:
我有以下数据配置:
@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;
}
}
假设我想切换到另一个数据库。我可以将Datasource#setUrl 设置为另一个值吗?还是我需要先关闭一些东西?
我可以将 URL 设置为 null 并临时断开与任何数据库的连接吗?例如,假设我希望从头开始创建SQLite 文件(它在首次访问时自动创建)。
【问题讨论】:
-
切换,能详细点吗?
-
从一个数据库断开,断开连接,然后以编程方式连接到另一个数据库。
标签: java spring sqlite spring-data-jpa