当前位置:网站首页 > 技术博客 > 正文

hikaricp(HikariCP简介)



In this introductory tutorial, we’ll learn about the HikariCP JDBC connection pool project. This is a very lightweight (at roughly 130Kb) and lightning-fast JDBC connection pooling framework developed by Brett Wooldridge around 2012.

There are several benchmark results available to compare the performance of HikariCP with other connection pooling frameworks, such as c3p0, dbcp2, tomcat, and vibur. For example, the HikariCP team published the below benchmarks (original results available here):

HikariCP-bench-2.6.0

The framework is so fast because the following techniques have been applied:

  • Bytecode-level engineering – some extreme bytecode level engineering (including assembly level native coding) has been done
  • Micro-optimizations – although barely measurable, these optimizations combined boost the overall performance
  • Intelligent use of the Collections framework – the ArrayList was replaced with a custom class, FastList, that eliminates range checking and performs removal scans from head to tail

First, let’s build a sample application to highlight its usage. HikariCP comes with support for all the main versions of JVM. Each version requires its dependency. For Java 8 through 11, we have:

 

HikariCP also supports older JDK versions, like 6 and 7. The appropriate versions can be found here and here, respectively. We can also check the latest versions in the Central Maven Repository.

Now we can create a demo application. Please note that we need to include a suitable JDBC driver class dependency in the pom.xml. If no dependencies are provided, the application will throw a ClassNotFoundException.

We’ll use HikariCP’s DataSource to create a single instance of a data source for our application:

 

One point to note here is the initialization in the static block.

HikariConfig is the configuration class used to initialize a data source. It comes with four well-known, must-use parameters: username, password, jdbcUrl, and dataSourceClassName.

Out of jdbcUrl and dataSourceClassName, we generally use one at a time. However, when using this property with older drivers, we may need to set both properties.

In addition to these properties, there are several other properties available that we may not find offered by other pooling frameworks:

  • autoCommit
  • connectionTimeout
  • idleTimeout
  • maxLifetime
  • connectionTestQuery
  • connectionInitSql
  • validationTimeout
  • maximumPoolSize
  • poolName
  • allowPoolSuspension
  • readOnly
  • transactionIsolation
  • leakDetectionThreshold

HikariCP stands out because of these database properties. It’s even advanced enough to detect connection leaks by itself.

A detailed description of the above properties can be found here.

We can also initialize HikariConfig with a properties file placed in the resources directory:

 

The properties file should look something like this:

 

In addition, we can use *java.util.Properties-*based configuration:

 

Alternatively, we can initialize a data source directly:

 

Now that we have defined the data source, we can use it to obtain a connection from the configured connection pool, and perform JDBC related actions.

Suppose we have two tables, named dept and emp, to simulate an employee-department use case. We’ll write a class to fetch those details from the database using HikariCP.

Below we’ll list the SQL statements necessary to create the sample data:

 

Please note, if we use an in-memory database such as H2, we need to automatically load the database script before running the actual code to fetch the data. Thankfully, H2 comes with an INIT parameter that can load the database script from the classpath at runtime. The JDBC URL should look like:

 

We need to create a method to fetch this data from the database:

 

Then we need to create a JUnit method to test it. Since we know the number of rows in the table emp, we can expect that the size of the returned list should be equal to the number of rows:

 

In this brief article, we learned the benefits of using HikariCP, and its configuration.

As always, the full source code is available over on GitHub.

  • 上一篇: c语言指针指向数组写法
  • 下一篇: 指针详解
  • 版权声明


    相关文章:

  • c语言指针指向数组写法2024-11-07 11:01:04
  • keycode是哪个键2024-11-07 11:01:04
  • python爬虫的url如何选择2024-11-07 11:01:04
  • rbf神经网络和bp神经网络2024-11-07 11:01:04
  • linux wait waitpid2024-11-07 11:01:04
  • 指针详解2024-11-07 11:01:04
  • pyqt5教程菜鸟教程2024-11-07 11:01:04
  • cjson.dll2024-11-07 11:01:04
  • js注释有什么作用2024-11-07 11:01:04
  • js特效怎么使用方法2024-11-07 11:01:04