Hikari 数据库连接池配置主从结构

HikariCP是数据库连接池,而且是号称史上最快的,而且目前来看确实是这样的,SpringBoot2.0也已经采用HikariCP作为默认连接池配置.

HikariCP 默认配置
主要参数是在 com.zaxxer.hikari.HikariConfig 中初始化的,部分参数是在 com.zaxxer.hikari.pool.PoolBase 中初始化的。

name 描述 构造器默认值 默认配置validate之后的值 validate重置
autoCommit 自动提交从池中返回的连接 true true -
connectionTimeout 等待来自池的连接的最大毫秒数 SECONDS.toMillis(30) = 30000 30000 如果小于250毫秒,则被重置回30秒
idleTimeout 连接允许在池中闲置的最长时间 MINUTES.toMillis(10) = 600000 600000 如果idleTimeout+1秒>maxLifetime 且 maxLifetime>0,则会被重置为0(代表永远不会退出);如果idleTimeout!=0且小于10秒,则会被重置为10秒
maxLifetime 池中连接最长生命周期 MINUTES.toMillis(30) = 1800000 1800000 如果不等于0且小于30秒则会被重置回30分钟
connectionTestQuery 如果您的驱动程序支持JDBC4,我们强烈建议您不要设置此属性 null null -
minimumIdle 池中维护的最小空闲连接数 -1 10 minIdle<0或者minIdle>maxPoolSize,则被重置为maxPoolSize
maximumPoolSize 池中最大连接数,包括闲置和使用中的连接 -1 10 如果maxPoolSize小于1,则会被重置。当minIdle<=0被重置为DEFAULT_POOL_SIZE则为10;如果minIdle>0则重置为minIdle的值
metricRegistry 该属性允许您指定一个 Codahale / Dropwizard MetricRegistry 的实例,供池使用以记录各种指标 null null -
healthCheckRegistry 该属性允许您指定池使用的Codahale / Dropwizard HealthCheckRegistry的实例来报告当前健康信息 null null -
poolName 连接池的用户定义名称,主要出现在日志记录和JMX管理控制台中以识别池和池配置 null HikariPool-1 -
initializationFailTimeout 如果池无法成功初始化连接,则此属性控制池是否将 fail fast 1 1 -
isolateInternalQueries 是否在其自己的事务中隔离内部池查询,例如连接活动测试 false false -
allowPoolSuspension 控制池是否可以通过JMX暂停和恢复 false false -
readOnly 从池中获取的连接是否默认处于只读模式 false false -
registerMbeans 是否注册JMX管理Bean(MBeans false false -
catalog 为支持 catalog 概念的数据库设置默认 catalog driver default null -
connectionInitSql 该属性设置一个SQL语句,在将每个新连接创建后,将其添加到池中之前执行该语句。 null null -
driverClassName HikariCP将尝试通过仅基于jdbcUrl的DriverManager解析驱动程序,但对于一些较旧的驱动程序,还必须指定driverClassName null null -
transactionIsolation 控制从池返回的连接的默认事务隔离级别 null null -
validationTimeout 连接将被测试活动的最大时间量 SECONDS.toMillis(5) = 5000 5000 如果小于250毫秒,则会被重置回5秒
leakDetectionThreshold 记录消息之前连接可能离开池的时间量,表示可能的连接泄漏 0 0 如果大于0且不是单元测试,则进一步判断:(leakDetectionThreshold < SECONDS.toMillis(2) or (leakDetectionThreshold > maxLifetime && maxLifetime > 0),会被重置为0 . 即如果要生效则必须>0,而且不能小于2秒,而且当maxLifetime > 0时不能大于maxLifetime
dataSource 这个属性允许你直接设置数据源的实例被池包装,而不是让HikariCP通过反射来构造它 null null -
schema 该属性为支持模式概念的数据库设置默认模式 driver default null -
threadFactory 此属性允许您设置将用于创建池使用的所有线程的java.util.concurrent.ThreadFactory的实例。 null null -
scheduledExecutor 此属性允许您设置将用于各种内部计划任务的java.util.concurrent.ScheduledExecutorService实例 null null -

0.maven

  
		    com.zaxxer
		    HikariCP
		    3.2.0
		

1.mysqlContext.xml



       
  
    
    
    
        
        
        
        
      

    
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
    
    
    
        
            
                
                
            
        
    
    
     
        
            
        
    

    
        
        
    

    
    
        
        
    

    
        
    




2.mybatis-config.xml





    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
        
    

    
    	
        
    
    
    
        
    


3.DynamicDataSourceInterceptor


import java.util.Locale;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
				RowBounds.class, ResultHandler.class }) })

public class DynamicDataSourceInterceptor implements Interceptor {

	private static final String REGEX = ".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*|.*xxl_job.*|.*XXL_JOB.*";

	@Override
	public Object intercept(Invocation invocation) throws Throwable {

		boolean syschronizationActive = TransactionSynchronizationManager.isActualTransactionActive();
		Object[] objects = invocation.getArgs();
		MappedStatement mappedStatement = (MappedStatement) objects[0];
		String lookupKey = DynamicDataSourceHolder.DB_MASTER;

		if (syschronizationActive != true) {

			if (mappedStatement.getSqlCommandType().equals(SqlCommandType.SELECT)) {
				if (mappedStatement.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
					lookupKey = DynamicDataSourceHolder.DB_MASTER;
				} else {
					BoundSql boundSql = mappedStatement.getSqlSource().getBoundSql(objects[1]);
					String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
					if (sql.matches(REGEX)) {
						lookupKey = DynamicDataSourceHolder.DB_MASTER;
					} else {
						lookupKey = DynamicDataSourceHolder.DB_SLAVE;
					}
				}

			}
		} else {
			lookupKey = DynamicDataSourceHolder.DB_MASTER;
		}
		DynamicDataSourceHolder.setDbType(lookupKey);
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		if (target instanceof Executor)
			return Plugin.wrap(target, this);
		else
			return target;
	}

	@Override
	public void setProperties(Properties properties) {
	}

}

4.PersistenceInterceptor


import java.util.Collection;
import java.util.Date;
import java.util.Properties;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

@Intercepts({@Signature(
    type = Executor.class,
    method = "update",
    args = {MappedStatement.class, Object.class}
)})
public class PersistenceInterceptor implements Interceptor {
    public PersistenceInterceptor() {
    }

    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement)args[0];
        Object object = args[1];
        NulEntity entity;
        if (mappedStatement.getSqlCommandType().equals(SqlCommandType.INSERT) && ThreadLocalHolder.get() != null) {
            if (object instanceof NulEntity) {
                entity = (NulEntity)object;
                entity.setCreator(ThreadLocalHolder.get().toString());
                entity.setCreatedAt(new Date());
            }

            if (object instanceof ParamMap) {
                ParamMap paramMap = (ParamMap)object;
                Object obj = paramMap.get("param1");
                if (obj instanceof Collection) {
                    Collection list = (Collection)obj;
                    Object[] objects = list.toArray();
                    if (objects[0] instanceof NulEntity) {
                        for(int i = 0; i < objects.length; ++i) {
                            NulEntity entity = (NulEntity)objects[i];
                            entity.setCreator(ThreadLocalHolder.get().toString());
                            entity.setCreatedAt(new Date());
                        }
                    }
                }
            }
        } else if (mappedStatement.getSqlCommandType().equals(SqlCommandType.UPDATE) && ThreadLocalHolder.get() != null && object instanceof NulEntity) {
            entity = (NulEntity)object;
            entity.setModifier(ThreadLocalHolder.get().toString());
            entity.setUpdatedAt(new Date());
            String key = CacheHelper.buildKey(entity.getClass().getName() + ":" + entity.getId());
            CacheHelper.evict(CacheType.FIVE_MINUTE_STORE.getValue(), key);
        }

        return invocation.proceed();
    }

    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    public void setProperties(Properties properties) {
    }
}

5.DynamicDataSourceHolder


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DynamicDataSourceHolder {

	private static final Logger LOG = LoggerFactory.getLogger(DynamicDataSourceHolder.class);

	private static ThreadLocal contextHolder = new ThreadLocal();
	public static final String DB_MASTER = "master";
	public static final String DB_SLAVE = "slave";

	public static String getDbType() {
		String db = contextHolder.get();
		if (db == null)
			db = DB_MASTER;
		if (LOG.isInfoEnabled())
			LOG.info("当前所使用的数据源为:" + db);
		return db;
	}

	public static void setDbType(String datasource) {

		contextHolder.set(datasource);
	}

	public static void clearDbType() {
		contextHolder.remove();
	}

	public static boolean isMaster() {
		return DB_MASTER.equals(getDbType());
	}

}

6.DynamicDataSource

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

	@Override
	protected Object determineCurrentLookupKey() {
		return DynamicDataSourceHolder.getDbType();
	}

}

你可能感兴趣的:(JAVA)