java – 抛出RuntimeException导致事务回滚,但Exception不在spring启动应用程序中
发布时间:2020-05-29 07:16:57 所属栏目:Java 来源:互联网
导读:在下面的代码中抛出异常不会回滚事务,但会抛出RuntimeException. @Servicepublic class HelloService { @Autowired protected CustomerRepository repository; @Transactional public void run() throws Exception {
|
在下面的代码中抛出异常不会回滚事务,但会抛出RuntimeException. @Service
public class HelloService {
@Autowired
protected CustomerRepository repository;
@Transactional
public void run() throws Exception {
repository.save(new Customer("Jack","Bauer"));
throw new RuntimeException("Kabooom!!!"); //Transaction is rolled back. Database is empty :)
//throw new Exception("Kabooom!!!"); //If this is used instead the records are inserted into the database. :(
}
}
我的存储库: public interface CustomerRepository extends CrudRepository<Customer,Long> {
}
Spring boot appliction.properties: # DataSource settings: set here configurations for the database connection spring.datasource.url = jdbc:mysql://localhost/hobbadb spring.datasource.username = root spring.datasource.password = spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate settings are prefixed with spring.jpa.hibernate.* spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.hibernate.hbm2ddl.auto= create-drop 任何想法为什么会这样? 解决方法从 docs:
您可以通过在@Transactional注释上指定rollbackFor或rollbackForClassName来覆盖此行为.有关完整选项,请参阅上述文档. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – BaseGameUtils无法将GoogleApiClient.ApiOptions解
- java – 应用程序类’com.sun.xml.messaging.saaj.soap.SOA
- 无法使用Javamail通过SSL或TLS使用SMTP发送邮件
- java – 有没有办法阻止Hibernate破坏@Where注释中的布尔文
- 如何从Java类访问会话
- Java中字符串中连续相同字符去重方法
- java – 为什么杰克逊2不认识第一个大写字母,如果领先的骆驼
- java – 黑莓线程模型
- java – RandomGenerator – 失去50%的飞机模拟
- 如何使用Java在Selenium WebDriver的隐藏字段中键入一些文本
