java - spring事務不回滾
为情所困
为情所困 2017-05-17 10:07:12
0
2
526

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);

    }
    ......
}

目錄結構:

#求大神幫忙看看,調了一天了,感激不盡。 ######
为情所困
为情所困

全部回覆(2)
伊谢尔伦

看了看,你對service介面進行事務管理,而不是controller
你拋出異常是在controller,當然不會事務回滾啦

你試試在sevice實作類別中,save一下,再拋個異常,看看save成功不成功

ps一下:mysql有兩個儲存引擎(常用),一個是InnoDB一個是MyISAM,前者支援行級鎖,事務,外鍵,後者不支援

大家讲道理

樓上說的沒錯,spring事務作用於service層,當遇到service方法拋出異常時,事務將回滾。所以你的正確測試實務應該是在service層方法中拋出異常。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!