使用gradle的JOOQ连接池

时间:2017-11-09 10:54:05

标签: java gradle activemq jooq

我正在读取activemq并为从activemq获取的值运行存储过程。

由于我将以常规速率运行大容量,因此我们包括Threading用于从activemq读取消息并运行存储过程。

执行此操作后,我发现JOOQ没有使用任何连接池,并且正在按顺序运行所有SQL操作。

JOOQ日志:

15:30:51.843 [Thread-19973] DEBUG org.jooq.tools.StopWatch - Query executed           : Total: 03:27
15:30:51.843 [Thread-19973] DEBUG org.jooq.tools.LoggerListener - Fetched result           : +-----------------------+
15:30:51.843 [Thread-19973] DEBUG org.jooq.tools.LoggerListener -                          : |loadoutputtobatchoutput|
15:30:51.843 [Thread-19973] DEBUG org.jooq.tools.LoggerListener -                          : +-----------------------+
15:30:51.843 [Thread-19973] DEBUG org.jooq.tools.LoggerListener -                          : |                       |
15:30:51.843 [Thread-19973] DEBUG org.jooq.tools.LoggerListener -                          : +-----------------------+
15:30:51.844 [Thread-19973] DEBUG org.jooq.tools.StopWatch - Finishing                : Total: 03:27, +0.272ms
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.StopWatch - Query executed           : Total: 03:27
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.LoggerListener - Fetched result           : +-----------------------+
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.LoggerListener -                          : |loadoutputtobatchoutput|
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.LoggerListener -                          : +-----------------------+
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.LoggerListener -                          : |                       |
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.LoggerListener -                          : +-----------------------+
15:30:51.856 [Thread-19975] DEBUG org.jooq.tools.StopWatch - Finishing                : Total: 03:27, +0.267ms
15:30:51.905 [Thread-19976] DEBUG org.jooq.tools.StopWatch - Query executed           : Total: 03:28
15:30:51.905 [Thread-19976] DEBUG org.jooq.tools.LoggerListener - Fetched result           : +-----------------------+
15:30:51.905 [Thread-19976] DEBUG org.jooq.tools.LoggerListener -                          : |loadoutputtobatchoutput|
15:30:51.905 [Thread-19976] DEBUG org.jooq.tools.LoggerListener -                          : +-----------------------+
15:30:51.905 [Thread-19976] DEBUG org.jooq.tools.LoggerListener -                          : |                       |
15:30:51.905 [Thread-19976] DEBUG org.jooq.tools.LoggerListener -                          : +-----------------------+
15:30:51.906 [Thread-19976] DEBUG org.jooq.tools.StopWatch - Finishing                : Total: 03:28, +0.256ms
15:30:51.938 [Thread-19977] DEBUG org.jooq.tools.StopWatch - Query executed           : Total: 03:28

Gradle Build初始化JOOQ:

jooq {
   csmart(sourceSets.main) {
      jdbc {
         driver = 'org.postgresql.Driver'
         url = 'jdbc:postgresql://0.0.0.0:8000/XXX'
         user = 'XXX'
         password = 'XXX'
         schema = 'public'
      }
      generator {
         name = 'org.jooq.util.DefaultGenerator'
         strategy {
            name = 'org.jooq.util.DefaultGeneratorStrategy'
         }
         database {
            name = 'org.jooq.util.postgres.PostgresDatabase'
            inputSchema = 'public'
            customTypes {
               customType {
                  name = 'JsonElement'
                  type = 'com.google.gson.JsonElement'                 
                  converter = 'XXX'
               }
            }
            forcedTypes {
               forcedType {
                  name = 'JsonElement'
                  expression = 'public\\.batchoutput\\.batchoutput_json|public\\.batchinput\\.batchinput_json'                           
               }
            }
         }
         generate {
           // relations = false
           //records = false            
           // pojos = true  
           // daos = true         
         }
         target {
            packageName = 'XXX'
            directory = 'jooq'
         }
      }
   }
}

修改

JOOQ连接类:

package com.check.orchestrator.di.configuration;

import com.jolbox.bonecp.BoneCPDataSource;
import org.jooq.SQLDialect;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultDSLContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;

@Configuration
@ComponentScan({
       "check.dal.jooq.tables.daos"
})
@EnableTransactionManagement
public class DBContextConfiguration {
    private static final String PROPERTY_NAME_DB_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DB_PASSWORD = "db.password";    
    private static final String PROPERTY_NAME_DB_URL = "db.url";
    private static final String PROPERTY_NAME_DB_USERNAME = "db.username";
    private static final String PROPERTY_NAME_JOOQ_SQL_DIALECT = "sql.dialect";
    private Properties properties;  

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {        
        BoneCPDataSource dataSource = new BoneCPDataSource();
        properties = new Properties();      

        try {
            properties.load(new FileInputStream("src/config.properties"));          
        }
        catch (IOException e) {         
        }

        dataSource.setDriverClass(properties.getProperty(PROPERTY_NAME_DB_DRIVER));
        dataSource.setJdbcUrl(properties.getProperty(PROPERTY_NAME_DB_URL));
        dataSource.setUsername(properties.getProperty(PROPERTY_NAME_DB_USERNAME));
        dataSource.setPassword(properties.getProperty(PROPERTY_NAME_DB_PASSWORD));      

        return dataSource;
    }

    @Bean
    public LazyConnectionDataSourceProxy lazyConnectionDataSource() {
        return new LazyConnectionDataSourceProxy(dataSource());
    }

    @Bean
    public TransactionAwareDataSourceProxy transactionAwareDataSource() {
        return new TransactionAwareDataSourceProxy(lazyConnectionDataSource());
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(lazyConnectionDataSource());
    }

    @Bean
    public DataSourceConnectionProvider connectionProvider() {
        return new DataSourceConnectionProvider(transactionAwareDataSource());
    }   

    @Bean
    public DefaultConfiguration configuration() {
        DefaultConfiguration jooqConfiguration = new DefaultConfiguration();

        jooqConfiguration.set(connectionProvider());        

        String sqlDialectName = properties.getProperty(PROPERTY_NAME_JOOQ_SQL_DIALECT);        
        SQLDialect dialect = SQLDialect.valueOf(sqlDialectName);
        jooqConfiguration.set(dialect);

        return jooqConfiguration;
    }

    @Bean
    public DefaultDSLContext dslContext() {
        return new DefaultDSLContext(configuration());
    }  

}

1 个答案:

答案 0 :(得分:1)

这并不是一个与jOOQ相关的问题,而是一个与BoneCP及其默认配置相关的问题。您没有指定您希望在池中建立多少连接,因此默认值将适用 - 这可能不够。

BoneCP文档对其默认值并不具有超级特定性,但您可以轻松地从com.jolbox.bonecp.BoneCPConfig对其进行反向工程。相关属性是:

/** Min number of connections per partition. */
private int minConnectionsPerPartition = 1;
/** Max number of connections per partition. */
private int maxConnectionsPerPartition = 2;
/** Number of partitions. */
private int partitionCount = 1;

因此,默认情况下,您的连接池最多有2个连接。不太令人印象深刻。