在哪里指定驱动程序版DB H2?
有两个不同版本的数据库H2:1.4.200和2.1.214。
application.yaml文件的内容:
app:
datasource1:
jdbcUrl: jdbc:h2:~/database/test1
driverClassname: org.h2.Driver
h2.version: 1.4.200
username: xxx
password: xxx
datasource2:
jdbcUrl: jdbc:h2:~/database/test2
driverClassname: org.h2.Driver
h2.version: 2.1.214
username: xxx
password: xxx
有两个配置类:dataSourcesConfiguration1" dataSourcesConfiguration2。
@Configuration
@EnableTransactionManagement
public class DataSourcesConfiguration1 {
@Primary
@Bean
@ConfigurationProperties("app.datasource1")
public DataSourceProperties dataSourceProperties1() {
return new DataSourceProperties();
}
@Primary
@Bean("dataSource1")
@ConfigurationProperties(prefix = "app.datasource1")
public HikariDataSource dataSource1() {
return dataSourceProperties1()
.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Primary
@Bean("jdbcTemplate1")
public NamedParameterJdbcTemplate jdbcTemplate1()
{
HikariDataSource ds = dataSource1();
return new NamedParameterJdbcTemplate(ds);
}
@Primary
@Bean
@Qualifier("transactionManager1")
DataSourceTransactionManager transactionManager1() {
HikariDataSource ds = dataSource1();
return new DataSourceTransactionManager(ds);
}
}
dataSourcesConfiguration2类看起来相似,没有@primary注释。
我不知道在哪里指定DB驱动程序版本。 我只能在文件pom.xml中指定版本,但它适用于两个数据源:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<!-- <version>2.1.214</version> -->
<scope>runtime</scope>
</dependency>
行 H2.version:1.4.200和H2.version:2.1.214 在file application.yaml中指定,创建bean时被忽略。 需要找到一些属性hikaridatasource ds.setdriverversion();
但是我不知道在哪里指定驱动程序版本DB H2?
there are two databases h2 of different versions: 1.4.200 and 2.1.214.
contents of the application.yaml file:
app:
datasource1:
jdbcUrl: jdbc:h2:~/database/test1
driverClassname: org.h2.Driver
h2.version: 1.4.200
username: xxx
password: xxx
datasource2:
jdbcUrl: jdbc:h2:~/database/test2
driverClassname: org.h2.Driver
h2.version: 2.1.214
username: xxx
password: xxx
there are two configuration classes: DataSourcesConfiguration1 и DataSourcesConfiguration2.
@Configuration
@EnableTransactionManagement
public class DataSourcesConfiguration1 {
@Primary
@Bean
@ConfigurationProperties("app.datasource1")
public DataSourceProperties dataSourceProperties1() {
return new DataSourceProperties();
}
@Primary
@Bean("dataSource1")
@ConfigurationProperties(prefix = "app.datasource1")
public HikariDataSource dataSource1() {
return dataSourceProperties1()
.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Primary
@Bean("jdbcTemplate1")
public NamedParameterJdbcTemplate jdbcTemplate1()
{
HikariDataSource ds = dataSource1();
return new NamedParameterJdbcTemplate(ds);
}
@Primary
@Bean
@Qualifier("transactionManager1")
DataSourceTransactionManager transactionManager1() {
HikariDataSource ds = dataSource1();
return new DataSourceTransactionManager(ds);
}
}
the DataSourcesConfiguration2 class looks similar, without the @Primary annotation.
I do not know where to specify the db driver version.
I can only specify the version in the file pom.xml, but it works for two data sources:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<!-- <version>2.1.214</version> -->
<scope>runtime</scope>
</dependency>
lines
h2.version: 1.4.200 and h2.version: 2.1.214
specified in the file application.yaml, ignored when creating beans.
need to find some property HikariDataSource ds.setDriverVersion();
but I don't know where to specify the driver version db h2 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能一次加载两个版本的H2,除非它们由不同的类负载器加载。
H2 1.4.200是一个旧版本,为什么这些天您需要使用它?如果要将数据从H2的旧版本移动到新版本,则可以尝试使用
org.h2.tools.upgrade
h2 2.1.214中的类,也可以使用第三方升级工具: https://github.com/manticore-projects/h2migrationTool但是,如果您真的需要两者都使用它们,最简单的解决方案是将H2 2.1.214包括在您的应用程序的类中并启动一个单独的H2服务器进程使用H2 1.4.200(
Java -Jar -Jar H2-1.4.4.200.jar
),连接URL将为JDBC:H2:TCP:// localhost/〜/database/test1
for 1.4.200和相同的jdbc:h2:〜/database/test2
2.1.214。您的应用程序将在1.4.200服务器上使用远程数据库,并使用2.1.214打开的嵌入式数据库。
You cannot load two versions of H2 at once, unless they are loaded by different classloaders.
H2 1.4.200 is an old unsupported version, why you need to use it these days? If you want to move your data from old version of H2 to new one you can try to use
org.h2.tools.Upgrade
class in H2 2.1.214 or you can use a third-party upgrade tool: https://github.com/manticore-projects/H2MigrationToolBut if you really need to use them both, the simplest solution is to include H2 2.1.214 to the classpath of your application and start a separate H2 Server process with H2 1.4.200 (
java -jar h2-1.4.200.jar
), connection URLs will bejdbc:h2:tcp://localhost/~/database/test1
for 1.4.200 and the samejdbc:h2:~/database/test2
for 2.1.214.Your application will use a remote database on 1.4.200 server and embedded database opened by 2.1.214.