【发布时间】: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