/** * 构造 * * @param config 数据库配置 */ public PooledDataSource(DbConfig config) { this.config = config; freePool = new LinkedList<PooledConnection>(); int initialSize = config.getInitialSize(); try { while (initialSize-- > 0) { freePool.offer(newConnection()); } } catch (SQLException e) { throw new DbRuntimeException(e); } }
/** * 直接从连接池中获取连接,如果池中无连接直接抛出异常 * * @return PooledConnection * @throws SQLException */ private PooledConnection getConnectionDirect() throws SQLException { int maxActive = config.getMaxActive(); if (maxActive <= 0 || maxActive < this.activeCount) { // 超过最大使用限制 throw new SQLException("In used Connection is more than Max Active."); } PooledConnection conn = freePool.poll(); if (null == conn) { conn = this.newConnection(); } activeCount++; return conn.open(); }
/** 从数据库连接池中获取数据库连接对象 */ @Override public synchronized Connection getConnection() throws SQLException { return getConnection(config.getMaxWait()); }