java - spring transaction not rolled back
为情所困
为情所困 2017-05-17 10:07:12
0
2
527

1. Spring transactions will not be rolled back. I have tried all the online methods but to no avail.
2. Configuration is as follows:

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

    }
    ......
}

Directory Structure:

Please help me, I’ve been adjusting it for a day, I’m very grateful.

为情所困
为情所困

reply all(2)
伊谢尔伦

Looking at it, you manage transactions on the service interface, not the controller
The exception you throw is in the controller, of course the transaction will not be rolled back

You try to save it in the service implementation class and then throw an exception to see if the save is successful

ps: mysql has two storage engines (commonly used), one is InnoDB and the other is MyISAM. The former supports row-level locks, transactions, and foreign keys, while the latter does not support it

大家讲道理

You are right as you said above. Spring transactions act on the service layer. When the service method throws an exception, the transaction will be rolled back. So your correct testing practice should be to throw exceptions in service layer methods.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!