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.
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, use the following steps.
<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>
<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>
<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.
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.
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.
<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.
@PersistenceContext private EntityManager em;
<!-- bean post-processor for JPA annotations --> <bean class= "org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="default"/> </bean>
<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.
<bean id="myEmf" class="javax.persistence.Persistence" factory-method="createEntityManagerFactory" > <constructor-arg type="java.lang.String" value="default"/> </bean>