使用 gradle 的 JOOQ 连接池
Posted
技术标签:
【中文标题】使用 gradle 的 JOOQ 连接池【英文标题】:JOOQ connection Pool using gradle 【发布时间】:2018-04-22 08:05:31 【问题描述】:我正在从 activemq 读取数据,并为从 activemq 获得的值运行一个存储过程。
由于我要以常规速率运行大容量,因此我们包括了用于从 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());
【问题讨论】:
你如何配置你的 jOOQConfiguration
/ DSLContext
(注意,代码生成配置在这里可能没有帮助)
@LukasEder 抱歉给您带来不便,我已经添加了连接类。
【参考方案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 个连接。不太令人印象深刻。
【讨论】:
以上是关于使用 gradle 的 JOOQ 连接池的主要内容,如果未能解决你的问题,请参考以下文章
如何将 MySQL JOOQ 重命名表查询范围限定为同一个数据库?