Configuring a data source using a JDBC driver

Using a JDBC driver to configure a data-source bean is the simplest data source in Spring. The three data source classes provided by Spring are as follows:

  • DriverManagerDataSource: It always creates a new connection for every connection request
  • SimpleDriverDataSource: It is similar to the DriverManagerDataSource except that it works with the JDBC driver directly
  • SingleConnectionDataSource: It returns the same connection for every connection request, but it is not a pooled data source

Let's see the following code for configuring a data source bean using the DriverManagerDataSource class of Spring in your application:

In Java-based configuration, the code is as follows:

    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName("org.h2.Driver"); 
    dataSource.setUrl("jdbc:h2:tcp://localhost/bankDB"); 
    dataSource.setUsername("root"); 
    dataSource.setPassword("root"); 

In XML-based configuration, the code will be like this:

    <bean id="dataSource"
class="org.springframework.jdbc.datasource
.DriverManagerDataSource"> <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"/> </bean>

The data source defined in the preceding code is a very simple data source, and we can use it in the development environment. It is not a suitable data source for production. I, personally, prefer to use JNDI to configure the data source for the production environment. Let's see how.

Let's implement the object pool design pattern to provide data source objects by configuring the data source using JNDI.

In a Spring application, you can configure a data source by using the JNDI lookup. Spring provides the <jee:jndi-lookup> element from Spring's JEE namespace. Let's see the code for this configuration.

In XML configuration, the code is given as follows:

    <jee:jndi-lookup id="dataSource"
jndi-name="java:comp/env/jdbc/datasource" />

In Java configuration, the code is as follows:

    @Bean 
    public JndiObjectFactoryBean dataSource() { 
      JndiObjectFactoryBean jndiObject = new JndiObjectFactoryBean(); 
      jndiObject.setJndiName("jdbc/datasource"); 
      jndiObject.setResourceRef(true); 
      jndiObject.setProxyInterface(javax.sql.DataSource.class); 
      return jndiObject; 
    } 

Application servers like WebSphere or JBoss allow you to configure data sources to be prepared via JNDI. Even a web container like Tomcat allows you to configure data sources to be prepared via JNDI. These servers manage the data sources in your application. It is beneficial, because the performance of the data source will be greater, as the application servers are often pooled. And they can be managed completely external to the application. This is one of the best ways to configure a data source to be retrieved via JNDI. If you are not able to retrieve through the JNDI lookup in production, you can choose another, better option, which we'll discuss next.

..................Content has been hidden....................

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