zoukankan      html  css  js  c++  java
  • dbcp第一次获取连接的时间问题

        最近优化代码,发现第一次调用数据库连接时非常慢,往后便不再发生。经了解,数据库连接是用dbcp管理的,想在网上查找答案,但没有找到。在某人的提醒下决定研究源代码:

        部分源代码如下(BasicDataSource.java):

    protected volatile DataSource dataSource = null;  
    
    public Connection getConnection() throws SQLException {
        return createDataSource().getConnection();
    }
    
    protected synchronized DataSource createDataSource()
            throws SQLException {
            if (closed) {
                throw new SQLException("Data source is closed");
            }
    
            // Return the pool if we have already created it
            if (dataSource != null) {
                return (dataSource);
            }
    
            // create factory which returns raw physical connections
            ConnectionFactory driverConnectionFactory = createConnectionFactory();
    
            // create a pool for our connections
            createConnectionPool();
    
            // Set up statement pool, if desired
            GenericKeyedObjectPoolFactory statementPoolFactory = null;
            if (isPoolPreparedStatements()) {
                statementPoolFactory = new GenericKeyedObjectPoolFactory(null,
                            -1, // unlimited maxActive (per key)
                            GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL,
                            0, // maxWait
                            1, // maxIdle (per key)
                            maxOpenPreparedStatements);
            }
    
            // Set up the poolable connection factory
            createPoolableConnectionFactory(driverConnectionFactory, statementPoolFactory, abandonedConfig);
    
            // Create and return the pooling data source to manage the connections
            createDataSourceInstance();
            
            try {
                for (int i = 0 ; i < initialSize ; i++) {
                    connectionPool.addObject();
                }
            } catch (Exception e) {
                throw new SQLNestedException("Error preloading the connection pool", e);
            }
            
            return dataSource;
        }

    当我第一次们调用BasicDataSource的getConnection()方法时,需要先调用createDataSource()方法,该方法内有下面这一句:

    // Return the pool if we have already created it
    if (dataSource != null) {
        return (dataSource);
    }

    而默认的dataSource是null

    protected volatile DataSource dataSource = null; 

    所以,第一次调用getConnection()方法时,需要先创建dataSource实例,这个实例的创建非常费时间,就导致了我开始说的第一次调用数据库连接非常慢的问题了。那么该如何解决这个问题呢?

    笔者想了两个方法,思路都是一样的,都是要在调用连接之前初始化好dataSource这个实例:

    第一种,在调用连接之前先获取连接,然后将这个连接关掉,这样,就执行了dataSource的实例化;

    第二种,写一个BasicDataSource的子类,用于代替BasicDataSource

    public class BasicDataSourceSub extends BasicDataSource 
    {
        public synchronized DataSource createDataSource()
        {
            try 
            {
                return super.createDataSource();
            } 
            catch (SQLException e)
            {
                throw new RuntimeException();
            }
        }
    }

    将原来protected synchronized DataSource createDataSource()方法变为共有的,在设置完dbcp的属性后执行这个方法,也会让dataSource实例化。

    以上就是我对dbcp第一次获取连接时间的相关经验,如有不到之处,还请大家多多指教。

  • 相关阅读:
    重登陆模式 --ESFramework 4.0 快速上手(07)
    信息处理,分而治之-- ESFramework 使用技巧
    ESFramework 4.0 有哪些优点?
    铁血规则:事件预订与取消预订
    聊天系统Demo,增加文件传送功能(附源码)-- ESFramework 4.0 快速上手(14)
    判定生死的心跳机制 --ESFramework 4.0 快速上手(07)
    Spring中bean注入前后的一些操作:
    Spring中注入List,Set,Map,Properties
    Spring MVC视图解析器:配置多个视图解析器的优先级
    Spring 3 MVC And JSR303 @Valid Example
  • 原文地址:https://www.cnblogs.com/white-knight/p/3190961.html
Copyright © 2011-2022 走看看