2012-01-16 53 views
0

我们必须在这里我们使用了弹簧IOC的应用程序。我们在applicationContext.xml中配置了dataSource bean,并在其他bean定义中引用它。数据源初始化启动

数据源bean认定中的样子:

<bean id="dbDataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
     <property name="url" 
      value="jdbc:oracle:oci:@TESTDB" /> 
     <property name="username" value="TESTUSER" /> 
     <property name="password" value="TESTPWD" /> 
     <property name="initialSize" value="50" /> 
     <property name="maxActive" value="40" /> 
     <property name="maxIdle" value="10" /> 
     <property name="minIdle" value="10" /> 
     <property name="maxWait" value="-1" /> 
    </bean> 
    <bean id="serviceDAO" class="com.test.impl.ServiceDAOImpl"> 
     <property name="dataSource" ref="dbDataSource" /> 
    </bean> 

ServiceDAOImpl如下所示:

public class ServiceDAOImpl implements ServiceDAO { 


    private JdbcTemplate jdbcTemplate; 

    public void setDataSource(DataSource dataSource) { 
     this.jdbcTemplate = new JdbcTemplate(dataSource); 
    } 
    @SuppressWarnings({ "rawtypes", "unchecked" }) 
    public ValueObj readValue(String key) { 

     String query = "SELECT * FROM SERVICE_LOOKUP WHERE KEY=?"; 
     /** 
     * Implement the RowMapper callback interface 
     */ 
     return (ValueObj) jdbcTemplate.queryForObject(query, 
       new Object[] { key }, new RowMapper() { 
        public Object mapRow(ResultSet resultSet, int rowNum) 
          throws SQLException { 
         return new ValueObj(resultSet.getString("KEY"), 
           resultSet.getString("VALUE")); 
        } 
       }); 
    } 
    public ServiceDAOImpl() { 

    } 
} 

现在,在服务器启动时注入正在发生细微而当我们使用数据源中serviceDAOImpl的连接正在发生。但是,第一次进行数据库调用需要大约3分钟才能获得响应。我想,这是因为池创建的第一个电话中进行,我们在applicationConext.xml设置参数"initialSize" = 50

因此,为了避免这一点,我们需要能够在应用程序启动时创建本身可直接使用的池的方式。

请建议。如果需要澄清,请告知我。

问候 Saroj

+0

春天确实渴望单身bean创建/默认初始化所以创建池时被读取并配置了环境。还有其他一些事情必须导致延迟。 – soulcheck 2012-01-16 12:34:42

+0

Soulcheck嗨, 许多感谢名单为您回复。但我已经用initialSize参数值为1测试了代码,时间缩短为6秒。 – user1061771 2012-01-16 13:19:29

回答

4

有一个解决这个。你可能会迫使JdbcTemplate的使用在启动时 DB连接。有关详细说明,请参阅链接here

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <constructor-arg index="0" ref="dataSource"/> 
    <constructor-arg index="1" value="false"/> 
</bean> 

第二个构造函数arg是懒惰的Init标志。

+0

亚拉文你好,感谢名单很多关于您的输入。我已经完成了这个,现在它工作正常。现在,在服务器启动期间注入数据源,从而提高应用程序的性能。再次感谢您的帮助。我appriciate !!! – user1061771 2012-01-18 09:19:17

+0

@ user1061771随时欢迎:)。我很高兴我能提供帮助。 – 2012-01-18 09:40:16

+1

对不起,愚蠢的问题......你会怎么用弹簧注释,而不是这样做?非常感谢! – 2015-06-15 13:05:17

0

亚拉文A的解决方案是优先停留之一,但以防万一你不希望定义一个额外的豆你可以点春天的DAO的init方法:

<bean id="serviceDAO" class="com.test.impl.ServiceDAOImpl" init-method="init"> 
    <property name="dataSource" ref="dbDataSource" /> 
</bean> 

,然后定义ServiceDAOImpl.init()这就要求一些SQL LIKE SELECT 1 FROM SERVICE_LOOKUP LIMIT 1,甚至更好一些空操作,像SELECT 1

public class ServiceDAOImpl implements ServiceDAO { 
    public void init() { 
     String query = "SELECT 1 FROM SERVICE_LOOKUP LIMIT 1"; 

     int i = jdbcTemplate.queryForInt(query); 
    } 
}