之前数据库层用的mybatis的时候用的默认的POOLED的连接池,后面报错了,我也不知道是什么原因,所以索性换一下连接池,连接池换成了比较高效的hikari,
配置文件 hikari.properties 的内容是:
driverClassName =com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/tst?useUnicode=true&characterEncoding=utf8
username=admin
password=9E5807FFE2BB21D5C7ADB00DDB8547D5
connectionTestQuery=select 1
idleTimeout=600000
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.connectionTimeout=30000
dataSource.maxLifetime=1800000
dataSource.maximumPoolSize=10
mybtaisconfig.xml的内容是:
mybtaisconfig.xml配置文件中com.test.send.utils.datasoureUtils.HikariDataSourceFactory 类中的内容:
package com.test.send.utils.datasoureUtils;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
import com.test.send.utils.DES;
import com.asiainfo.send.utils.ReadPropertiesUrl;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class HikariDataSourceFactory extends UnpooledDataSourceFactory {
public HikariDataSourceFactory(){
HikariConfig config = new HikariConfig("/config/hikari.properties"));
config.setMaximumPoolSize(5);
String passwd=config.getPassword();
try {
String dePassword = DES.decrypt(passwd);
//将解密后的密码写入配置文件
config.setPassword(dePassword);
} catch (Exception e) {
e.printStackTrace();
}
this.dataSource = new HikariDataSource(config);
}
}
由于数据库密码需要加密,所以我采用的这种方式,
在LoadMybatisConfig 类中加载mybtaisconfig.xml配置文件
package com.test.send.utils;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class LoadMybatisConfig {
private static SqlSessionFactory sqlSessionFactory;
public static boolean initDatasource() {
InputStream cfgStream = null;
Reader cfgReader = null;
InputStream proStream = null;
Reader proReader = null;
boolean flag=false;
try {
//读入Mybatis配置文件 com/test/receive/config/mybaitsconfig.xml
String file="com/test/send/config/mybaitsconfig.xml";
cfgStream = Resources.getResourceAsStream(file);
cfgReader = new InputStreamReader(cfgStream);//cfgReader依赖于cfgStream,先关闭cfgStream
//创建sqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfgReader);
//判断是否成功创建sqlSessionFactory
if(sqlSessionFactory!=null)
flag=true;
} catch (Exception e) {
//有异常的话就将返回false
e.printStackTrace();
return flag;
}finally{
closeIO(cfgStream,cfgReader);
closeIO(proStream,proReader);
}
return flag;
}
public static SqlSession getSqlSession() {
return PropertiesRead.sqlSessionFactory.openSession();
}
public static void closeIO(InputStream inputstream,Reader reader){
try {
if (inputstream != null) {
inputstream.close();// 如果此处出现异常,流也会被关闭
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (reader != null) {
reader.close();// 如果此处出现异常,流也会被关闭
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
控制台日志打印的结果:
[2019-09-05 14:40:16,230] (HikariConfig.java:480) DEBUG [main] hikari.HikariConfig (HikariConfig.java:480) - Driver class com.mysql.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@18b4aac2
[2019-09-05 14:40:35,510] (HikariConfig.java:1020) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1020) - HikariPool-1 - configuration:
[2019-09-05 14:40:35,558] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - allowPoolSuspension.............false
[2019-09-05 14:40:35,570] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - autoCommit......................true
[2019-09-05 14:40:35,581] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - catalog.........................none
[2019-09-05 14:40:35,594] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - connectionInitSql...............none
[2019-09-05 14:40:35,607] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - connectionTestQuery............."SELECT 1"
[2019-09-05 14:40:35,618] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - connectionTimeout...............30000
[2019-09-05 14:40:35,629] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - dataSource......................none
[2019-09-05 14:40:35,641] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - dataSourceClassName.............none
[2019-09-05 14:40:35,652] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - dataSourceJNDI..................none
[2019-09-05 14:40:35,673] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - dataSourceProperties............{cacheServerConfiguration=true, useServerPrepStmts=true, elideSetAutoCommits=true, maxLifetime=1800000, cachePrepStmts=true, rewriteBatchedStatements=true, cacheResultSetMetadata=true, useLocalSessionState=true, maintainTimeStats=false, prepStmtCacheSize=250, prepStmtCacheSqlLimit=2048, connectionTimeout=30000, password=}
[2019-09-05 14:40:35,683] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - driverClassName................."com.mysql.jdbc.Driver"
[2019-09-05 14:40:35,693] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - healthCheckProperties...........{}
[2019-09-05 14:40:35,702] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - healthCheckRegistry.............none
[2019-09-05 14:40:35,711] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - idleTimeout.....................600000
[2019-09-05 14:40:35,721] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - initializationFailTimeout.......1
[2019-09-05 14:40:35,732] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - isolateInternalQueries..........false
[2019-09-05 14:40:35,742] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - jdbcUrl.........................jdbc:mysql://localhost:3306/tst?useUnicode=true&characterEncoding=utf8
[2019-09-05 14:40:35,753] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - leakDetectionThreshold..........0
[2019-09-05 14:40:35,763] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - maxLifetime.....................1800000
[2019-09-05 14:40:35,772] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - maximumPoolSize.................5
[2019-09-05 14:40:35,781] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - metricRegistry..................none
[2019-09-05 14:40:35,791] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - metricsTrackerFactory...........none
[2019-09-05 14:40:35,804] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - minimumIdle.....................5
[2019-09-05 14:40:35,829] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - password........................
[2019-09-05 14:40:35,839] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - poolName........................"HikariPool-1"
[2019-09-05 14:40:35,850] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - readOnly........................false
[2019-09-05 14:40:35,861] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - registerMbeans..................false
[2019-09-05 14:40:35,870] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - scheduledExecutor...............none
[2019-09-05 14:40:35,879] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - schema..........................none
[2019-09-05 14:40:35,890] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - threadFactory...................internal
[2019-09-05 14:40:35,898] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - transactionIsolation............default
[2019-09-05 14:40:35,907] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - username........................"admin"
[2019-09-05 14:40:35,918] (HikariConfig.java:1052) DEBUG [main] hikari.HikariConfig (HikariConfig.java:1052) - validationTimeout...............5000
[2019-09-05 14:40:35,935] (HikariDataSource.java:80) INFO [main] hikari.HikariDataSource (HikariDataSource.java:80) - HikariPool-1 - Starting...
[2019-09-05 14:40:43,171] (HikariPool.java:545) DEBUG [main] pool.HikariPool (HikariPool.java:545) - HikariPool-1 - Added connection com.mysql.jdbc.JDBC4Connection@d41f816
[2019-09-05 14:40:43,202] (HikariDataSource.java:82) INFO [main] hikari.HikariDataSource (HikariDataSource.java:82) - HikariPool-1 - Start completed.
[2019-09-05 14:40:43,303] (HikariPool.java:411) DEBUG [HikariPool-1 housekeeper] pool.HikariPool (HikariPool.java:411) - HikariPool-1 - Pool stats (total=1, active=0, idle=1, waiting=0)
[2019-09-05 14:40:43,468] (HikariPool.java:709) DEBUG [HikariPool-1 connection adder] pool.HikariPool$PoolEntryCreator (HikariPool.java:709) - HikariPool-1 - Added connection com.mysql.jdbc.JDBC4Connection@11f8fd60
具体还有别的实现方法,可以参考
源码 GitHub源码链接
另外也可以参考博客另外的实现方式
https://blog.csdn.net/elyacc/article/details/79952851
https://www.cnblogs.com/yoyotl/p/10458412.html
欢迎交流留言!