Configuring Data Sources in Spring
by, 11-27-2011 at 09:51 PM (23620 Views)
Spring offers a number of options for configuring a data sources via data source beans. These sources include the following:
- Data sources that use JNDI
- Data sources that use JDBC drivers
- Data sources that pool connections
There is significant differences in the features available depending on what you are trying to do. In this tip, I will outline how to configure different data sources and what they are best used for.
If you are looking to do really robust testing such as load or performance testing of your system where you are trying to simulate your live environment or, if you are going into production with your application, I would recommend that you use a JNDI data source. This is often the way that you configure data sources with top end Java EE application server. In Spring, we can configure a data source in JNDI as though it was a straightforward Spring bean. The <jee:jndi-lookup> element is from Spring’s jee namespace. It makes it possible to retrieve any object, including data sources, from JNDI and make it available as a Spring bean. The jndi-name attribute is used to specify the name of the resource in JNDI. Finally if the application is running within a Java application server, then you set the resource-ref property to true so that the value given in jndi- name will be prepended with java:comp/env/
Sometimes you will not be able to access data via JNDI. If that is the case, your next best option is to use a pooled data source directly in Spring. The way to do this is to use the Jakarta Commons Database Connection Pooling (DBCP) project. Although it has a number of data source options for providing pooling, the one which is most often used is the BasicDataSource. The reason is that it is comparable to Springs DriverManagerDataSource. If we wanted to configure a datasource using the BasicDataSource, it would look like the following:Java Code:<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/CarDS" resource-ref="true" />
Note how this is very similar to the standard data source configuration within Spring. The four basic properties define connection information for BasicDataSource. There are several properties that can be used to configure the data source pool itself. You can look at the document for DBCP to get more information on these properties.Java Code:<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:hsql://localhost/springexamples/se" /> <property name="username" value="joe" /> <property name="password" value="" /> <property name="initialSize" value="5" /> <property name="maxActive" value="10" /> </bean>
If you just need basic connectivity to the database during development or basic unit or integration testing, then you can use a JDBC driver. Spring has two data source classes that can be used:
- DriverManagerDataSource - Returns a new connection every time that a connection is requested.
- SingleConnectionDataSource - Returns the same connection every time a connection is requested.
The configuration of either data sources is similar to how we configured DBCP’s BasicDataSource:
The only difference is that since neither DriverManagerDataSource nor Single- ConnectionDataSource provides a connection pool, there are no pool configuration properties to set. So now should be able to easily configure the various data sources in Spring.Java Code:<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:hsql://localhost/springexamples/se" /> <property name="username" value="joe" /> <property name="password" value="" /> </bean>