@Transactional没起作用ssm框架

@Transactional使用这个注解是声明式事务,需要配置tx命名空间:

xmlns:tx="http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

   <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- enable transaction annotation support -->
    <tx:annotation-driven transaction-manager="transactionManager" />


然后写一个测试试一下就好了…
今天我在测试的时候发现事务没起作用,因为一般数据库都是InnoDB然后也没去看,然后查了一个多小时,说看看数据库,结果,竟然是myisam的,我服了.粗心了.然后改为innodb就没问题了.写个博客记录一下,
在这里插入图片描述
在这里插入图片描述
检查项:

  • @Transactional没起作用,首先一定要看看数据库引擎,myisam是不支持事务的.
  • 然后看看当前类是否被扫描到,一般使用@Service注解,都扫到了,
  • 再然后看看抛出的异常,一般RuntimeException都可以回滚,
  • 然后看加注解的方法,是不是public,spring事务也是基于aop代理对方法增强的,如果是service类里面的方法调内部方法也不会生效.主要就是这些,如果还是不行可以留言,大家一起看看
posted @ 2019-05-16 10:01  你就像甜甜的益达  阅读(79)  评论(0编辑  收藏  举报