use*_*039 4 database hibernate dropwizard
我正在研究一些利用Dropwizard的代码,这需要我需要连接到至少两个不同的数据库(我计划也使用Hibernate).我无法找到任何允许我在.yml配置文件的数据库块中配置两个不同数据库连接的示例/文档.这可能在Dropwizard中吗?如果没有,那么过去人们使用的解决方法是什么.谢谢你先进的帮助!
Man*_*dan 11
您可以在dropwizard中配置多个数据库.在config.yml中,您可以拥有多个这样的数据库配置.
数据库1:
driverClass: org.postgresql.Driver
user: user
password: pwd
url: jdbc:postgresql://localhost:5432/db1
validationQuery: select 1
minSize: 2
maxSize: 8
Run Code Online (Sandbox Code Playgroud)
DATABASE2:
driverClass: org.postgresql.Driver
user: user
password: pwd
url: jdbc:postgresql://localhost:5432/db2
validationQuery: select 1
minSize: 2
maxSize: 8
Run Code Online (Sandbox Code Playgroud)
在config类中获取两个配置详细信息.
public class DBConfig extends Configuration {
private DatabaseConfiguration database1;
private DatabaseConfiguration database2;
public DatabaseConfiguration getDatabase1() {
return database1;
}
public DatabaseConfiguration getDatabase2() {
return database2;
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的服务中配置哪个Dao使用哪个数据库.
@Override
public void run(MyConfiguration configuration,
Environment environment) throws ClassNotFoundException {
...
final DBIFactory factory = new DBIFactory();
// Note that the name parameter when creating the DBIs must be different
// Otherwise you get an IllegalArgumentException
final DBI jdbi1 = factory.build(
environment, configuration.getUserDatabase(), "db1");
final DBI jdbi2 = factory.build(
environment, configuration.getItemDatabase(), "db2");
final MyFirstDAO firstDAO = jdbi1.onDemand(MyFirstDAO.class);
final MySecondDAO secondDAO = jdbi2.onDemand(MySecondDAO.class);
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3725 次 |
| 最近记录: |