protected synchronized DataSource createDataSource()

in src/main/java/org/apache/commons/dbcp2/BasicDataSource.java [529:574]


    protected synchronized DataSource createDataSource() throws SQLException {
        if (closed) {
            throw new SQLException("Data source is closed");
        }

        // Return the pool if we have already created it
        // This is double-checked locking. This is safe since dataSource is
        // volatile and the code is targeted at Java 5 onwards.
        if (dataSource != null) {
            return dataSource;
        }
        synchronized (this) {
            if (dataSource != null) {
                return dataSource;
            }
            jmxRegister();

            // create factory which returns raw physical connections
            final ConnectionFactory driverConnectionFactory = createConnectionFactory();

            // Set up the poolable connection factory
            final PoolableConnectionFactory poolableConnectionFactory;
            try {
                poolableConnectionFactory = createPoolableConnectionFactory(driverConnectionFactory);
                poolableConnectionFactory.setPoolStatements(poolPreparedStatements);
                poolableConnectionFactory.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
                // create a pool for our connections
                createConnectionPool(poolableConnectionFactory);
                final DataSource newDataSource = createDataSourceInstance();
                newDataSource.setLogWriter(logWriter);
                connectionPool.addObjects(initialSize);
                // If timeBetweenEvictionRunsMillis > 0, start the pool's evictor
                // task
                startPoolMaintenance();
                dataSource = newDataSource;
            } catch (final SQLException | RuntimeException se) {
                closeConnectionPool();
                throw se;
            } catch (final Exception ex) {
                closeConnectionPool();
                throw new SQLException("Error creating connection factory", ex);
            }

            return dataSource;
        }
    }