HiKariCP数据库连接池

HiKariCP是数据库连接池的一个后起之秀,号称性能最好,可以完美地PK掉其他连接池

官网:https://github.com/brettwooldridge/HikariCP

 

Java 8 maven artifact:

    
        com.zaxxer
        HikariCP
        2.6.2
    

Java 9 Early Access maven artifact:

    
        com.zaxxer
        HikariCP-java9ea
        2.6.1
    

Java 7 maven artifact (maintenance mode):

    
        com.zaxxer
        HikariCP-java7
        2.4.12
    

Java 6 maven artifact (maintenance mode):

    
        com.zaxxer
        HikariCP-java6
        2.3.13
     
  

Initialization

You can use the HikariConfig class like so1:

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);

 1 MySQL-specific example, do not copy verbatim.

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:

// Examines both filesystem and classpath for .properties file
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

or java.util.Properties based:

Properties props = new Properties();
props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
props.setProperty("dataSource.user", "test");
props.setProperty("dataSource.password", "test");
props.setProperty("dataSource.databaseName", "mydb");
props.put("dataSource.logWriter", new PrintWriter(System.out));

HikariConfig config = new HikariConfig(props);
HikariDataSource ds = new HikariDataSource(config);

There is also a System property available, hikaricp.configurationFile, that can be used to specify the location of a properties file. If you intend to use this option, construct a HikariConfig or HikariDataSource instance using the default constructor and the properties file will be loaded.

Spring配置文件

 

 

destroy-method="shutdown">


 

HikariCP所做的一些优化,总结如下:

  • 字节码精简:优化代码,直到编译后的字节码最少,这样,CPU缓存可以加载更多的程序代码;
  • 优化代理和拦截器:减少代码,例如HikariCP的Statement proxy只有100行代码,只有BoneCP的十分之一;
  • 自定义数组类型(FastStatementList)代替ArrayList:避免每次get()调用都要进行range check,避免调用remove()时的从头到尾的扫描;
  • 自定义集合类型(ConcurrentBag):提高并发读写的效率;
  • 其他针对BoneCP缺陷的优化,比如对于耗时超过一个CPU时间片的方法调用的研究(但没说具体怎么优化)。

你可能感兴趣的:(HiKariCP数据库连接池)