Configuring access to a Spring application data source

You can use WebSphere® Application Server to manage access to a data source for a Spring application.

About this task

For a Spring application to access a data source, such as a Java Database Connectivity (JDBC) data source, the application must use a resource provider that is managed by the WebSphere Application Server. For more information about Spring applications and the Spring Framework see the following topics:

Procedure

  1. During development, configure the WAR module with a resource reference.
    For example:
    <resource-ref>
        <res-ref-name>jdbc/springdb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
  2. For Enterprise JavaBeans (EJB) Java™ archive (JAR) files, declare the same resource reference in each EJB that needs to access the data source.
    Use one of the following steps:
    • Declare a data source proxy bean. In the Spring application configuration, declare a proxy bean that references a resource provider that the application server manages. Set the value of the jndiName property to java:comp/env/ followed by the value of res-ref-name property that you declared in the resource reference. For example:
      <bean id="wasDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName" value="java:comp/env/jdbc/springdb"/>
          <property name="lookupOnStartup" value="false"/>
          <property name="cache" value="true"/>
          <property name="proxyInterface" value="javax.sql.DataSource"/>
      </bean>
      
    • Alternatively, for Spring Framework Version 2.5 or later, use the <j2ee:jndi-lookup/> approach. Set the value of the jndi-name property to the value of the res-ref-name property that you declared in the resource reference, and the value of the resource-ref property to true. For example:
      <jee:jndi-lookup id=" wasDataSource "
          jndi-name="jdbc/springdb"
          cache="true"
          resource-ref="true"
          lookup-on-startup="false"
          proxy-interface="javax.sql.DataSource"/>
    The Spring application can then use the data source proxy bean as appropriate.
  3. When the application is deployed to an application server, configure a resource provider and resource data source that the Spring application resource reference can use.

Results

The resource reference that is declared in the deployment descriptor of the module will be bound to the configured data source of the application server during deployment.