【问题标题】:Connecting to multiple MySQL db instances using jooq in spring boot application在 Spring Boot 应用程序中使用 jooq 连接到多个 MySQL 数据库实例
【发布时间】:2017-07-31 00:43:56
【问题描述】:

我有一个 Spring Boot 应用程序,它使用 gradle 作为构建工具,使用 jooq 进行 dao 类生成和数据库连接。以前我的应用程序连接到单个 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) 主应用项目 MainApp 具有带有上述键值对的 application.properties。

b) 单独的应用程序项目为 DBProject,它具有 jooq 生成的 DAO 类。 MainApp 将 DBProject 包含为 jar。

我正在使用 gradle 作为构建工具。

到这里为止一切正常。但现在我必须再连接一个 MySQL 实例。因此,我创建了另一个数据库项目 DBProject2,其中还包含由 jooq 使用另一个 mysql 模式生成的 dao 类。我完全按照创建 DBProject 的方式创建了 DBProject2。

现在,我的问题是,如果我将 MainApp 中的两个 DBProjects 都包含为 jar,那么两者都将使用与 application.properties 中相同的数据库配置。我如何制作单独的 db jar 以指向它们各自的 db 模式。我用谷歌搜索了很多关于这个但找不到有用的解决方案。

【问题讨论】:

    标签: spring-boot jooq


    【解决方案1】:

    这是我在 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()...

    【讨论】:

      猜你喜欢
      • 2020-05-04
      • 1970-01-01
      • 2019-10-19
      • 2015-05-31
      • 2018-08-08
      • 2018-10-12
      • 2023-01-15
      • 2019-06-16
      • 2019-07-07
      相关资源
      最近更新 更多