I am using SOA and have multiple persistence.xml (each component dependent on DB). I am using Spring + JPA. Code sample as below:
In Core:
Code:
public abstract class GenericJpaDAOImpl<T extends BaseEntity> implements GenericJpaDAO<T> {
protected abstract EntityManager getEntityManager();
}
In Component SSO:
Code:
public class UserDAOImpl extends GenericJpaDAOImpl<User> implements UserDAO {
/* Any method specific to UserLogin */
@PersistenceContext(unitName = "sso", type = PersistenceContextType.TRANSACTION)
protected EntityManager entityManager;
@Override
protected EntityManager getEntityManager() {
return this.entityManager;
}
}
Persistence.xml
Code:
<persistence-unit name="sso" transaction-type="RESOURCE_LOCAL">
Application XML
Code:
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="ssoDataSource" />
<property name="persistenceUnitName" value="sso"></property>
</bean>
<bean id="ssoDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="<URL>"/>
<property name="user" value="<USER>"/>
<property name="password" value="<PASSWORD>"/>
</bean>
Similarlly Component: Billing (as Above)
I got similar thread o开发者_开发百科n http://forum.springsource.org/showth...sistence-units but this does work for expected behaviour. In my case If application context xml for Billing gets loaded first DAO operations works file for this component but SSO component does not work. Please suggest and let me know if more detail is required.
If you have multiple data sources, you can configure your DAOs to target a specific Session using the @Qualifier
annotation, as follows:
@Autowired
public MyDAOImpl(@Qualifier("someSessionFactory") SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
精彩评论