+

Search Tips   |   Advanced Search

Data access and the Spring Framework

For Spring beans to access a data source, 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 we 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 JDBC data source, the application must use a resource provider managed by the application server.

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


JDBC native connections

WAS 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, we can use the application server WSCallHelper class to access non-standard vendor extensions for data sources.


Java Persistence API

WAS includes two JPA providers; one based on EclipseLinks for JPA 2.1 and one based on the Apache OpenJPA implementation of JPA 2.0. See 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 provided with the Spring Framework in the org.springframework.orm.jpa package.

To use managed JPA from the Spring Framework, we 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" />


Related:

  • Additional Application Programming Interfaces (APIs)
  • Configure access to a Spring application data source
  • Developing applications that use JNDI
  • Assemble data access (EJB) applications
  • Developing JPA 2.x applications for a Java EE environment
  • Data access portability features
  • Leveraging OpenJPA with WAS V6.1
  • Spring Documentation