Spring @Transactional失效与否
package xyz.pascall.mybatis.spring.boot.learn.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import xyz.pascall.mybatis.spring.boot.learn.entity.User; import xyz.pascall.mybatis.spring.boot.learn.mapper.UserMapper; import xyz.pascall.mybatis.spring.boot.learn.service.UserService; /** * UserServiceImpl * * Spring事务失效原因: * 原因一:是否是数据库引擎设置不对(mysql:MyISAM,InnoDB) * 原因二:入口方法必须是public(Spring AOP特性决定)(不生效:private, final, static) * 原因三:Spring的事务管理默认只对运行期异常进行回滚 * 原因四:@EnableTransactionManagement // 启注解事务管理 === <tx:annotation-driven /> * @EnableTransactionManagement 在springboot1.4后可不写,只支持Mybatis和Hibernate * 1.框架在初始化时默认注入两个事务管理器的Bean(JDBC->DataSourceTransactionManager(Mybatis)
* JPA->JpaTransactionManager(Hibernate) ) * 2.当然如果不是AutoConfig的而是自己自定义的,使用该注解开启事务 * 原因五:请确认你的类是否被AOP代理 * 原因六:请确保你的业务和事务入口在同一个线程里 * 原因七:service方法中调用本类中的另一个方法,事务没有生效 * 1.事务无效:无事务方法调用有事务方法(有异常) * @Override * public void addInfo(final String name) { * create(name); * } * @Transactional * public int create(String name) { * User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); * Integer result = userMapper.add(user); * int i = 1/0; * return i; * } * 2.事务有效:有事务方法调用有事务方法(有异常) * @Transactional * @Override * public void addInfo(final String name) { * create(name); * } * @Transactional * public int create(String name) { * User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); * Integer result = userMapper.add(user); * int i = 1/0; * return i; * } * 3.事务有效:有事务方法(有异常)调用有事务方法 * @Transactional * @Override * public void addInfo(final String name) { * create(name); * int i = 1 / 0; * } * @Transactional * public int create(String name) { * User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); * return userMapper.add(user); * } * 4.事务有效:有事务方法(有异常)调用无事务方法 * @Transactional * @Override * public void addInfo(final String name) { * create(name); * int i = 1 / 0; * } * public int create(String name) { * User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); * return userMapper.add(user); * } * 5.事务有效:有事务方法调用无事务方法(有异常) * @Transactional * @Override * public void addInfo(final String name) { * create(name); * } * public int create(String name) { * User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); * Integer result = userMapper.add(user); * int i = 1/0; * return i; * } * 6.事务有效:无事务方法(注入本对象)调用有事务方法(有异常) * @Override * public void addInfo(final String name) { * userService.create(name); * } * @Transactional * public int create(String name) { * User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); * Integer result = userMapper.add(user); * int i = 1/0; * return i; * } * * @author Jie Zhang, 2019/7/31 * @version DEMO v1.0 */ @Service @Slf4j public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Autowired private UserService userService; @Override public User getUser(Integer id) { return userMapper.getUser(id); } @Override public PageInfo<User> query(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? 5 : pageSize, true); return new PageInfo<>(userMapper.query()); } @Override public void addInfo(final String name) { userService.create(name); } @Transactional public int create(String name) { User user = new User().setName(name).setAge(30).setEmail(name + "@qq.com"); log.info(String.valueOf(userMapper.add(user))); int i = 1/0; return i; } }