Data Source definition
<bean id="oracleDataSource" > <property name="jndiName" value="jdbc/oracleDSName"/> </bean> <bean id="mySqlDataSource" > <property name="jndiName" value="jdbc/mySqlDSName"/> </bean> Session Factory Configuration <bean id="oracleSessionFactory" > <property name="dataSource" ref="oracleDataSource" /> <property name="packagesToScan"> <list> <value>com.feature.entities.db</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean> <bean id="mySQLSessionFactory" > <property name="dataSource" ref="mySqlDataSource" /> <property name="annotatedClasses"> <list> <value>com.feature.entities.db.ClassA</value> <value>com.feature.entities.db.ClassB</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean> Configure the DAOs. <bean id="oracleBaseDAO" > <property name="sessionFactory" ref="oracleSessionFactory"/> </bean> <bean id="mySqlBaseDAO" > <property name="sessionFactory" ref="mySQLSessionFactory"/> </bean>
1 Comment
Step 1: declarations in applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- SessionFactory is assumed to be defined --> <bean id="documentsDAO" class="com.feature.dao.DocumentsDAOImpl" lazy-init="true"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="documentsService" class="com.feature.service.DocumentsServiceImpl" lazy-init="true"> <property name="dao" ref="documentsDAO"/> </bean> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> <aop:config> <aop:pointcut id="documentsServiceMethods" expression="execution(* com.feature.service.DocumentsService.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="documentsServiceMethods"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans> Step 2: Annotate the Class package com.feature.service import org.springframework.transaction.annotation.Transactional; import com.feature.dao.DocumentsDAO; import com.feature.entity.Document; import com.feature.entity.Category ; public class DocumentsServiceImpl implements DocumentsService { @Transactional private List<Document> getDocuments(Category category){ DocumentsDAO dao = getDocumentsDAO(); return dao.getDocuments(category); } } NOTE: The @Transactional annotation can be applied to methods in any class - not necessarily a DAO |