当前位置: 首页 > 知识库问答 >
问题:

SpringBoot@Transactional rollback不工作

许寒
2023-03-14

我是一个新的springboot和我正在考虑它为一个新的项目。在测试其功能时,我使用@Transactional注释总是失败。

我创建了一个小的MySql数据库,并将其连接到该数据库,设置此application.properties文件:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3311/mb
spring.datasource.username=mb
spring.datasource.password=password
spring.datasource.tomcat.default-auto-commit=false
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
@Transactional
public class UserRepository {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    protected JdbcTemplate jdbc;


    public User getUser(long id) {
        return jdbc.queryForObject("SELECT * FROM test_table WHERE id=?", userMapper, id);
    }

    public List<User> getUsers(long[] ids) {
        String inIds = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(ids));
        List<User> u= jdbc.query("SELECT * FROM test_table WHERE id IN (" + inIds + ")", userMapper);
        return u;
    }

    @Transactional
    public int insertUser(User u) {

        int total = jdbc.update("insert into test_table values ('"+u.id+"', '"+u.firstname+"', 'toto', '"+u.email+"', NOW())");
        //second insert that will cause an exception due to a duplicate key on PK (first column)
        total += jdbc.update("insert into test_table values ('"+u.id+"', '"+u.firstname+"', 'toto', '"+u.email+"', NOW())");
        return total;
    }

    private static final RowMapper<User> userMapper = new RowMapper<User>() {
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User(rs.getLong("id"), rs.getString("firstname"));
            user.email = rs.getString("email");
            System.out.println("recup du user "+user.id + user.firstname);
            return user;
        }
    };

} 

为什么?

共有1个答案

督弘化
2023-03-14

找到的解决方案:创建mysql表时设置的默认存储引擎(MyISAM)不能处理多个语句事务。使用InnoDB的引擎创建表使所有工作都非常完美。

 类似资料:
  • 我有一个Springboot应用程序,我的实体模型与作为依赖项包含的主应用程序分开。 我的Application.java位于此包中 我的实体模型位于这个包com.a.b中的另一个项目中 但是我得到一个错误:由:java.lang.IllegalArgumentException:不是托管类型:类引起

  • 我正在学习Spring Boot来构建应用程序。我试图用不同包中的控制器作为应用程序来构建我的第一个Spring Boot应用程序。Tomcat实例出现,但请求没有到达为URI注册的RestController。 以下是控制器类: 以下是应用程序类: Pom.xml tomcat启动时的日志: 我添加了基本包扫描,甚至尝试了注释,但当我点击URL(http://localhost:8080/abc

  • 我已经将spring boot嵌入式tomcat服务器中的保持活动超时设置为30秒。因此,我在应用程序中使用下面的内容。JAVA 然后我从我的Rest控制器Hibernate一个请求线程40秒。但是当我通过postman发出请求时,它成功返回HTTP状态代码200,而不是返回网关超时错误。 我尝试setConnectionTimeout和setKeepAliveTimeout,但它不起作用。 我错

  • 我在我的模型中使用LocalDateTime,在包括LocalDateTimeDeserializer之后,将bean字段转换为 包括 属性在SpringBoot的应用程序中。属性文件,应用程序最终能够反序列化JSON并正确显示如下内容:, 但是,当我进行测试时,它会忽略WRITE_DATES_AS_TIMESTAMPS标志,我猜它会为上面相同的字符串日期生成一个输出, 请注意,在测试资源文件夹中

  • controller.java UserServiceImpl.java 我得到了这个错误 应用程序启动失败 描述: 我使用的SpringBoot版本:2.1.0.发行版

  • 我无法在注释中使用方法。还有给出。我错过了什么?尽管工作正常。 我正在从数据库中为用户分配权限。