JDBC, Transactions, Spring Data JPA
July 28, 2021

M03 Q02 - How do you configure a DataSource in Spring? Which bean is very useful for development/test databases?

Data Source is represented by generic interface javax.sql.DataSource which represents any data source for sql database.

The general rule to configure data source is to have the data source bean in your Spring context and you can do this in different ways. It depends on type of the application that you are creating.

One of the approach is to have a Configuration class and inside this Configuration class you should have a method that will return the javax.sql.DataSource bean. And there are different classes that you can choose.

You can use for example following types of javax.sql.DataSource:

  • DriverManagerDataSource – basic JDBC driver connection source
  • BasicDataSource – Apache DBCP for Connection Pooling
  • ComboPooledDataSource - C3P0 for Connection Pool
  • SmartDataSource
  • AbstractDataSource
  • SingleConnectionDataSource
  • TransactionAwareDataSourceProxy
  • DataSourceTransactionManager

So usually when you want to have the simplest connection you can use the DriverManagerDataSource. But for an application that is more demanding, you will probably want to have something that implements connection pooling. Because opening the connection to the database takes some of the resources, it takes some time. So it is actually better to have the connection pool and to distribute the connections from the pool and more of those reusable whenever the connection is closed.

Configuration of Data Source in Spring is dependent on the type of application that is executed.

Type of execution:

  • Standalone – Data Source is configured in @Configuration class and is created as a bean of one of the supported data source types
  • Spring Boot – Data Source is configured through application.properties
spring.datasource.url=jdbc:hsqldb:mem:localhost
spring.datasource.driver-class-name=org.hsqldb.jdbcDriver

By default Spring loads data.sql and schema.sql from the resources folder. This is happening because of spring-boot-starter-data-jdbc dependency.

  • Application Server – Data Source should be fetched from JNDI via JndiDataSourceLookup / JndiTemplate, application server is responsible for creating and managing data source requested in resources configurations of deployment descriptors

So in the application the server you actually want to configure Tomcat or any other runtime application server. For example? you can use GlassFish. I have Tomcat configured for this way.

So the way that it works is that I have the application configuration.

This will be loaded by AnnotationConfigWebApplicationInitializer. I am using Servlet 3 API for this purpose.

And when this module is being compiled it is being compiled into the war file. This war file is being deployed to the Tomcat server. And when Tomcat will load this module first it will call the WebApplicationInitializer which will create the Spring context. During the creating of the Spring context it will load the data source configuration class

Here I am using JndiDataSourceLookup to fetch the database that was created by the application server.

Also, I am using DataSourseInitializer to load all of the db schema and test data.

Now let's look at the web-app folder and context.xml that contains the configuration of the data source that will be visible in the JNDI.

  • Embedded builder - is very similar to the first example but instead of using DataSourceInitializer and DataSourceConfiguration I am using EmbeddedDatabaeBuilder which is by default using HSQLDB

When working with development/test databases, the following beans are very useful:

  • EmbeddedDatabaseBuilder – allows to easily configure H2/HSQLDB embedded database with schema/data initialization scripts
  • DataSourceInitializer / ResourceDatabasePopulator – allows to use schema/data initialization scripts without usage of EmbeddedDatabaseBuilder