先来看看jboss数据源的整体类图。
1.入口类LocalTxDataSource,负责对外参数设置和初始化框架
2.获取数据库连接的入口类WrapperDataSource,其实现了DataSource接口
3.ManagedConnectionFactory接口用来获取真实连接和创建WrapperDataSource
4.LocalManagedConnectionFactory负责从driver获取连接,driver层面的参数都在这里维护
5.JBossManagedConnectionPool维护着连接池必须的参数,管理着连接池的状态
6.ManagedConnectionPool提供了获取和归还连接的功能
接下来看一下jboss数据源的初始化过程,主要关注一个问题:数据源连接池如何从配置的参数启动?
入口代理类为LocalTxDataSource,一般使用方式如下
LocalTxDataSource localTxDataSource = new LocalTxDataSource(); localTxDataSource.setName(dataSourceDO.getDsName()); localTxDataSource.setBackgroundValidation(dataSourceDO.isBackgroundValidation()); localTxDataSource.setBackGroundValidationMinutes(dataSourceDO .getBackgroundValidationMinutes()); localTxDataSource.setBlockingTimeoutMillis(dataSourceDO.getBlockingTimeoutMillis()); localTxDataSource.setCheckValidConnectionSQL(dataSourceDO.getCheckValidConnectionSQL()); localTxDataSource.setConnectionProperties(dataSourceDO.getConnectionProperties()); localTxDataSource.setConnectionURL(dataSourceDO.getConnectionURL()); localTxDataSource.setDriverClass(dataSourceDO.getDriverClass()); localTxDataSource.setExceptionSorterClassName(dataSourceDO.getExceptionSorterClassName()); localTxDataSource.setIdleTimeoutMinutes(dataSourceDO.getIdleTimeoutMinutes()); localTxDataSource.setMaxSize(dataSourceDO.getMaxPoolSize()); localTxDataSource.setMinSize(dataSourceDO.getMinPoolSize()); localTxDataSource.setNewConnectionSQL(dataSourceDO.getNewConnectionSQL()); localTxDataSource.setNoTxSeparatePools(dataSourceDO.isNoTxSeparatePools()); localTxDataSource.setPassword(dataSourceDO.getPassWord()); localTxDataSource.setEncPassword(dataSourceDO.getEncPassword()); localTxDataSource.setPrefill(dataSourceDO.isPrefill()); localTxDataSource.setPreparedStatementCacheSize(dataSourceDO .getPreparedStatementCacheSize()); localTxDataSource.setQueryTimeout(dataSourceDO.getQueryTimeout()); localTxDataSource.setSharePreparedStatements(dataSourceDO.isSharePreparedStatements()); localTxDataSource.setTrackStatements(dataSourceDO.getTrackStatements()); localTxDataSource.setTransactionIsolation(dataSourceDO.getTransactionIsolation()); localTxDataSource.setTxQueryTimeout(dataSourceDO.isTxQueryTimeout()); localTxDataSource.setUseFastFail(dataSourceDO.isUseFastFail()); localTxDataSource.setUserName(dataSourceDO.getUserName()); localTxDataSource.setValidateOnMatch(dataSourceDO.isValidateOnMatch()); localTxDataSource.setValidConnectionCheckerClassName(dataSourceDO .getValidConnectionCheckerClassName()); localTxDataSource.setCriteria(dataSourceDO.getCriteria()); localTxDataSource.init(zdatasource);
初始化过程如下:
public void init(ZDataSource zdatasource) throws Exception { initPool(); initConnectionManager(); pool.start(); initDataSource(zdatasource); this.zdatasource = zdatasource; }
其最核心的start方法
public void start() throws Exception { pool.startService(); connectionManager.setPoolingStrategy(pool.getPoolingStrategy()); connectionManager.startService(); if (logger.isDebugEnabled()) { logger.debug("Connection pool " + name + " is started"); } }
启动pool,根据criteria配置生成不同的Pool类型,默认OnePool
public void startService() throws Exception { if ("ByContainerAndApplication".equals(criteria)) poolingStrategy = new PoolBySubjectAndCri(mcf, name, poolParams, noTxSeparatePools, log); else if ("ByContainer".equals(criteria)) poolingStrategy = new PoolBySubject(mcf, name, poolParams, noTxSeparatePools, log); else if ("ByApplication".equals(criteria)) poolingStrategy = new PoolByCri(mcf, name, poolParams, noTxSeparatePools, log); else // "ByNothing".equals(criteria) poolingStrategy = new OnePool(mcf, name, poolParams, noTxSeparatePools, log); }
启动ConnectionManager,顺便做下预热
public void startService() { poolingStrategy.setConnectionListenerFactory(this); if (poolingStrategy instanceof PreFillPoolSupport) { PreFillPoolSupport prefill = (PreFillPoolSupport) poolingStrategy; if (prefill.shouldPreFill()) { prefill.prefill(); } } }
最后初始化datasource
private void initDataSource(ZDataSource zdatasource) throws ResourceException { datasource = (DataSource) mcf.createConnectionFactory(new ConnectionManagerDelegate(), this.name, zdatasource); } public Object createConnectionFactory(ConnectionManager cm, String dataSourceName, ZDataSource zdatasource) throws ResourceException { return new WrapperDataSource(this, cm, dataSourceName, zdatasource); }