Data access and the Spring Framework

For Spring beans to access a data source, you must configure those beans so that the Spring Framework delegates to, and integrates with, the WebSphere® Application Server runtime correctly.

The Spring Framework wraps Spring beans with a container-management layer that, in an enterprise application environment, delegates to the underlying enterprise application runtime. The following sections describe what to consider when you configure Spring beans that access a data source.

Access to data sources configured in the application server

For a Spring application to access a resource such as a Java™ Database Connectivity (JDBC) data source, the application must use a resource provider that is managed by the application server.

To do this, see the Configuring access to a Spring application data source topic.

JDBC native connections

WebSphere Application Server does not support the use of the NativeJdbcExtractor class that the Spring Framework provides, so avoid scenarios that use this class. Implementations of this class access native JDBC connections and bypass quality of service functions in the application server such as tracking and reassociating connection handles, sharing connections, managing connection pools and involvement in transactions.

As an alternative, you can use the application server WSCallHelper class to access non-standard vendor extensions for data sources.

Java Persistence API

WebSphere Application Server includes a default JPA provider based on the Apache OpenJPA implementation of JPA. For more information, see the related links.

To use the Spring Framework with a JPA implementation, it is advisable to use JPA directly rather than using the JPA helper classes that are provided with the Spring Framework in the org.springframework.orm.jpa package.

To use managed JPA from the Spring Framework, you define a persistence context reference in the web descriptor (web.xml):
<persistence-context-ref>
  <persistence-context-ref-name>some/name</persistence-context-ref-name>
  <persistence-unit-name>pu_name</persistence-unit-name>
</persistence-context-ref>
where pu_name is the name of the persistence unit as defined in the persistence.xml file.
The persistence context is then available from JNDI through java:comp/env/some/name inside the web application. For the Spring Framework, the persistence context can then be retrieved using a <jee:jndi-lookup/> as shown in the following example code. The resulting EntityManager object is available under the entityManager ID.
<jee:jndi-lookup id="entityManager" jndi-name="some/name" />
Similarly, a persistence unit (for direct use, or use with Spring wrapper classes) can be made available through a persistence unit reference:
<persistence-unit-ref>
  <persistence-unit-ref-name>some/ref_name</persistence-unit-ref-name>
  <persistence-unit-name>pu_name</persistence-unit-name>
</persistence-unit-ref>
The resulting EntityManagerFactory object is available under the entityManagerFactory ID:
<jee:jndi-lookup id="entityManagerFactory" jndi-name="some/ref_name" />