我有一个spring boot应用程序,它使用gradle作为构建工具,jooq用于dao类生成和db连接。以前,我的应用程序连接到单个mysql实例。下面是我们用来连接单个数据库实例的配置:
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.name=ds_name
spring.datasource.schema=ds_schema
spring.jooq.sql-dialect=MYSQL当前的项目结构是
a)具有具有上述键值对的application.properties的主应用程序项目MainApp。
b)将应用程序项目分离为DBProject,其中包含jooq生成的DAO类。MainApp将DBProject作为jar包含在内。
我使用gradle作为构建工具。
到现在为止,一切都运行得很好。但现在我必须连接到另一个MySQL实例。因此,我创建了另一个名为DBProject2的数据库项目,其中也包含了由jooq使用另一个mysql模式生成的dao类。我已经完全按照创建DBProject的方式创建了DBProject2。
现在,我的问题是,如果我将两个DBProjects都包含在MainApp中作为jar,那么这两个都将使用与application.properties中相同的数据库配置。如何让单独的db jar指向它们各自的db模式。我在谷歌上搜索了很多,但找不到有用的解决方案。
发布于 2017-03-21 23:08:42
这就是我在我的Play应用程序中连接到多个(附加)数据源的方法。我不确定这是不是最好的方法,但它对我来说很好。我已将下面的名称更改为通用名称。
// In my application.conf
// default data source
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/myDb?useSSL=false"
db.default.username=myuser
db.default.password="mypassword"
// additional data source
db.anothersource.driver=com.mysql.jdbc.Driver
db.anothersource.url="jdbc:mysql://localhost:3306/myothersource?useSSL=false"
db.anothersource.username=myuser
db.anothersource.password="mypassword"
// Then in Java, I create a JooqContextProvider class to expose both connections.
public class JooqContextProvider {
@Inject
Database db;
@Inject
play.Configuration config;
public JooqContextProvider(){}
/**
* Creates a default database connection for data access.
* @return DSLConext.
*/
public DSLContext dsl() {
return DSL.using(new JooqConnectionProvider(db), SQLDialect.MYSQL);
}
/**
* Creates an anothersource database connection for data access.
* @return DSLConext for anothersource.
*/
public DSLContext anotherDsl() {
return DSL.using(
new JooqAnotherSourceConnectionProvider(
config.getString("db.anothersource.url"),
config.getString("db.anothersource.username"),
config.getString("db.anothersource.password")),
SQLDialect.MYSQL);
}
}
// Then I needed to implement my JooqAnotherSourceConnectionProvider
public class JooqAnotherSourceConnectionProvider implements ConnectionProvider {
private Connection connection = null;
String url;
String username;
String password;
public JooqAnotherSourceConnectionProvider(String url, String username, String password){
this.url = url;
this.username = username;
this.password = password;
}
@Override
public Connection acquire() throws DataAccessException {
try {
connection = DriverManager.getConnection(url, username, password);
return connection;
}
catch (java.sql.SQLException ex) {
throw new DataAccessException("Error getting connection from data source", ex);
}
}
@Override
public void release(Connection releasedConnection) throws DataAccessException {
if (connection != releasedConnection) {
throw new IllegalArgumentException("Expected " + connection + " but got " + releasedConnection);
}
try {
connection.close();
connection = null;
}
catch (SQLException e) {
throw new DataAccessException("Error closing connection " + connection, e);
}
}
}
// Then in Java code where I need to access one or the other data sources...
jooq.dsl().select().from().where()...
jooq.anotherDsl().select().from().where()...
https://stackoverflow.com/questions/42705378
复制相似问题