事务未在spring中回滚
在发出两个插入时,如果第二个插入失败,事务应该回滚,但在我的情况下不会发生这种情况,下面是配置的详细信息事务未在spring中回滚,spring,aop,transactionmanager,Spring,Aop,Transactionmanager,在发出两个插入时,如果第二个插入失败,事务应该回滚,但在我的情况下不会发生这种情况,下面是配置的详细信息 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedCla
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.xyz.model.Merchant</value>
....
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allServices"
expression="execution(*
com.xyz.services.DemoApiServiceImpl.saveEmployee(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
</aop:config>
<aop:aspectj-autoproxy/>
}
不幸的是,没有回滚该事务,我已手动生成异常以反转该事务。
这方面有什么帮助吗?
TA.在取消注释注释注释的代码(
rollboor=Exception.class
)时是否有效?不,不起作用,这就是为什么注释掉它。MySQL作为数据库,MyISAM作为存储引擎。还有,您根本没有使用的@Transactional
有什么问题(您的配置中没有
。没错,Deinum先生!有什么问题吗?我需要添加这个吗?
@Service
@Transactional
public class DemoApiServiceImpl implements DemoApiService {
@Override
//@Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
public boolean saveEmployee(Employee employee) throws Exception {
Boolean flag = Boolean.FALSE;
flag = demoApiDao.saveOrUpdateEmployee(employee);
if(flag)
//generating exception so that above insert should rollback!
throw new Exception("ok");
if(!CollectionUtils.isEmpty(employee.getUsers())) {
Users user = (Users) employee.getUsers().toArray()[0];
//sendEmail(user,EmailType.USER_SIGN_UP.name(),Constants.MAIL_SUBJECT_ACTIVATION_LINK);
UsersDto usersDto = new UsersDto();
usersDto.setActivationCode(user.getActivationCode());
String emailText = Utils.createEmailText(usersDto, EmailType.USER_SIGN_UP.name());
Utils.sendEmail(user.getUsername(), Constants.FROM_EMAIL, Constants.MAIL_SUBJECT_ACTIVATION_LINK, emailText);
}
return flag;
}