天天看點

HikariCP資料庫連接配接池

快速,簡單,可靠。HikariCP是一個“零開銷”生産就緒JDBC連接配接池。大約130Kb,它的性能幾乎是C3P0、DBCP的25倍,十分強悍

引用大話資料庫連接配接池

配置也是十分簡單

  • maven依賴
Java 8/9 maven artifact:
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.1.0</version>
    </dependency>

Java 7 maven artifact (maintenance mode):
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP-java7</artifactId>
        <version>2.4.13</version>
    </dependency>

Java 6 maven artifact (maintenance mode):
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP-java6</artifactId>
        <version>2.3.13</version>
    </dependency>
           
  • 建立hikari.properties檔案,放到src下或resources下
jdbcUrl=jdbc:mysql://xxx:3306/testdb?useSSL=false&characterEncoding=utf-8
dataSource.user=root
dataSource.password=root

//連接配接的閑置時間
dataSource.maxLifetime=604800
//連接配接池大小
dataSource.maximumPoolSize=18

dataSource.cachePrepStmts = true
dataSource.prepStmtCacheSize = 250
dataSource.prepStmtCacheSqlLimit = 2048
dataSource.useServerPrepStmts = true
dataSource.useLocalSessionState = true
dataSource.rewriteBatchedStatements = true
dataSource.cacheResultSetMetadata = true
dataSource.cacheServerConfiguration = true
dataSource.elideSetAutoCommits = true
dataSource.maintainTimeStats = false
           

其實隻需根據情況配置以下兩項即可,其它都可以用預設

dataSource.maxLifetime=604800

dataSource.maximumPoolSize=18

  • 代碼如下
HikariConfig config = new HikariConfig("/hikari.properties");
HikariDataSource ds = new HikariDataSource(config);
Connection conn = ds.getConnection();
           

繼續閱讀