春季交易不会回滚

时间:2018-07-24 11:27:19

标签: spring aop transactionmanager

在发出两次插入操作时,如果第二次插入操作失败,则应回滚事务,但是在我的情况下这不会发生,以下是配置的详细信息!

<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/>

这些是代码详细信息;

@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;
}

}

不幸的是,该事务未回滚,我手动生成了异常以撤消该事务。 在这方面有帮助吗? TA。

0 个答案:

没有答案