When we develop a modern web application, we might have lot of REST services in a single page. Usually we face slow page loading in Local Development Environment.
But, The same page loads little faster in PRO because of its high configurations (For example its RAM size and such other things). In order to improve the
performance we have to change certain things in our local. One of the thing we found is Datasource connection pool configuration. When we hit multiple REST service at a
time,all are handled as different request. If each request is a DB call, DriverManagerDataSource cannot perform well.
Since it can handle your multiple DB requests one by one, to load the entire page it will take comparitively much time in local env. In spring’s documentation itself its given that
This class is not an actual connection pool; it does not actually pool Connections. It just serves as simple replacement for a full-blown connection pool, implementing the same standard interface, but creating new Connections on every callSo we have to go for some other datasource which gives you Connection pooling mechanism.
One of such a datasource which gives you a “real” connection pool outside of a JEE container is BasicDataSource.
And we did a small testcase in a heavy page page which has 4 REST calls and each have its own subsequent DAO calls.
Test Case | DriverManagerDataSource(time taken to load the page ) | BasicDataSource(time taken to load the page ) |
---|---|---|
hit the page | 128 seconds | 37 seconds |
Setting up Connection pool for BasicDataSource :
Lets get started with Maven dependency
1
2
3
4
5
| < dependency > < groupId >commons-dbcp</ groupId > < artifactId >commons-dbcp</ artifactId > < version >1.4</ version > </ dependency > |
- This artifact is compatible with JAVA 6. If you are using Java 7 You have to use version 2.0 commons-dbcp
1
2
3
4
5
6
7
8
9
| < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" > < property name = "driverClassName" value = "oracle.jdbc.driver.OracleDriver" ></ property > < property name = "url" value = "jdbc:oracle:thin:@teoramon.bansel.it:1540:ORAMON" ></ property > < property name = "username" value = "SVIL_WKS" ></ property > < property name = "password" value = "wksc_123" ></ property > < property name = "initialSize" value = "25" ></ property > < property name = "maxActive" value = "50" ></ property > < property name = "maxIdle" value = "25" ></ property > </ bean > |
Important Properties in Configuration
The following are the very basic properties1.initialSize :
The initial number of connections that are created when the bean is initialized or the server is started. Default is zero.
2.maxActive:
The maximum number of active connections that can be handled at the same time. Default is 8. And you have to use maxTotal instead of If you are using Java 7
3.maxIdle:
The maximum number of connections that can remain idle in the pool. Default is 8
No comments :
Post a Comment