-
Apr 30th, 2010, 02:21 PM
#1
Transaction Rollback not working across Multiple DAOs
Hi all,
I am using declarative transaction.
All of my DAOs extend HibernateDAO Support, but when exception occurs in the middle of a transaction the rest of the inserts are not rollback.
This are the declarations in application-context.xml:
<bean id="jcdcDataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDriver" />
<property name="url" value="jdbc:sybase:Tds:**" />
<property name="username" value="**" />
<property name="password" value="**#" />
</bean>
<!-- Transaction Manager -->
<bean id="jcdcSessionFactory"
class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource" ref="jcdcDataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.autocommit">false</prop>-->
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory" ref="jcdcSessionFactory" />
</bean>
<!-- Transaction Proxy -->
<!-- base transaction proxy for which spring beans inherit-->
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="txManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<!--<prop key="get*">PROPAGATION_REQUIRED</prop>-->
<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
</props>
</property>
</bean>
I have added DAO and businnes objects in one more xml
<bean id="sessionFactoryAware" abstract="true">
<property name="sessionFactory"><ref bean="jcdcSessionFactory"/></property>
</bean>
<bean id="enrollmentDetailBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.EnrollmentDetailBusine ss">
<property name="enrollmentDetailDao" ref="EnrollmentDetailDAO" />
</bean>
</property>
</bean>
<bean id="EnrollmentDetailDAO" class="org.jobcorps.saas.db.orm.dao.EnrollmentDeta ilDAO"
parent="sessionFactoryAware">
</bean>
Please let me know if I am missing any other declaration.
Any help is greatly appreciated.
Thanks!
Last edited by narmashan; May 3rd, 2010 at 02:03 PM.
-
Apr 30th, 2010, 02:32 PM
#2
Post the call to the DAO's please.
-
Apr 30th, 2010, 02:36 PM
#3
public class EnrollmentDAO extends HibernateDaoSupport
{
public void save(Enrollment transientInstance) {
log.debug("saving Enrollment instance");
try {
getHibernateTemplate().saveOrUpdate(transientInsta nce);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
}
Please let me know if this is the information you have asked.
-
Apr 30th, 2010, 02:38 PM
#4
No, I mean where is the service call to multiple daos, something like:
public void saveInBusiness(...){
saveDao1();
saveDao2();
}
etc
-
Apr 30th, 2010, 02:50 PM
#5
I am using multiple daos , but inserting in to 3 tables in a single transaction.
Each Dao is called by each business.
I have 3 tables StudentEarning,StudentDeduction,Reimbursement for which I perform inserts.
I have 3 daos. StudentEarningdao,StudentDeductiondao,ReimbursemtD ao
Each dao has sperated business object StudentEarningBusniess,StudenDedBusniess,Reimburse mentBusiness.
The code in the Business method is:
public class StudentDeductionBusiness implements IStudentDeductionBusiness {
private StudentDeductionDAO studentDeductionDao;
public Set getStudentDeductionsByEnrollmentDetail(Long enrDetailId) {
return studentDeductionDao.getStudentDeductionsByEnrollme ntDetail(enrDetailId);
}
public void setStudentDeductionDao(StudentDeductionDAO studentDeductionDao) {
this.studentDeductionDao = studentDeductionDao;
}
public void delete(StudentDeduction studentDeduction) throws Exception{
studentDeductionDao.delete(studentDeduction);
}
public void save(StudentDeduction studentDeduction) throws Exception{
try{
studentDeductionDao.save(studentDeduction);
}
catch(Exception e)
{
System.out.println("Exception while saving student deduction " + e);
}
}
}
Similarly for other 2 business
and finally the 3 business is called in my final service file:
studentDeductionBusiness.save(studDed);
studentEarningBusiness.save(studEarn);
ReimbursemnetBusiness.save(reimb);
I am manually throwing an exception before studentEarningBusniess,
but exverything is getting commited.
-
Apr 30th, 2010, 03:09 PM
#6
Please let me know if you need any more information.
-
May 3rd, 2010, 09:23 AM
#7
Should I use @transaction to make the daos transactional, or is there any other to achieve transaction across Multiple DAOS?
Appreciate all your help. Thanks.
-
May 3rd, 2010, 01:44 PM
#8
A couple things stand out:
1) Where are the bean declarations for the other DAO's I only see them for one.
2) Your business has a parent of the transactionamanager and the dao has a parent of the session factory why did you do that?
-
May 3rd, 2010, 01:59 PM
#9
Hi,
The following is 2nd application-context.xml
<beans>
<bean id="sessionFactoryAware" abstract="true">
<property name="sessionFactory">
<ref bean="jcdcSessionFactory" />
</property>
</bean>
<bean id="AdvancePaySaasBlImpl" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.AdvancePaySaasBlImpl">
<property name="sessionFactory">
<ref bean="jcdcSessionFactory" />
</property>
<property name="enrollmentBusiness" ref="enrollmentBusiness" />
<property name="payControlBusiness" ref="payControlBusiness" />
<property name="deductionBusiness" ref="deductionBusiness" />
<property name="earningBusiness" ref="earningBusiness" />
<property name="enrollmentDetailBusiness" ref="enrollmentDetailBusiness" />
<property name="earnHistBusiness" ref="earnHistBusiness" />
<property name="reimbursementBusiness" ref="reimbursementBusiness" />
<property name="studentDeductionBusiness" ref="studentDeductionBusiness" />
<property name="studentEarningBusiness" ref="studentEarningBusiness" />
<property name="studentPayBusiness" ref="studentPayBusiness" />
</bean>
</property>
</bean>
<!--
######## Business Objects ##########
-->
<bean id="payControlBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.PayControlBusiness">
<property name="payControlDao" ref="PayControlDAO" />
</bean>
</property>
</bean>
<bean id="enrollmentBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.EnrollmentBusiness">
<property name="enrollmentDao" ref="EnrollmentDAO" />
</bean>
</property>
</bean>
<bean id="enrollmentDetailBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.EnrollmentDetailBusine ss">
<property name="enrollmentDetailDao" ref="EnrollmentDetailDAO" />
</bean>
</property>
</bean>
<bean id="earnHistBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.EarnHistBusiness">
<property name="earnHistDao" ref="EarnHistDAO" />
</bean>
</property>
</bean>
<bean id="deductionBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.DeductionBusiness">
<property name="deductionDao" ref="DeductionDAO" />
</bean>
</property>
</bean>
<bean id="earningBusiness" parent="baseTransactionProxy" autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.EarningBusiness">
<property name="earningDao" ref="EarningDAO" />
</bean>
</property>
</bean>
<bean id="studentEarningBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.StudentEarningBusiness ">
<property name="studentEarningDAO" ref="StudentEarningDAO" />
</bean>
</property>
</bean>
<bean id="studentDeductionBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.StudentDeductionBusine ss">
<property name="studentDeductionDao" ref="StudentDeductionDAO" />
</bean>
</property>
</bean>
<bean id="paydueBusiness" parent="baseTransactionProxy" autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.PaydueBusiness">
<property name="paydueDao" ref="PaydueDAO" />
</bean>
</property>
</bean>
<bean id="reimbursementBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.ReimbursementBusiness" >
<property name="reimbursementDao" ref="ReimbursementDAO" />
</bean>
</property>
</bean>
<bean id="studentPayBusiness" parent="baseTransactionProxy"
autowire="byName">
<property name="target">
<bean class="org.jobcorps.saas.bl.StudentPayBusiness">
<property name="studentPayDao" ref="StudentPayDAO" />
</bean>
</property>
</bean>
<!-- =================== -->
<!-- DAO implementations -->
<bean id="sessionFactoryAware" abstract="true">
<property name="sessionFactory">
<ref bean="jcdcSessionFactory" />
</property>
</bean>
<bean id="StudentDeductionDAO" class="org.jobcorps.saas.db.orm.dao.StudentDeducti onDAO"
parent="sessionFactoryAware">
</bean>
<bean id="EarnHistDAO" class="org.jobcorps.saas.db.orm.dao.EarnHistDAO"
parent="sessionFactoryAware">
</bean>
<bean id="StudentEarningDAO" class="org.jobcorps.saas.db.orm.dao.StudentEarning DAO"
parent="sessionFactoryAware">
</bean>
<bean id="EnrollmentDetailDAO" class="org.jobcorps.saas.db.orm.dao.EnrollmentDeta ilDAO"
parent="sessionFactoryAware">
</bean>
<bean id="PayControlDAO" class="org.jobcorps.saas.db.orm.dao.PayControlDAO"
parent="sessionFactoryAware">
</bean>
<bean id="DeductionDAO" class="org.jobcorps.saas.db.orm.dao.DeductionDAO"
parent="sessionFactoryAware">
</bean>
<bean id="EarningDAO" class="org.jobcorps.saas.db.orm.dao.EarningDAO"
parent="sessionFactoryAware">
</bean>
<bean id="StudentPayDAO" class="org.jobcorps.saas.db.orm.dao.StudentPayDAO"
parent="sessionFactoryAware">
</bean>
<bean id="EnrollmentDAO" class="org.jobcorps.saas.db.orm.dao.EnrollmentDAO"
parent="sessionFactoryAware">
</bean>
<bean id="PaydueDAO" class="org.jobcorps.saas.db.orm.dao.PaydueDAO"
parent="sessionFactoryAware">
</bean>
<bean id="ReimbursementDAO" class="org.jobcorps.saas.db.orm.dao.ReimbursementD AO"
parent="sessionFactoryAware">
</bean>
</beans>
The above are the complete dao,business decalartion.
"Your business has a parent of the transactionamanager and the dao has a parent of the session factory why did you do that?"
Could you tell how else that can be implemented?
Thank You!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules