Configuring the data source using pool connections

The following open-sources technologies provide pooled data sources:

  • Apache commons DBCP
  • c3p0
  • BoneCP

The following code configures DBCP's BasicDataSource.

The XML-based DBCP configuration is given as follows:

    <bean id="dataSource" 
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName" value="org.h2.Driver"/> <property name="url" value="jdbc:h2:tcp://localhost/bankDB"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="initialSize" value="5"/> <property name="maxActive" value="10"/> </bean>

The Java-based DBCP configuration is as follows:

    @Bean 
    public BasicDataSource dataSource() { 
      BasicDataSource dataSource = new BasicDataSource(); 
      dataSource.setDriverClassName("org.h2.Driver"); 
      dataSource.setUrl("jdbc:h2:tcp://localhost/bankDB"); 
      dataSource.setUsername("root"); 
      dataSource.setPassword("root"); 
      dataSource.setInitialSize(5); 
      dataSource.setMaxActive(10); 
      return dataSource; 
    } 

As you can see in the preceding code, there are many other properties which are introduced for a pooled data sources provider. The properties of the BasicDataSource class in Spring are listed next:

  • initialSize: This is the number of connections created at the time of initialization of the pool.
  • maxActive: This is the maximum number of connections that can be allocated from the pool at the time of initialization of the pool. If you set this value to 0, that means there's no limit.
  • maxIdle: This is the maximum number of connections that can be idle in the pool without extras being released. If you set this value to 0, that means there's no limit.
  • maxOpenPreparedStatements: This is the maximum number of prepared statements that can be allocated from the statement pool at the time of initialization of the pool. If you set this value to 0, that means there's no limit.
  • maxWait: This is the maximum waiting time for a connection to be returned to the pool before an exception is thrown. If you set it to 1, it means wait indefinitely.
  • minEvictableIdleTimeMillis: This is the maximum time duration a connection can remain idle in the pool before it's eligible for eviction.
  • minIdle: This is the minimum number of connections that can remain idle in the pool without new connections being created.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset