Spring Boot 和 Spring Data JPA @Transactional 不起作用

3
我创建了一个示例SpringBoot项目,并使用JPA Repository将简单的问候对象(Greeting Id和Greeting Text)保存到数据库中。我在服务类GreetingServiceImpl的createGreeting()方法中添加了@Transactional注释,并在Greeting记录被持久化到数据库后抛出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;
    }

}

Rest Controller

    @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"
}

1
你是否偶尔使用带有MyISAM引擎的MySQL?我问这个问题是因为它不支持事务。 - Roman
是的Roman,我正在使用MySQL。 - Sandeep Goud Karrey
但是你在Greeting表中使用哪个引擎:MyISAM还是InnoDB还是其他引擎? - Roman
它的默认引擎。我猜是Innodb。 - Sandeep Goud Karrey
当我将SpringBoot应用程序打包为war并使用Jboss作为服务器时,事务无法回滚。在Jboss AS中是否有任何设置需要调整? - Sandeep Goud Karrey
显示剩余3条评论
3个回答

0

理想情况下,回滚应该在单元/集成测试期间完成。在这些场景中,注释@TransactionConfiguration(defaultRollback = true)被用于编写测试用例的类上。

你可以在你的服务类上尝试一下这个方法。


@TransactionConfiguration(defaultRollback = true) 注释已被弃用,SriKanta。 - Sandeep Goud Karrey

0

需要注意以下几点:

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

我认为您的问候实体可能没有正确映射,但是由于显示的错误消息很难猜测。您能否提供更多关于错误堆栈跟踪的信息?


感谢您的积分。请在下面的Github网址中找到代码。我在代码中声明了@service注释,但是在复制粘贴时错过了它。 https://github.com/SandeepGoud0612/SpringBootDataWeb.git - Sandeep Goud Karrey
顺便说一下,我已经创建了Web应用程序项目(War打包)。我正在使用JBoss Wildfly和Mysql。 - Sandeep Goud Karrey

-1
在高级别中,JpaTransactionManager 被绑定到 greetingRepository.save(greeting)
如果在保存方法内发生任何异常,JpaTransactionManager 将执行回滚操作。如果在 save 过程中没有出现异常,JpaTransactionManager 将执行提交操作,并将 TransactionStatus 设置为 completed
因此,在提交后,如果应用程序代码中发生任何与 JpaTransactionManager 无关的异常,则为应用程序异常。

但我想从Service类中管理事务。我该如何实现? - Sandeep Goud Karrey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接