Spring Boot + Spring Data JPA +交易无法正常工作

K. *_*ddy 10 spring jpa spring-data-jpa spring-boot

我使用带有spring-boot-starter-data-jpa的1.2.0版本创建了一个Spring Boot应用程序,我正在使用MySQL.我正确地在application.properties文件中配置了MySQL属性.

我有一个简单的JPA实体,Spring Data JPA Repository和Service如下:

@Entity
class Person
{
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;
    //setters & getters
}

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer>{

}


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
class PersonService
{
    @Autowired PersonRepository personRepository;

    @Transactional
    void save(List<Person> persons){
        for (Person person : persons) {         
            if("xxx".equals(person.getName())){
                throw new RuntimeException("boooom!!!");
            }
            personRepository.save(person);          
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下JUnit测试:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {

    @Autowired
    PersonService personService;

    @Test
    public void test_logging() {
        List<Person> persons = new ArrayList<Person>();
        persons.add(new Person(null,"abcd"));
        persons.add(new Person(null,"xyz"));
        persons.add(new Person(null,"xxx"));
        persons.add(new Person(null,"pqr"));

        personService.save(persons);
    }

}
Run Code Online (Sandbox Code Playgroud)

这里的期望是它不应该在PERSON表中插入任何记录,因为它会在插入第三个人对象时抛出异常.但它没有回滚,前2个记录被插入并提交.

然后我想到快速尝试使用JPA EntityManager.

@PersistenceContext
private EntityManager em;

em.save(person);
Run Code Online (Sandbox Code Playgroud)

然后我得到javax.persistence.TransactionRequiredException:没有事务性EntityManager可用异常.

谷歌搜索了一段时间后,我在同一主题上遇到了这个JIRA线程https://jira.spring.io/browse/SPR-11923.

然后我将Spring Boot版本更新为1.1.2以使Spring版本高于4.0.6.

然后em.save(person)按预期工作,并且Transaction工作正常(意味着它在RuntimeException发生时回滚所有db插入).

但即使使用Spring 4.0.5 + Spring Data JPA 1.6.0版本,当使用personRepository.save(person)而不是em.persist(person)时,事务也不起作用.

似乎Spring Data JPA存储库正在提交事务.

我错过了什么?如何使服务级别@Transactional注释有效?

PS:

Maven pom.xml

<?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.sivalabs</groupId>
    <artifactId>springboot-data-jpa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-data-jpa</name>
    <description>Spring Boot Hello World</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
        <relativePath />
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.sivalabs.springboot.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

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

    <dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

Application.java

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

eis*_*eis 2

来自@m-deinum 的评论:

创建您的 PersonServicepublic以及您正在调用的方法。

这似乎已经为一些用户带来了好处。这个答案也涵盖了同样的事情,引用手册说

使用代理时,您应该仅将 @Transactional 注释应用于具有公共可见性的方法。如果您使用 @Transactional 注释来注释受保护的、私有的或包可见的方法,则不会引发错误,但带注释的方法不会显示配置的事务设置。如果您需要注释非公共方法,请考虑使用 AspectJ(见下文)。