我有一个场景,我需要在循环中调用事务方法来保持同一类中非事务方法的单个记录。
这可以使用Spring声明式事务管理来实现吗?
我在xml文件中配置了txManager,如下所示:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
并提出建议,涵盖所有服务方法。 但是在一个服务中,我需要从其他方法调用一个方法。
我的班级看起来像这样。
public class Test{
saveAllRecords(){
// some required processing
for(i=0;i<10;i++)
{
// some required processing before calling save one record
saveOneRecord();}
}
@Transactional
saveOneRecord()
{
}
}
My Questions are
1)how to make only method transnational..?
2) How do i make both different transaction..? like as soon as method2() execution finished i should see one record in database..
答案 0 :(得分:0)
您无需定义建议以使所有方法都遵循事务性注释。您需要在上下文配置中指定此项,并且使用@Transactional
注释的任何方法都是事务性的。
<tx:annotation-driven/>
您可以将交易的传播添加为REQUIRES_NEW。每次执行此方法时,它都会导致打开一个新事务。
@Transactional(propagation=Propagation.REQUIRES_NEW)
但是,请注意,在for循环中拥有多个事务是非常不鼓励的,因为它可以锤击数据库并降低高数据事务的性能。