2017-06-16 1728 views
0

我正在與HikariCP2.6.1HikariPool PoolInitializationException添加了一些@Test方法

我正在與MySQLJUnit 4.x和平。就在今天,我添加了一個新的@Test方法,現在測試失敗。如果我把@Ignore這個新的@Test所有的工作當然。如果我把@Ignore到其他以前的@Test那通過,併到這個新的@Test我刪除@Ignore所有的工作。

因此加入新的@Test方法,工程或需要DB連接出現以下異常:

Caused by: org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [javax.sql.DataSource]: 
Factory method 'dataSource' threw exception; 
nested exception is com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: 
Failed to initialize pool: Could not create connection to database server. Attempted reconnect 3 times. Giving up. 
    a 
... 
Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: 
Failed to initialize pool: Could not create connection to database server. Attempted reconnect 3 times. Giving up. 
    a 
... 
Caused by: java.sql.SQLNonTransientConnectionException: 
Could not create connection to database server. 
Attempted reconnect 3 times. Giving up. 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:526) 
... 
Caused by: java.lang.ArrayIndexOutOfBoundsException: 24 
    at com.mysql.cj.mysqla.io.Buffer.readInteger(Buffer.java:271) 

我目前的設置是:

@Bean(destroyMethod = "close") 
public DataSource dataSource() throws SQLException { 

    HikariConfig hc = new HikariConfig(); 

    hc.setDriverClassName(environment.getProperty("mysql.driverClass")); 
    hc.setJdbcUrl(environment.getProperty("mysql.jdbcUrl")); 
    hc.setUsername(environment.getProperty("mysql.user")); 
    hc.setPassword(environment.getProperty("mysql.password")); 

    hc.setPoolName(environment.getProperty("hikaricp.poolName")); 

    hc.addDataSourceProperty("databaseName", environment.getProperty("hikaricp.databaseName")); 
    hc.addDataSourceProperty("cachePrepStmts", environment.getProperty("hikaricp.cachePrepStmts")); 
    hc.addDataSourceProperty("prepStmtCacheSize", environment.getProperty("hikaricp.prepStmtCacheSize")); 
    hc.addDataSourceProperty("prepStmtCacheSqlLimit", environment.getProperty("hikaricp.prepStmtCacheSqlLimit")); 
    hc.addDataSourceProperty("useServerPrepStmts", environment.getProperty("hikaricp.useServerPrepStmts")); 

    HikariDataSource hds = new HikariDataSource(hc); 

    return hds; 
} 

而且

hikaricp.poolName = "hikaricp-channels-pool" 
hikaricp.databaseName = batch 
hikaricp.cachePrepStmts = true 
hikaricp.prepStmtCacheSize = 250 
hikaricp.prepStmtCacheSqlLimit = 2048 
hikaricp.useServerPrepStmts = true 

mysql.user = root 
mysql.password = somepassword 
mysql.driverClass = com.mysql.cj.jdbc.Driver 
mysql.jdbcUrl = jdbc:mysql://localhost:3306/batch?autoReconnect=true&useSSL=false 

需要額外的配置嗎?

阿爾法

版01

  • 來源:mysql.jdbcUrl = jdbc:mysql://localhost:3306/batch?autoReconnect=true&useSSL=false
  • 要:mysql.jdbcUrl = jdbc:mysql://localhost:3306/batch?useSSL=false

版02

  • 補充:hc.addDataSourceProperty("maximumPoolSize", 2);

關於DEBUG報告:

111483 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceJNDI..................none 
111483 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceProperties............{password=<masked>, prepStmtCacheSqlLimit=2048, databaseName=batch, cachePrepStmts=true, useServerPrepStmts=true, maximumPoolSize=2, prepStmtCacheSize=250} 
111483 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - driverClassName................."com.mysql.cj.jdbc.Driver" 
111483 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckProperties...........{} 
111484 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckRegistry.............none 
111484 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - idleTimeout.....................600000 
111484 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - initializationFailFast..........true 
111485 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - initializationFailTimeout.......1 
111485 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - isolateInternalQueries..........false 
111485 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - jdbc4ConnectionTest.............false 
111485 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - jdbcUrl........................."jdbc:mysql://localhost:3306/batch?useSSL=false" 
111486 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - leakDetectionThreshold..........0 
111486 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - maxLifetime.....................1800000 
111486 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - maximumPoolSize.................10 
111486 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - metricRegistry..................none 
111486 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - metricsTrackerFactory...........none 
111487 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - minimumIdle.....................10 
111487 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - password........................<masked> 
111487 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - poolName........................""hikaricp-channels-pool"" 
111487 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - readOnly........................false 
111487 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - registerMbeans..................false 
111488 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutor...............none 
111488 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutorService........internal 
111488 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - threadFactory...................internal 
111488 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation............default 
111488 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - username........................"root" 
111488 [Test worker] DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout...............5000 

從上面我沒有意識到我的hc.addDataSourceProperty("maximumPoolSize", 2)體現在

dataSourceProperties............{ 
    password=<masked>, 
    prepStmtCacheSqlLimit=2048, 
    databaseName=batch, 
    cachePrepStmts=true, 
    useServerPrepStmts=true, 
    maximumPoolSize=2, <------ 
    prepStmtCacheSize=250} 

maximumPoolSize.................10

測井輸出遵循與此錯誤堆棧跟蹤

111488 [Test worker] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Starting... 
111495 [Test worker] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - Added connection [email protected] 
111495 [Test worker] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Start completed. 
111595 ["hikaricp-channels-pool" housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - Pool stats (total=10, active=0, idle=10, waiting=0) 
111598 ["hikaricp-channels-pool" connection adder] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - Added connection [email protected] 
111606 ["hikaricp-channels-pool" connection adder] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Failed to create/setup connection: Could not create connection to database server. 
111607 ["hikaricp-channels-pool" connection adder] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - Cannot acquire connection from data source 
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. 
... 
Caused by: java.lang.ArrayIndexOutOfBoundsException: 24 
    at com.mysql.cj.mysqla.io.Buffer.readInteger(Buffer.java:271) 
    at com.mysql.cj.mysqla.io.MysqlaCapabilities.setInitialHandshakePacket(MysqlaCapabilities.java:62) 

顯示於其它地方的其它相同的錯誤消息與:

Pool stats (total=1, active=0, idle=1, waiting=0)

多頭上面所示的兩個輸出都位於中部.log文件。就在底部,出現以下很多次。

119835 [Thread-7] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
119824 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119828 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119836 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119829 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119829 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119838 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119839 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119839 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119839 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119839 [Thread-5] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
119839 [Thread-5] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
119832 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119835 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119840 [Thread-17] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
119841 [Thread-17] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
119841 [Thread-6] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
119841 [Thread-6] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
119840 [Thread-16] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
119843 [Thread-16] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
119840 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119841 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119845 ["hikaricp-channels-pool" connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - "hikaricp-channels-pool" - Closing connection [email protected]: (connection evicted) 
119845 [Thread-15] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
119846 [Thread-15] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
119846 [Thread-10] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
119846 [Thread-10] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 
123927 [Thread-19] DEBUG com.zaxxer.hikari.pool.HikariPool - "hikaricp-channels-pool" - After shutdown stats (total=0, active=0, idle=0, waiting=0) 
123928 [Thread-19] INFO com.zaxxer.hikari.HikariDataSource - "hikaricp-channels-pool" - Shutdown completed. 

現在只是改變:

//hc.addDataSourceProperty("maximumPoolSize", 2); 
    hc.setMaximumPoolSize(2); 

所有現在通過。

從這個經驗,我會通過GitHub出現一些'問題'如何改進當前文檔的建議。

回答

1

我想你絕對不想autoReconnect。此外,對於測試,您可能不需要池大小爲10,請嘗試1或2.還要爲com.zaxxer.hikari啓用DEBUG日誌記錄,並確保池在測試之間正確關閉。

+0

添加了'Alpha'部分,只是好奇和困惑,爲什麼使用'2'而不能使用'10'。 –