图解spring中七种事务传播行为 终于有人讲明白了
发布日期:2021-05-14 12:39:16 浏览次数:20 分类:精选文章

本文共 4689 字,大约阅读时间需要 15 分钟。

Spring������������������������

���������Spring���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Spring���������������������������������������������������������������

1. ���������REQUIRED���

��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� rollback���

���������������

// ������A
@Transactional(propagation = Propagation.REQUIRED)
public void insertA() {
// ������������
insertB();
// ������������
}
// ������B
@Transactional(propagation = Propagation.REQUIRED)
public void insertB() {
// ������������
}

��������������� ���A���������B������������@Transactional(propagation = Propagation.REQUIRED)������������A������������B���������������������������������������������������A���B���������������������������������������

2. ���������SUPPORTS���

���������������������������������������������������������������������������������������

���������������

// ������A
@Transactional(propagation = Propagation.REQUIRED)
public void insertA() {
// ������������
insertB();
// ������������
}
// ������B
@Transactional(propagation = Propagation.SUPPORTS)
public void insertB() {
// ������������
}

��������������� ������A������������������������������B���������������������������������������������A���������������������B������������������������������������

3. ���������������REQUIRES_NEW���

���������������������������������������������������������������������������������������������

���������������

// ������A
@Transactional(propagation = Propagation.REQUIRED)
public void insertA() {
// ������������
insertB();
// ������������
}
// ������B
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void insertB() {
// ������������
}

��������������� ���A������������B������������������A���������������B���������A������������������������������������������������������������������������������A���B���������������������������������������������������������������������������������������

4. ���������MANDATORY���

���������������������������������������������������������������������������������

���������������

// ������B
@Transactional(propagation = Propagation.MANDATORY)
public void insertB() {
// ������������
}

��������������� ���������B������������������������������������������������������������������B���������������IllegalTransactionOutOfBoundsException������������������������������������B������������������������������������

5. ������������NOT_SUPPORTED���

������������������������������������������������������

���������������

// ������A
@Transactional(propagation = Propagation.REQUIRED)
public void insertA() {
// ������������
insertB();
// ������������
}
// ������B
@Transactional(propagation = Propagation.NOT_supported)
public void insertB() {
// ������������
}

��������������� ���A���������������������B���������������A������������������������������������������������A���������������������B������������������������������������������������������������������������

6. ���������������NEVER���

������������������������������������������������������������������������������������������������

���������������

// ������B
@Transactional(propagation = Propagation.NEVER)
public void insertB() {
// ������������
}

��������������� ������������������������������B���������������������������������������������������������������������B������������������������

7. ���������������NESTED���

���������������������������������������������������������������������������������������������REQUIRED���������������

���������������

// ������A
@Transactional(propagation = Propagation.REQUIRED)
public void insertA() {
try {
insertB();
} catch (Exception e) {
e.printStackTrace();
}
// ������������
}
// ������B
@Transactional(propagation = Propagation.NESTED)
public void insertB() throws Exception {
// ������������
throw new Exception("������������");
}

��������������� ������A������������������������������������B������������������������������������������B���������������������������B���������������������A������������������������������

������

���������������������Spring������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:设计模式 -- 适配器模式
下一篇:synchronized 锁升级过程

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月19日 01时27分36秒