為何要使用HiKariCP?這要先從BoneCP說起:
什么?不是有C3P0/DBCP這些成熟的數據庫連接池嗎?一直用的好好的,為什么又搞出一個BoneCP來?因為,傳說中BoneCP在快速這個特點上做到了極致,官方數據是C3P0等的25倍左右。不相信?其實我也不怎么信。可是,有圖有真相啊(圖片來自BoneCP官網:http://jolbox.com/benchmarks.html):



英文:
constrained 不舒服的,被強迫的,拘泥的;
- 字節碼精簡:優化代碼,直到編譯后的字節碼最少,這樣,CPU緩存可以加載更多的程序代碼;
- 優化代理和攔截器:減少代碼,例如HikariCP的Statement proxy只有100行代碼,只有BoneCP的十分之一;
- 自定義數組類型(FastStatementList)代替ArrayList:避免每次get()調用都要進行range check,避免調用remove()時的從頭到尾的掃描;
- 自定義集合類型(ConcurrentBag):提高並發讀寫的效率;
- 其他針對BoneCP缺陷的優化,比如對於耗時超過一個CPU時間片的方法調用的研究(但沒說具體怎么優化)。

使用方法:
you can use the HikariConfig class like so:
HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons"); config.setUsername("bart"); config.setPassword("51mp50n"); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); HikariDataSource ds = new HikariDataSource(config);
or directly instantiate a HikariDataSource like so:
HikariDataSource ds = new HikariDataSource(); ds.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons"); ds.setUsername("bart"); ds.setPassword("51mp50n"); ...
or property file based:
HikariConfig config = new HikariConfig("some/path/hikari.properties"); HikariDataSource ds = new HikariDataSource(config);
Example property file:
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=test dataSource.password=test dataSource.databaseName=mydb dataSource.portNumber=5432 dataSource.serverName=localhost
- <!-- Hikari Datasource -->
- <bean id="dataSourceHikari" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">
- <!-- <property name="driverClassName" value="${db.driverClass}" /> --> <!-- 無需指定,除非系統無法自動識別 -->
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="${db.username}" />
- <property name="password" value="${db.password}" />
- <!-- 連接只讀數據庫時配置為true, 保證安全 -->
- <property name="readOnly" value="false" />
- <!-- 等待連接池分配連接的最大時長(毫秒),超過這個時長還沒可用的連接則發生SQLException, 缺省:30秒 -->
- <property name="connectionTimeout" value="30000" />
- <!-- 一個連接idle狀態的最大時長(毫秒),超時則被釋放(retired),缺省:10分鍾 -->
- <property name="idleTimeout" value="600000" />
- <!-- 一個連接的生命時長(毫秒),超時而且沒被使用則被釋放(retired),缺省:30分鍾,建議設置比數據庫超時時長少30秒,參考MySQL wait_timeout參數(show variables like '%timeout%';) -->
- <property name="maxLifetime" value="1800000" />
- <!-- 連接池中允許的最大連接數。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count) -->
- <property name="maximumPoolSize" value="15" />
- </bean>
下載地址及依賴環境
======================1 下載地址
http://mvnrepository.com/artifact/com.zaxxer/HikariCP
2 Git主頁
https://github.com/brettwooldridge/HikariCP
3 依賴包
log4j-1.2.16.jar
slf4j-api-1.5.10.jar
slf4j-log4j12-1.5.10.jar
4 版本支持
Java 7 and Java 8 maven artifact:
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>2.4.5</version> </dependency>
Java 6 maven artifact (maintenance mode):
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP-java6</artifactId> <version>2.3.13</version> </dependency>
What do “Connection Cycle ops/ms” and “Statement Cycle ops/ms” mean in the context of a microbenchmark of a connection pool?
In the context of a pool, we're trying to measure just the speed of pool operations -- so a "no-op" DataSource is used that does not perform actually connections or SQL. Connection Cycles measures how fast a connection can be obtained from the pool and then returned. Basically, this:
Connection conn = dataSource.getConnection();
conn.close();
Where dataSource is a HikariDataSource (pool) and conn.close() actually returns the Connection to the pool instead of closing the underlying DB Connection.
The Statement Cycle benchmark performs:
Statement statement = connection.createStatement();
statement.execute();
statement.close();
Because connection pools wrap Statement (and it's subclasses) with proxies, and track them so that they can be closed when the Connection is closed, there is overhead associated with both tracking the Statement and invoking against it. This micro-benchmark measures both.
proxool 更新時間截止2008年。速度可以,穩定性稍差,發較高的情況下會出錯。
c3p0 太古老,代碼及其復雜,不利於維護。貌似都比它強。
dbcp 是 apache 上的一個 java 連接池項目,也是 tomcat 使用的連接池組件。
druid 功能比較全面,且擴展性較好,比較方便對jdbc接口進行監控跟蹤等。
BoneCP 13年前最快的連接池項目。2013年后不再更新,心灰意冷。
HikariCP 光連接池
