java - spring事务不回滚

查看:89
本文介绍了java - spring事务不回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

1.spring事务不会回滚,网上的方法都试过了,没有效果。
2.配置如下:

1.spring.xml:

<context:component-scan base-package="com.szcshl">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

2.springmvc.xml

<context:component-scan base-package="com.szcshl" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

3.spring-hibernate.xml

<!-- 配置事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 注解方式配置事物 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" />
            <tx:method name="save*" />
            <tx:method name="update*" />
            <tx:method name="modify*" />
            <tx:method name="edit*" />
            <tx:method name="delete*" />
            <tx:method name="remove*" />
            <tx:method name="repair" />
            <tx:method name="deleteAndRepair" />

            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="datagrid*" propagation="SUPPORTS" read-only="true" />

            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.szcshl.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>

4.controller

 @RequestMapping(value = "/Save" , method= RequestMethod.POST)
    public void departmentSave(Department department, HttpServletResponse response){
            departmentService.save(department);
            throw new RuntimeException("抛出异常");
    }

5.service

@Service
@Transactional
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    
    @Autowired
    private DepartmentDao deptDao;

    ......

    public void save(Department entity) {
        deptDao.save(entity);
    }
}

6.baseDao

public class BaseDaoImpl<T> implements BaseDao<T> {

    private SessionFactory sessionFactory;
    protected Class<T> entityClass;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @SuppressWarnings("rawtypes")
    protected Class getEntityClass() {
        if (entityClass == null) {
            entityClass = (Class<T>) ((ParameterizedType) getClass()
                    .getGenericSuperclass()).getActualTypeArguments()[0];
        }
        return entityClass;
    }

    @Transactional
    public Serializable save(T entity) {
        return this.getCurrentSession().save(entity);

    }
    ......
}

目录结构:

求大神帮忙看看,调了一天了,感激不尽。

解决方案

看了看,你对service接口进行事务管理,而不是controller
你抛出异常是在controller,当然不会事务回滚啦

你试试在sevice实现类中,save一下,再抛个异常,看看save成功不成功

ps一下:mysql有两个存储引擎(常用),一个是InnoDB一个是MyISAM,前者支持行级锁,事务,外键,后者不支持

这篇关于java - spring事务不回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆