Configuring DataSource

DataSource can be configured in the application in the following ways:

  • Obtained from the JNDI lookup: Spring helps us maintain a large-scale JavaEE application, which runs in application servers such as Glassfish, JBoss, Weblogic, and Tomcat. All these servers support the facility to configure a pool of data sources, which can be acquired by the Java Naming Directory Interface (JNDI) using the lookup mechanism. The lookup helps developers to get a configured connection with better performance. The framework provides the jee namespace, which facilitates easy configuration. The jee namespace can be used to obtain the data source configured in the application, as shown in the following piece of code:
        <beans xmlns="http://www.springframework.org/schema/beans" 
          xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org
/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd"> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/myDataSource"/> </bean> <jee:jndi-lookup jndi-name="jdbc/myDataSource"
id="dataSource" resource-ref="true"/> </beans>

The attributes configured in the DataSource configuration are as follows:

    • jndi-name: This specifies the name of the resource configured on the server in JNDI
    • id: This is the ID of the bean that gives the object of DataSource
    • resource-ref: This specifies whether to prefix with java:comp/env or not
  • Fetched from the pooled connection: Spring doesn't have a pooled data source, but we can configure the pooled data source provided by Jakarta Commons Database Connection Pooling. The DBCP-provided BasicDataSource can be configured as follows:
        <bean id="dataSource" 
          class="org.apache.commons.dbcp.BasicDataSource"> 
            <property name="driverClassName" 
              value="org.hsqldb.jdbcDriver"/> 
            <property name="url" 
              value="jdbc:hsqldb:hsql://locahost/name_of_schama"/> 
            <property name="username" 
              value="credential_for_username"/> 
            <property name="password" 
              value="credential_for_password"/> 
            <property name="initialSize" 
              value=""/> 
            <property name="maxActive" 
              value=""/> 
        </bean> 

The attributes configured for the DataSource configuration are as follows:

    • initialSize: This specifies how many connections need to be created when the pool is started
    • maxActive: This specifies how many connections can be allocated from the pool.

Along with these attributes, we can even specify the time that needs to wait until the connection is returned from the pool (maxWait), the maximum/ minimum number of connections that can be idle in the pool (maxIdle/ minIdle), and the maximum number of prepared statements that can be allocated from the statement pool ( maxOpenPreparedStatements).

  • With the help of the JDBC driver: One can use the following class to configure the simplest way to get an object of DataSource:
    • SingleConnectionDataSource: As we already discussed, this returns a single connection
    • DriverManagerDataSource: This returns a new connection object on request

The DriverMangerDataSource configuration can be done as follows:

        <bean id="dataSource" 
class="org.springframework.jdbc.datasource
.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://locahost/name_of_schama"/> <property name="username" value="credential_for_username"/> <property name="password" value="credential_for_password"/> </bean>
SingleConnectionDataSource is suitable for small, single-threaded applications. DriverManagerDataSource supports multithreaded applications. The DriverManagerDataSource is not a pooled DataSource, and it needs to be managed carefully for handling multiple connections. It ultimately hampers the application performance. It's recommended to use a pooled data source for better performance.

Let's develop a sample demo using loosely coupled modules so that you understand the practical aspects of the Spring Framework application development.

The DataSource facilitates handling of connection with the database, so it needs to be injected in the module. The choice of using a setter DI or constructor DI can be totally decided by you, as you are well aware of both of these dependency injections. We will use the setter DI. Instead of starting from the class, we will start by considering the interface, as it's the best way to do programming by contract. Interfaces can have multiple implementations. So, the use of interface and Spring configuration helps you achieve loosely coupled modules. We will declare the methods for database operations; you are free to choose the signature. However, make sure that they are testable. As loose coupling is a major feature of the framework, the application will also demonstrate why we keep on saying that loosely coupled modules can be easily written using Spring. You can use any database, but we will use MySQL throughout this book. Whatever database you choose, make sure to install it before moving ahead. So, let's start by following these steps!

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

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