设置:
Tabe:CREATE TABLE test (id int PRIMARY KEY NOT NULL, name varchar(25));
基本测试流程:
Preconditions:数据库已经有了id=2的记录(强制SqlException)
案例1:
当按原样调用基本流时,第二个insert由于主键冲突而失败,第一个insert将被回滚。似乎是一种合理的违约行为。
案例2:
通过添加回滚异常策略修改基本流,并在异常策略中添加一个记录器,以便在调用时打印某些内容。
当调用flow时,第二个insert由于主键冲突而失败,第一个insert将被回滚,但是 Rollback
关于这个例外,我只知道来自Spring的和一些论坛帖子,上面有结霜的开发人员贴着巨大的堆栈跟踪,没有回复。
来自Spring的文档:
当试图提交事务导致意外回滚时引发
我想一劳永逸
到底是什么原因造成的?
- Where did the rollback occur? in the App Server code or in the Database?
- Was it caused due to a specific underlying exception (e.g. something from java.sql.\*)?
- Is it related to Hib
我在写入器上使用了一个spring batch重试。作者看起来像这样:
public class MyWriter implements ItemWriter<MyClass> {
@Retryable(maxAttempts=3, backoff=@Backoff(delay=2000))
public void write(List<? extends MyClass> list) throws Exception {
// db operation 1 -- insert query
// some business logic
当一个方法被@Transactional注释并且有一个运行时异常时,spring会吃掉这个异常并抛出:
org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
如何避免此“一般”异常并传播原始异常,但保留回滚?
谢谢。
我的应用程序在Spring 4.2和postgres数据库上。在我的应用程序中,我们使用了一个API,它是使用spring编写的,它有自己的数据库(mysql)。
@Transaction(rollbackfor = Exception.class)
updateOrder(Order order) {
// This insert is part of my application
update(order); //**STEP - 1**
//This is not part of our application &
// happening in
我有一个下面的场景,我想要了解Spring中的嵌套事务处理以及传播。实际上,我读了很多关于这方面的内容,但对一些事实仍然不清楚。
public class ServiceImpl {
@Autowired
public AnotherService anotherService;
@Transactional // by default it is PROPOGATION_REQUIRED
public void insert (){
anotherService.anotherInsert();
}
}
public class AnotherServiceImpl {
@T
我有一个dao类(MyDao),它用@Transactional注释(在类级别上)标记,没有额外的参数。在这个dao类中,我有一个方法,在某些情况下需要抛出一个检查过的异常并执行事务回滚。如下所示:
@Transactional
public class MyDao {
public void daoMethod() throws MyCheckedException() {
if (somethingWrong) {
TransactionAspectSupport.currentTransactionStatus().setRollbac