[Fix Pack 1 or later]


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 Version 7 and later, and WebSphere Application Server Version 6.1 Feature Pack for EJB 3.0 support the Enterprise JavaBeans (EJB) 3.0 specification, and therefore the Java Persistence API (JPA).

WebSphere Application Server Version 6.1 and later supports the use of 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.

WebSphere Application Server Version 6.1 and later supports JPA application-managed entity managers, which might have a transaction type of either JTA or resource-local. Entity managers that have a transaction type of JTA use the JTA transaction support of the application server. You can define transaction demarcation for such entity managers by using Java EE techniques or the declarative transaction model in the Spring Framework. For further information about transaction support with Spring Framework Version 2.5, see the related links.

A data access object that uses JPA is packaged with a persistence.xml file that defines persistence context for the JPA EntityManager object that the application uses.

The following example shows how to set up a persistence.xml file for a JTA entity manager that uses a data source with the JNDI name java:comp/env/jdbc/springdb:
<persistence 
  xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  <persistence-unit name="default" transaction-type="JTA">
    <provider> org.apache.openjpa.persistence.PersistenceProviderImpl 
    </provider>
    <jta-data-source> java:comp/env/jdbc/springdb </jta-data-source>
    <properties>
      <property name="openjpa.TransactionMode" value="managed" />
      <property name="openjpa.ConnectionFactoryMode"value="managed" />
      <property name="openjpa.jdbc.DBDictionary" value="db2" />
    </properties>
  </persistence-unit>
</persistence>

The openjpa.TransactionMode and openjpa.ConnectionFactoryMode properties are set to the value managed so that the JPA EntityManager object delegates management of transactions and connections to the application server. The data access object can use the declarative transaction model in the Spring Framework for transaction demarcation. For further information about transaction support with Spring Framework Version 2.5, see the related links.

if you want to use annotation-style injection of a JPA EntityManager object, you need Java Platform, Standard Edition 5 (Java SE 5) or later. Therefore, you can use this method with WebSphere Application Server Version 6.1 or later. The annotation is identical to standard JPA. For example:
@PersistenceContext
private EntityManager em;
Use the following lines of XML to enable EntityManager injection in the Spring XML configuration:
<!-- bean post-processor for JPA annotations -->
  <bean class=
  "org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
Spring will create an EntityManager object from any EntityManagerFactory instance that is defined in the Spring XML configuration file. If there is more than one EntityManagerFactory instance, creation will fail. To create an EntityManagerFactory instance, use only one of the following methods:
  • Create an EntityManagerFactory instance by using the basic configuration of the Spring Framework:
    <bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="default"/>
    </bean>
    
  • Create an EntityManagerFactory instance by using the advanced configuration of the Spring Framework:
    <bean id="myEmf" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="ds"/>
    </bean>
    
    <jee:jndi-lookup 
        id="ds" 
        jndi-name="jdbc/ds" 
        cache="true" 
        expected-type="javax.sql.DataSource"
    />

When the application server supports EJB 3.0, you can create an EntityManagerFactory object that uses JPA through the EJB 3.0 support. This approach has the advantage that you can separate the Spring and JPA configuration. WebSphere Application Server Version 6.1 Feature Pack for EJB 3.0 and WebSphere Application Server Version 7 and later support EJB 3.0.

Do not use this approach for versions of the application server that do not support EJB 3.0, because any EntityManager objects that are created might not be managed correctly.

The following example code shows how to create an EntityManagerFactory object that uses EJB 3.0:
<bean id="myEmf" 
  class="javax.persistence.Persistence" 
  factory-method="createEntityManagerFactory" >
  <constructor-arg type="java.lang.String" value="default"/>  
</bean>



Related concepts
Additional Application Programming Interfaces (APIs)
Spring Framework
Related tasks
Configuring access to a Spring application data source
Developing applications that use JNDI
Assembling data access applications
Developing JPA and packaging applications for a Java EE environment
Related reference
Data access portability features
Related information
Leveraging OpenJPA with WebSphere Application Server Version 6.1
Spring Documentation
Concept topic Concept topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Jun 11, 2013 8:40:09 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=v701sca&product=was-nd-mp&topic=cspr_data_access
File name: cspr_data_access.html