sách gpt4 ăn đã đi

spring - Spring上的嵌套事务

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 13:53:57 29 4
mua khóa gpt4 giày nike

我在使用嵌套 Spring 事务时发现了一些奇怪的行为:当在同一个类中,一个注释为 @Giao dịch 的方法调用另一个也注释为 @Giao dịch 的方法时没有使用第二个注释。

让我们考虑以下类:

lớp công khai Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
final Main main = context.getBean(Main.class);
// First Op
System.out.println("Single insert: " + main.singleInsert());
// Second Op
main.batchInsert();
// Third Op
main.noTransBatchInsert();
}

@PersistenceContext
private EntityManager pm;

@Transactional(lan truyền=Lan truyền.BẮT BUỘC)
public void batchInsert() {
System.out.println("batchInsert");
System.out.println("First insert: " + singleInsert());
System.out.println("Second insert: " + singleInsert());
}

public void noTransBatchInsert() {
System.out.println("noTransBatchInsert");
System.out.println("First insert: " + singleInsert());
System.out.println("Second insert: " + singleInsert());
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public int singleInsert() {
System.out.println("singleInsert");
Pojo p = new Pojo();
pm.persist(p);
return p.getId();
}
}

实体如果是以下类:

@Thực thể
public class Pojo {
@Nhận dạng
@GeneratedValue(strategy = GenerationType.AUTO)
số nguyên riêng tư id;

@Ghi đè
công khai String toString() {
return "Pojo: " + id;
}

công khai int getId() {
trả về id;
}
}

和字符串部分applicationContext.xml:


xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaVị trí="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">











和配置类(我可以在 applicationContext.xml 中合并它)。

@Cấu hình
@ImportResource("/META-INF/applicationContext.xml")
public class Config {

@Đậu
public Main main() {
return new Main();
}
}

为了完整性,persistence.xml 文件:


xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">


org.hibernate.ejb.HibernatePersistence





















所以在主类中,第一个操作在新事务中按预期执行。输出(包括一些 DEBUG 消息)是:

DEBUG o.h.transaction.JDBCTransaction - begin
singleInsert
DEBUG o.h.transaction.JDBCTransaction - commit
Single insert: 1

第二个操作给出以下输出:

batchInsert
singleInsert
DEBUG o.h.transaction.JDBCTransaction - begin
First insert: 2
singleInsert
Second insert: 3
GỠ LỖI

这不是我所期望的,因为在使用 @Transactional(propagation=Propagation.REQUIRES_NEW) 注释 singleInsert 时,我希望为每个调用创建一个新事务,这不是发生的事情,因为相同顶级事务用于两个插入。

第三次操作失败,也没有创建任何事务:

noTransBatchInsert
singleInsert
DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
First insert: 0
singleInsert
DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
Second insert: 0

hiện hữu @Cấu hình bean 中,Spring 确保对同一类上的方法的调用被代理,这显然不会在这里发生。有没有办法改变这种行为?

câu trả lời hay nhất

此行为是 Spring 在将 proxy 模式用于 AOP 时的记录行为。可以通过切换到 aspectj 模式来更改它,该模式在编译或运行时执行代码检测。

关于spring - Spring上的嵌套事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5232624/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress