【问题标题】:Spring boot and Spring Data JPA @Transactional Not WorkingSpring Boot 和 Spring Data JPA @Transactional 不工作
【发布时间】:2016-10-04 21:47:17
【问题描述】:

我创建了一个示例SpringBoot 项目并使用JPA Repository 将简单的问候语对象持久保存在数据库中(Greeting Id 和问候语文本)。我在服务类GreetingServiceImpl 中将@Transactional 注释添加到createGreeting() 方法,并在问候记录保存在数据库中后抛出RuntimeException。我除了 Greeting 记录在数据库中被回滚。但是该记录仍然存在于数据库中。下面给出的代码。有什么建议?提前致谢。

应用程序

@SpringBootApplication
@EnableTransactionManagement
public class SpringBootDataWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDataWebApplication.class, args);
    }
}

ServletInitializer

 public class ServletInitializer extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringBootDataWebApplication.class);
        }

    }

型号

@Entity
public class Greeting {

    @Id
    @GeneratedValue
    private Long id;

    private String text;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

休息控制器

    @RestController
public class GreetingController {

    @Autowired
    private GreetingService greetingService;

    @RequestMapping(value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Greeting> createGreeting(@RequestBody Greeting greeting) throws Exception {
        Greeting greetingCreated = greetingService.createGreeting(greeting);
        return new ResponseEntity<>(greetingCreated, HttpStatus.CREATED);
    }

}

服务类

@Service
public class GreetingServiceImpl implements GreetingService {

    @Autowired
    private GreetingRepository greetingRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Greeting createGreeting(Greeting greeting) {
        if (greeting.getId() != null)
            return null;
        Greeting greetingCreated = greetingRepository.save(greeting);
        if (true) {
            throw new RuntimeException("Roll me back!");
        }
        return greetingCreated;
    }

}

存储库

@Repository
public interface GreetingRepository extends JpaRepository<Greeting, Long> {

}

POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sam</groupId>
    <artifactId>spring</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>SpringBootDataWeb</name>
    <description>Demo project for Spring Boot and Spring Data</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jta-atomikos</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

错误信息

{
  "timestamp": 1465094495762,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.RuntimeException",
  "message": "Roll me back!",
  "path": "/SpringBootDataWeb/api/greetings"
}

【问题讨论】:

  • 我已经测试了您的设置,它的工作方式与您预期的一样 - 事务已回滚,没有任何内容保存到数据库中。也许你应该以 github 项目的形式创建一个minimal reproducible example
  • 您是否有机会将 MySQL 与 MyISAM engine 一起使用?我问是因为它不支持交易。
  • 是的 Roman,我正在使用 MySQL。
  • 但是您为Greeting 表使用哪个引擎:MyISAM 或 InnoDB 或其他?
  • 它的默认引擎。我猜它的 Innodb。

标签: spring jpa spring-boot spring-data


【解决方案1】:

需要注意的几点:

  • @Repository 注释不是必需的,因为您的存储库接口已经从 JpaRepository 扩展。
  • 您的服务实现缺少(或至少在上面的示例中没有)@Service 注释,因此控制器中的@Autowired 应该不起作用(我假设您在接口中有它?既然您没有'没有 bean 相关的异常)。
  • 方法级别的@Transactional是正确的,虽然默认设置为readOnly=false,所以不需要再次指定

我在想的是,您的 Greeting 实体可能没有正确映射,但仍然很难猜测显示的错误消息。您能否提供有关错误堆栈跟踪的更多信息?

【讨论】:

  • 感谢您的积分。请在下面的 github url 找到代码。我在代码中声明了@service Annotation,但是在复制粘贴时我错过了。 github.com/SandeepGoud0612/SpringBootDataWeb.git
  • 顺便说一下,我已经创建了Web应用程序项目(战争包装)。我正在使用 JBoss Wildfly 和 Mysql。
【解决方案2】:

理想的回滚是在单元/集成测试时完成的。在这些场景中,注释@TransactionConfiguration(defaultRollback = true) 用于编写测试用例的类。

你能在你的服务类上试试这个吗?

【讨论】:

  • @TransactionConfiguration(defaultRollback = true) 注释已弃用 SriKanta。
【解决方案3】:

在高层JpaTransactionManager 绑定到greetingRepository.save(greeting)

如果在save方法中发生任何异常会进行回滚,如果save期间没有异常,JpaTransactionManager会执行commitTransactionStatus会被设置为completed

因此,如果未绑定到JpaTransactionManager 的应用程序代码中发生任何异常,则提交后是应用程序异常。

【讨论】:

  • 但我想从服务类管理事务。我怎样才能做到这一点?
猜你喜欢
  • 2018-06-27
  • 2017-11-29
  • 2020-02-03
  • 2017-05-09
  • 2015-04-20
  • 2014-07-29
  • 2017-06-01
  • 2018-08-06
  • 2019-11-01
相关资源
最近更新 更多