Skip to content

Commit abf59e9

Browse files
committed
[docs update] BigDecimal 和 Spring 事务自调用完善
1 parent 895bf6e commit abf59e9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

docs/java/basis/bigdecimal.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public class BigDecimalUtil {
256256
}
257257
BigDecimal b1 = BigDecimal.valueOf(v1);
258258
BigDecimal b2 = BigDecimal.valueOf(v2);
259-
return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
259+
return b1.divide(b2, scale, RoundingMode.HALF_EVEN).doubleValue();
260260
}
261261

262262
/**
@@ -351,6 +351,12 @@ public class BigDecimalUtil {
351351
}
352352
```
353353

354+
相关 issue:[建议对保留规则设置为 RoundingMode.HALF_EVEN,即四舍六入五成双](https://github.com/Snailclimb/JavaGuide/issues/1122)
355+
356+
357+
358+
![RoundingMode.HALF_EVEN](https://oss.javaguide.cn/github/javaguide/java/basis/RoundingMode.HALF_EVEN.png)
359+
354360
## 总结
355361

356362
浮点数没有办法用二进制精确表示,因此存在精度丢失的风险。

docs/system-design/framework/spring/spring-transaction.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,25 @@ private void method1() {
679679

680680
解决办法就是避免同一类中自调用或者使用 AspectJ 取代 Spring AOP 代理。
681681

682+
[issue #2091](https://github.com/Snailclimb/JavaGuide/issues/2091)补充了一个例子:
683+
684+
```java
685+
@Service
686+
public class MyService {
687+
688+
private void method1() {
689+
((MyService)AopContext.currentProxy()).method2(); // 先获取该类的代理对象,然后通过代理对象调用method2。
690+
//......
691+
}
692+
@Transactional
693+
public void method2() {
694+
//......
695+
}
696+
}
697+
```
698+
699+
上面的代码确实可以在自调用的时候开启事务,但是这是因为使用了 `AopContext.currentProxy()` 方法来获取当前类的代理对象,然后通过代理对象调用 `method2()`。这样就相当于从外部调用了 `method2()`,所以事务注解才会生效。我们一般也不会在代码中这么写,所以可以忽略这个特殊的例子。
700+
682701
#### `@Transactional` 的使用注意事项总结
683702

684703
- `@Transactional` 注解只有作用到 public 方法上事务才生效,不推荐在接口上使用;

0 commit comments

Comments
 (0)