BoneCP数据库连接池源码解析

传统的连接池比如proxool,会使用List存放所有的连接,通过读写锁来修改连接的状态,标示该连接是否是可用,而BoneCP采用了分区的方式提高了获取数据库连接的并发性,类似ConcurrentHashMap解决并发问题的思想。

 

下面看下主要的几个类:

  • BoneCPDataSource:实现了DataSource接口,接收数据库的配置并对外提供获取数据库连接的功能,第一次获取连接时会初始化连接池BoneCP对象。
public Connection getConnection() throws SQLException {
		
		FinalWrapper<BoneCP> wrapper = this.pool;

        if (wrapper == null) {
                synchronized (this) {
                        if (this.pool == null) {
                        	try{
                        		if (this.getDriverClass() != null){
                        			loadClass(this.getDriverClass());
                        		}
    					
                        		logger.debug(this.toString());
                        		this.pool = new FinalWrapper<BoneCP>(new BoneCP(this));
    				    
                        	} catch (ClassNotFoundException e) {
                        		throw new SQLException(PoolUtil.stringifyException(e));
                        	}
                        }

                        wrapper = this.pool;
                } 
        }

        return wrapper.value.getConnection();
     }

 

  • BoneCp:负责连接池的初始化、获取数据库连接的功能,初始化流程:
  1. 根据配置文件中分区的个数创建ConnectionPartition对象,并根据配置文件中的最小连接数为每个分区创建连接
  2. 为每个ConnectionPartition创建三个线程,PoolWatchThread用于数据库新连接的建立,ConnectionMaxAgeThread用于连接最大存活时间的检查,ConnectionTesterThread最大空闲时间的检查
    	public BoneCP(BoneCPConfig config) throws SQLException {
    		Class<?> clazz;
    		try {
    			jvmMajorVersion = 5;
    			clazz = Class.forName(connectionClass , true, config.getClassLoader());
    			clazz.getMethod("createClob"); // since 1.6
    			jvmMajorVersion = 6;
    			clazz.getMethod("getNetworkTimeout"); // since 1.7
    			jvmMajorVersion = 7;
    		} catch (Exception e) {
    			// do nothing
    		}
    		try {
    			this.config = Preconditions.checkNotNull(config).clone(); // immutable
    		} catch (CloneNotSupportedException e1) {
    			throw new SQLException("Cloning of the config failed");
    		}
    		this.config.sanitize();
    
    		this.statisticsEnabled = config.isStatisticsEnabled();
    		this.closeConnectionWatchTimeoutInMs = config.getCloseConnectionWatchTimeoutInMs();
    		this.poolAvailabilityThreshold = config.getPoolAvailabilityThreshold();
    		this.connectionTimeoutInMs = config.getConnectionTimeoutInMs();
    
    		if (this.connectionTimeoutInMs == 0){
    			this.connectionTimeoutInMs = Long.MAX_VALUE;
    		}
    		this.nullOnConnectionTimeout = config.isNullOnConnectionTimeout();
    		this.resetConnectionOnClose = config.isResetConnectionOnClose();
    		this.clientInfo = jvmMajorVersion > 5  ? config.getClientInfo() : null;
    		AcquireFailConfig acquireConfig = new AcquireFailConfig();
    		acquireConfig.setAcquireRetryAttempts(new AtomicInteger(0));
    		acquireConfig.setAcquireRetryDelayInMs(0);
    		acquireConfig.setLogMessage("Failed to obtain initial connection");
    
    		if (!config.isLazyInit()){
    			try{
    				Connection sanityConnection = obtainRawInternalConnection();
    				sanityConnection.close();
    			} catch (Exception e){
    				if (config.getConnectionHook() != null){
    					config.getConnectionHook().onAcquireFail(e, acquireConfig);
    				}
    				throw PoolUtil.generateSQLException(String.format(ERROR_TEST_CONNECTION, config.getJdbcUrl(), config.getUsername(), PoolUtil.stringifyException(e)), e);
    
    			}
    		}
    		if (!config.isDisableConnectionTracking()){
    			this.finalizableRefQueue = new FinalizableReferenceQueue();
    		}
    
    		this.asyncExecutor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    
    		this.config = config;
    		this.partitions = new ConnectionPartition[config.getPartitionCount()];
    		String suffix = "";
    
    		if (config.getPoolName()!=null) {
    			suffix="-"+config.getPoolName();
    		}
    
    
    		this.keepAliveScheduler =  Executors.newScheduledThreadPool(config.getPartitionCount(), new CustomThreadFactory("BoneCP-keep-alive-scheduler"+suffix, true));
    		this.maxAliveScheduler =  Executors.newScheduledThreadPool(config.getPartitionCount(), new CustomThreadFactory("BoneCP-max-alive-scheduler"+suffix, true));
    		this.connectionsScheduler =  Executors.newFixedThreadPool(config.getPartitionCount(), new CustomThreadFactory("BoneCP-pool-watch-thread"+suffix, true));
    
    		this.partitionCount = config.getPartitionCount();
    		this.closeConnectionWatch = config.isCloseConnectionWatch();
    		this.cachedPoolStrategy = config.getPoolStrategy() != null && config.getPoolStrategy().equalsIgnoreCase("CACHED");
    		if (this.cachedPoolStrategy){
    			this.connectionStrategy = new CachedConnectionStrategy(this, new DefaultConnectionStrategy(this));
    		} else {
    			this.connectionStrategy = new DefaultConnectionStrategy(this);
    		}
    		boolean queueLIFO = config.getServiceOrder() != null && config.getServiceOrder().equalsIgnoreCase("LIFO");
    		if (this.closeConnectionWatch){
    			logger.warn(THREAD_CLOSE_CONNECTION_WARNING);
    			this.closeConnectionExecutor =  Executors.newCachedThreadPool(new CustomThreadFactory("BoneCP-connection-watch-thread"+suffix, true));
    
    		}
    		for (int p=0; p < config.getPartitionCount(); p++){
    
    			ConnectionPartition connectionPartition = new ConnectionPartition(this);
    			this.partitions[p]=connectionPartition;
    			BlockingQueue<ConnectionHandle> connectionHandles = new LinkedBlockingQueue<ConnectionHandle>(this.config.getMaxConnectionsPerPartition());
    
    			this.partitions[p].setFreeConnections(connectionHandles);
    
    			if (!config.isLazyInit()){
    				for (int i=0; i < config.getMinConnectionsPerPartition(); i++){
    					this.partitions[p].addFreeConnection(new ConnectionHandle(null, this.partitions[p], this, false));
    				}
    
    			}
    
    
    			if (config.getIdleConnectionTestPeriod(TimeUnit.SECONDS) > 0 || config.getIdleMaxAge(TimeUnit.SECONDS) > 0){
    
    				final Runnable connectionTester = new ConnectionTesterThread(connectionPartition, this.keepAliveScheduler, this, config.getIdleMaxAge(TimeUnit.MILLISECONDS), config.getIdleConnectionTestPeriod(TimeUnit.MILLISECONDS), queueLIFO);
    				long delayInSeconds = config.getIdleConnectionTestPeriod(TimeUnit.SECONDS);
    				if (delayInSeconds == 0L){
    					delayInSeconds = config.getIdleMaxAge(TimeUnit.SECONDS);
    				}
    				if (config.getIdleMaxAge(TimeUnit.SECONDS) < delayInSeconds
    						&& config.getIdleConnectionTestPeriod(TimeUnit.SECONDS) != 0 
    						&& config.getIdleMaxAge(TimeUnit.SECONDS) != 0){
    					delayInSeconds = config.getIdleMaxAge(TimeUnit.SECONDS);
    				}
    				this.keepAliveScheduler.schedule(connectionTester, delayInSeconds, TimeUnit.SECONDS);
    			}
    
    
    			if (config.getMaxConnectionAgeInSeconds() > 0){
    				final Runnable connectionMaxAgeTester = new ConnectionMaxAgeThread(connectionPartition, this.maxAliveScheduler, this, config.getMaxConnectionAge(TimeUnit.MILLISECONDS), queueLIFO);
    				this.maxAliveScheduler.schedule(connectionMaxAgeTester, config.getMaxConnectionAgeInSeconds(), TimeUnit.SECONDS);
    			}
    			// watch this partition for low no of threads
    			this.connectionsScheduler.execute(new PoolWatchThread(connectionPartition, this));
    		}
    
    		if (!this.config.isDisableJMX()){
    			registerUnregisterJMX(true);
    		}
    
    
    	}
     当需要获取连接时采用策略模式从BoneCP定义的ConnectionPartition数组中随机选择一个获取连接,如果获取不到会尝试其它ConnectionPartition。创建连接的方式是直接使用java中的DriverManager来创建数据库连接,并放到对应ConnectionPartition的队列中。
  • ConnectionPartition:通过LinkedBlockingQueue维护数据库连接,获取连接时从队列中取出一个

  • ConnectionHandle:实现了Connection接口,对于close操作会将连接放回到指定ConnectionPartition的LinkedBlockingQueue队列中,这样就能复用连接,同时缓存创建的statement

  • ConnectionTesterThread和ConnectionMaxAgeThread会根据配置文件定时执行,在执行时会将ConnectionPartition中的LinkedBlockingQueue一次取出,判断连接的各种状态,符合条件后再放回LinkedBlockingQueue中,这期间会对连接池有比较大的性能影响

在用C3P0数据连接池的时候,一旦并发上来就坑不住了,因为C3P0存在BUG,c3p0在从连接池中获取和返回连接的时候,采用了异步的处理方式,使用一个线程池来异步的 把返回关闭了(没有真正关闭)的连接放入连接池中。这样就意味着,我们在调用了从c3p0获得的连接的close方法时,不是立即放回池中的,而是放入一 个事件队列中等待c3p0内部的线程池顺序的处理。这里给出bonecp连接池,用了就知道好了 #bonecp properties #分区数量 bonecp.partitionCount = 1 #每个分区含有的最小连接数 bonecp.minConnectionsPerPartition = 1 #每个分区含有的最大连接数 bonecp.maxConnectionsPerPartition = 2 #每次新增连接的数量 bonecp.acquireIncrement = 1 #连接池阀值,当 可用连接/最大连接 < 连接阀值 时,创建新的连接 bonecp.poolAvailabilityThreshold = 20 #连接超时时间阀值,获取连接时,超出阀值时间,则获取失败,毫秒为单位 bonecp.connectionTimeout = 10000 #连接池助手线程数量,可设置为0,该参数会降低运行速度,但程序有大量连接时,有助于提升高并发程序的性能 bonecp.releaseHelperThreads = 0 #语句助手线程数,可设置为0,该参数会降低运行速度,但程序有大量的查询语句时,有助于提升高并发程序的性能 bonecp.statementReleaseHelperThreads = 0 #测试连接有效性的间隔时间,单位分钟 bonecp.idleConnectionTestPeriod = 60 #连接的空闲存活时间,当连接空闲时间大于该阀值时,清除该连接 bonecp.idleMaxAge = 240 #语句缓存个数,默认是0 bonecp.statementsCacheSize = 5 在Hibernate中使用BoneCP除了需要上面提到的jar包之外,还需要下载一个名为bonecp-provider-0.7.0.jar的bonecp-provider的jar包,它的下载位置是:http://jolbox.com/bonecp/downloads/maven/com/jolbox/bonecp-provider/0.7.0/bonecp-provider-0.7.0.jar。 除此之外,还需要做如下配置: <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" autowire="autodetect"> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop> <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> <prop key="hibernate.connection.url">jdbc:mysql://127.0.0.1/yourdb</prop> <prop key="hibernate.connection.username">root</prop> <prop key="hibernate.connection.password">abcdefgh</prop> <prop key="bonecp.idleMaxAge">240</prop> <prop key="bonecp.idleConnectionTestPeriod">60</prop> <prop key="bonecp.partitionCount">3</prop> <prop key="bonecp.acquireIncrement">10</prop> <prop key="bonecp.maxConnectionsPerPartition">60</prop> <prop key="bonecp.minConnectionsPerPartition">20</prop> <prop key="bonecp.statementsCacheSize">50</prop> <prop key="bonecp.releaseHelperThreads">3</prop> </props> </property> </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值