In the post Data access in Spring framework it is already discussed in detail that Spring framework uses templates with fixed and call back parts in order to reduce the boiler plate code.
In order to manage the fixed part like getting connection, releasing resources Spring template needs a reference to a DataSource. This post shows different ways to configure datasource in Spring.
Options for getting DataSource in Spring
DataSource is part of the JDBC specification and is a generalized connection factory. It allows a container or a framework to hide connection pooling and transaction management issues from the application code.
When using Spring’s JDBC layer, there are several options to obtain a data source-
- DataSource lookup through JNDI.
- DataSource configuration of your own with a connection pool implementation provided by a third party.
- Data sources that are defined by a JDBC driver.
- DataSource for embedded DataBase.
DataSource lookup through JNDI - Spring configuration
If you are working on a big application there is a good chance that it will be deployed or already deployed in an application server like Weblogic, Jboss or in a web container like Tomcat. These servers let you configure data source to be looked up using JNDI. That way management of data source is with the server.
Using Spring framework’s <jee:jndi-lookup> element from jee namespace data sources can be retrieved from JNDI. That way you can make available your DataSource as spring bean.
As example– If you need a resource (in this case data source) named /jdbc/myds from JNDI then you can use the following configuration
<beans> <jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/myds"/> </beans>
or you can use resource-ref attribute and set it to true so that the value given in jndi-name will be appended with java:comp/env/.
<beans> <jee:jndi-lookup id="myDataSource" jndi-name="/jdbc/myds" resource-ref="true" /> </beans>
If you are using Java configuration then you can use Spring’s JndiObjectFactoryBean to lookup data source from JNDI.
Spring DataSource configuration for third party pooled DataSource
You can also configure a pooled data source directly in Spring. Spring doesn’t provide any pooled data source of its own so you’ll have to use a third party implementation. Some of the open source options which are mostly used are Apache DBCP and C3P0. If you want to configure any of these connection pools as a data source in Spring then it can be done as-
DBCP configuration
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/>
Here all the properties are provided in the properties file jdbc.properties. You can provide values based on the DB used.
C3P0 configuration
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/>
If you want to use Java Configuration then you can create an object of BasicDataSource or ComboPooledDataSource and set the properties.
BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); .............. ..............
- Refer Spring JdbcTemplate Insert, Update And Delete Example to see how to setup datasource and use JDBCTemplate in order to insert, update and delete.
DataSource defined through JDBC driver
Spring provides data source Implementation in the Spring distribution which are meant only for testing purposes and do not provide pooling. These implementations reside in org.springframework.jdbc.datasource package.
- DriverManagerDataSource— Simple implementation of the standard JDBC DataSource interface, configuring the plain old JDBC DriverManager via bean properties, and returning a new Connection from every getConnection call.
- SimpleDriverDataSource— Similar to DriverManagerDataSource except that it provides direct Driver usage which helps in resolving general class loading issues with the JDBC DriverManager within special class loading environments such as OSGi.
- SingleConnectionDataSource— Returns the same connection every time a connection is requested. This implementation wraps a single JDBC Connection which is not closed after use so you can think of it as a data source with a pool of exactly one connection.
You obtain a connection with any of these data source classes as you typically obtain a JDBC connection. Specify the fully qualified classname of the JDBC driver so that the DriverManager can load the driver class. Next, provide a URL that varies between JDBC drivers. (Consult the documentation for your driver for the correct value.) Then provide a username and a password to connect to the database.
Here is an example of how to configure a DriverManagerDataSource in Java code-
DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); dataSource.setUrl("jdbc:hsqldb:hsql://localhost:PORT_NUMBER/SCHEMA_NAME"); dataSource.setUsername("sa"); dataSource.setPassword("");
If your using XML configuration then-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://localhost:PORT_NUMBER/SCHEMA_NAME"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean>
DataSource for embedded DataBase
Many time we use an embedded database like derby or HSQLDB for development or testing purposes. Spring provides an easy way to configure an embedded database using <jdbc:embedded-database> from jdbc namespace.
If you want to configure HSQLDB datasource it can be done as-
<jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="classpath:/db/jdbc/create-schema.sql"/> <jdbc:script location="classpath:/db/jdbc/insert-data.sql"/> </jdbc:embedded-database>
Here type property is set to HSQL indicating that the embedded database should be HSQL database. You can provide the script’s location using <jdbc:script> element.
Same way for Derby it can be provided as -
<jdbc:embedded-database id="dataSource" type="DERBY"> <jdbc:script location="classpath:/db/jdbc/create-schema.sql"/> <jdbc:script location="classpath:/db/jdbc/insert-data.sql"/> </jdbc:embedded-database>
That's all for this topic Configuring DataSource in Spring Framework. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Spring Tutorial Page
Related Topics
You may also like-