我创建了一个示例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"
}
Greeting
表中使用哪个引擎:MyISAM还是InnoDB还是其他引擎? - Roman