Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify the parameters of an internal connection pool #141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
|---|---|---|---|
| vtConnectionInitSql | String | select 1 | 此属性设置一个SQL语句,该语句将在每次创建新连接之后执行,然后再将其添加到池中。如果此SQL无效或引发异常,则将其视为连接失败,并将遵循标准重试逻辑。 |
| vtConnectionTestQuery | String | 空 | 如果您的驱动程序支持JDBC4,我们强烈建议您不要设置此属性。这适用于不支持JDBC4的“遗留”驱动程序Connection.isValid() API。这是在从池中给出连接之前执行的查询,以验证与数据库的连接是否仍然存在。再次尝试运行没有此属性的池,如果您的驱动程序不符合JDBC4,将记录错误以通知您。 |
| vtConnectionTimeout | long | 30000 | 此属性控制客户端等待池中连接的最大毫秒数。如果在没有连接可用的情况下超过此时间,则将抛出SQLException。最低可接受的连接超时为250毫秒。 |
| vtConnectionTimeout | long | 1000 | 此属性控制客户端等待池中连接的最大毫秒数。如果在没有连接可用的情况下超过此时间,则将抛出SQLException。最低可接受的连接超时为250毫秒。 |
| vtIdleTimeout | long | 600000(10分钟) | 此属性控制允许连接在池中处于空闲状态的最长时间。(此设置仅在大于minimumIdle(最小空闲数)小于maximumPoolSize(池大小)时才适用。一旦池到达连接, 空闲连接将不会退出minimumIdle。连接是否空闲退出的最大变化为+30秒,平均变化为+15秒。在此超时之前,连接永远不会被空闲。)值为0表示永远不会从池中删除空闲连接。允许的最小值为10000毫秒(10秒)。 |
| vtMaximumPoolSize | int | 10 | 最大连接数 |
| vtMaxLifetime | long | 10800000(3小时) | 此属性控制池中连接的最长生命周期。使用中的连接永远不会退役。 强烈建议您设置此值,它应比任何数据库或基础结构强加的连接时间限制短几秒。 值0表示没有最大寿命(无限寿命)。 如果设置成0会出现问题:长时间没对库进行操作(超过8小时)连接会依然在池内, 超过8小时数据库会断开与客户端的链接,在时用此链接必定报错 如果不等于0且小于30秒则会被重置回30分钟。 |
| vtMinimumIdle | int | 5 | 最小连接数(初始连接数) |
| vtValidationTimeout | long | 5000 | 此属性控制连接测试活动的最长时间。该值必须小于connectionTimeout。最低可接受的验证超时为250毫秒。 |
| vtValidationTimeout | long | 1000 | 此属性控制连接测试活动的最长时间。该值必须小于connectionTimeout。最低可接受的验证超时为250毫秒。 |

*注*:`vtMinimumIdle`和`vtMaximumPoolSize`两个参数同时未指定且分片数大于等于8时, 默认值有所不同:

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jd/jdbc/pool/HikariUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public static HikariConfig getHikariConfig(Topodata.Tablet tablet, String user,
hikariConfig = new HikariConfig(properties);
} else {
hikariConfig = new HikariConfig();
hikariConfig.setConnectionTestQuery("select 1");
hikariConfig.setConnectionTimeout(10_000);
hikariConfig.setConnectionTimeout(1_000);
hikariConfig.setMinimumIdle(5);
hikariConfig.setMaximumPoolSize(10);
hikariConfig.setConnectionInitSql("select 1");
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/jd/jdbc/pool/StatefulConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static InnerConnection getJdbcConnection(final String keyspace, final Top
continue;
}
try {
return pool.getNoStatefulConn(2000L);
return pool.getNoStatefulConn();
} catch (SQLException e) {
logger.error("getNoStatefulConn error.causeby:" + e.getMessage());
}
Expand Down Expand Up @@ -149,10 +149,6 @@ public InnerConnection getNoStatefulConn() throws SQLException {
return new InnerConnection(connection);
}

protected InnerConnection getNoStatefulConn(long hardTimeout) throws SQLException {
Connection connection = hikariPool.getConnection(hardTimeout);
return new InnerConnection(connection);
}

// GetAndLock locks the connection for use. It accepts a purpose as a string.
public StatefulConnection getAndLock(long id, String reason) throws SQLException {
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/com/jd/jdbc/vitess/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public final class Config {
FILED_TYPES.put("MaxLifetime", long.class);
FILED_TYPES.put("ValidationTimeout", long.class);
FILED_TYPES.put("ConnectionInitSql", String.class);
FILED_TYPES.put("ConnectionTestQuery", String.class);

FILED_NAMES.add("MinimumIdle");
FILED_NAMES.add("MaximumPoolSize");
Expand All @@ -63,7 +62,6 @@ public final class Config {
FILED_NAMES.add("MaxLifetime");
FILED_NAMES.add("ValidationTimeout");
FILED_NAMES.add("ConnectionInitSql");
FILED_NAMES.add("ConnectionTestQuery");
PROPERTIES_MAP = new ConcurrentHashMap<>(16);
INNER_CP_CONFIG_MAP = new ConcurrentHashMap<>(16);
}
Expand Down Expand Up @@ -144,18 +142,16 @@ public static class InnerCPConfig {

private int maximumPoolSize = 10;

private long connectionTimeout = 30_000;
private long connectionTimeout = 1_000;

private long idleTimeout = 600_000;

private long maxLifetime = 1_800_000;

private long validationTimeout = 5_000;
private long validationTimeout = 1_000;

private String connectionInitSql = "select 1";

private String connectionTestQuery = "select 1";

public InnerCPConfig(Properties properties) {
FILED_NAMES.forEach(s -> {
String key = PREFIX + s;
Expand Down Expand Up @@ -255,12 +251,5 @@ public void setConnectionInitSql(String connectionInitSql) {
this.connectionInitSql = connectionInitSql;
}

public String getConnectionTestQuery() {
return connectionTestQuery;
}

public void setConnectionTestQuery(String connectionTestQuery) {
this.connectionTestQuery = connectionTestQuery;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void test0() throws NoSuchFieldException, IllegalAccessException {
Assert.assertEquals(vtMinimumIdle, hikariConfig.getMinimumIdle() + "");
Assert.assertEquals(vtMaximumPoolSize, hikariConfig.getMaximumPoolSize() + "");
Assert.assertEquals("select 1", hikariConfig.getConnectionInitSql() + "");
Assert.assertEquals("select 1", hikariConfig.getConnectionTestQuery() + "");
Assert.assertNull(hikariConfig.getConnectionTestQuery());
Assert.assertEquals(vtConnectionTimeout, hikariConfig.getConnectionTimeout() + "");
Assert.assertEquals(vtIdleTimeout, hikariConfig.getIdleTimeout() + "");
}
Expand Down
Loading