As mentioned above... if your class that uses the DAOs is a Spring bean and you have a Spring managed TransactionManager then you can do this:
Code:
<aop:config>
<aop:pointcut id="classThatUsesDaosPointcut" expression="execution(* com.your.package.ClassThatUsesDaos.*(..))"/>
<aop:advisor advice-ref="classThatUsesDaosTxAdvice" pointcut-ref="classThatUsesDaosPointcut"/>
</aop:config>
<tx:advice id="classThatUsesDaosTxAdvice">
<tx:attributes>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="methodThatNeedsTransaction" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
Or via annotations:
Code:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
<tx:annotation-driven/>
......
</beans>
Code:
@Transactional( readOnly = true )
public ClassThatUsesDaos {
private YourDao yourDao;
@Transactional( propagation = Propagation.REQUIRED )
public void methodThatUsesDaos() {
.....
}
.....
}
You can configure a lot more stuff for the transaction (e.g. rollback for exceptions, do not rollback for some other exceptions, different propagation level, etc.)
Read the Transaction Management section of the Reference Manual (link posted above) and you'll find the answers to all your questions.
Good luck!
nicolas.loriente