使用HikkariCP

时间:2018-09-05 13:32:02

标签: java oracle java.util.concurrent hikaricp

使用hikari池在OracleDb上同时运行简单选择查询时,出现套接字超时异常。有什么可以帮助的吗?下面是产生异常的示例代码。

唯一有用的是将线程数减少到2并设置一些时间限制,但这不是解决方案。我已经找到了2014年类似手袋的一些信息,但已在更高版本的hikari中修复。 Java 8。

还是我想知道这是否与查询数据库太频繁有关?我检查了活动的连接和游标,但它们低于最大限制。我想数据库应该处理这样的查询。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class Overflow {

    public static void main(String[] args) throws Exception {

        for (int i = 0; i < 10; i ++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        runQueries();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    private static void runQueries() throws SQLException {
        for (int i = 0; i < 10000; i++) {
            Connection con = DbConnection.getConnection();
            querySelect(con);
        }
    }

    private static void querySelect(Connection con) throws SQLException {
        PreparedStatement st = con.prepareStatement("select 1 from dual");
        st.execute();
        System.out.println("done");
        con.close();
    }

}

class DbConnection {
    private static HikariConfig config = new HikariConfig("hikkari.properties");
    private static HikariDataSource ds = new HikariDataSource(config);

    private DbConnection() {}

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

}

Hikkari设置:

jdbcUrl=jdbc:oracle:thin:@//adress:1521/db
dataSource.user=user
dataSource.password=password
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.connectionTimeout=10000
dataSource.maxLifetime=45000
#dataSource.readOnly=true
autoCommit=true
maximumPoolSize=8
#connectionInitSql=select 1 from dual

一段时间后,此代码会产生套接字超时异常:

2018-09-05 16:17:41,990 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=8, active=8, idle=0, waiting=2) 
2018-09-05 16:17:42,708 [Thread-9] WARN  c.z.hikari.pool.ProxyConnection - HikariPool-1 - Connection oracle.jdbc.driver.T4CConnection@79b0cad1 marked as broken because of SQLSTATE(08006), ErrorCode(17002) 
java.sql.SQLRecoverableException: IO Error: Socket read timed out
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:899)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1175)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1296)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3714)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1378)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
    at Overflow.querySelect(Overflow.java:37)
    at Overflow.runQueries(Overflow.java:31)
    at Overflow.access$0(Overflow.java:28)
    at Overflow$1.run(Overflow.java:19)
    at java.lang.Thread.run(Unknown Source)

Oracle和hikari版本:

<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.2.0</version>

<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>

0 个答案:

没有答案