当前位置:网站首页>This call when the transaction does not take effect
This call when the transaction does not take effect
2022-04-23 04:59:00 【GreatorMan】
In business development , We call a service method of the same class in a class , Transaction failure found , The code is as follows :
For example, an order is warehoused :
public class OrderService implements IOrderService {
@Override
@Transactional
public Boolean initOrder() {
// Initial master order
return Boolean.TRUE;
}
private Boolean initProdcut(Long productId) {
// Initialize the item corresponding to the order
return Boolean.TRUE;
}
}
Method initProduct The transaction does not take effect , The scene appears as above .
First, by analyzing that the transaction does not take effect, the steps are as follows :
1、 Check whether the storage engine is innodb.
2、 Check whether the transaction in the project is effective , Check whether the project correctly opens the transaction .
3、 Check whether the configuration is correct , Main inspection :@Transactional Annotated rollbackFor The scope of the exception caught is smaller than the exception thrown by the code , Cause not to roll ; Set the... Of the transaction timeout Time , Code execution timed out , Cause the transaction to fail
; By @Transactional Annotation modifies the method , Modifier is not public Or be final modification . Aop There is no way to generate a proxy for it .
4、 Does the project exist this call .
Through analysis, it used to belong to the second 4 spot ; Then I'll pass JDK Dynamic proxy implementation code to analyze why it doesn't work . Check that the user is printing the calling method log while adding a new user , The code is as follows .
User interface :
public interface IUserService {
/**
* New users
* @return
*/
public Integer addUser();
/**
* Check user information
* @return
*/
public Boolean checkUser();
}
The operation user realizes :
public class UserService implements IUserService {
@Override
public Integer addUser() {
System.out.println(" New users ");
this.checkUser();
return 0;
}
@Override
public Boolean checkUser() {
System.out.println(" Check users ");
return Boolean.TRUE;
}
}
Log processor :
public class LogHanler implements InvocationHandler {
private Object object;
public LogHanler(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(String.format(" Call method to start :[%s] ", method.getName()));
Object result = method.invoke(object, args); // call target Of method Method
System.out.println(String.format(" End of method call [%s] ", method.getName()));
return result;
}
}
function :
public static void main(String[] args) {
UserService userService = new UserService();
LogHanler logHanler = new LogHanler(userService);
ClassLoader classLoader = userService.getClass().getClassLoader();
IUserService o = (IUserService)Proxy.newProxyInstance(classLoader, userService.getClass().getInterfaces(), logHanler);
o.addUser();
}
The operation results are as follows :
Call method to start :[addUser]
New users
Check users
End of method call [addUser]
A careful friend will find it No print call method checkUser. I'm adjusting the code , I pass the dynamic code class into UserService In service . At the same time, I was IUserService Add... In the interface setUserService , The code is adjusted as follows :
User interface :
public interface IUserService {
/**
* New users
* @return
*/
public Integer addUser();
/**
* Check user information
* @return
*/
public Boolean checkUser();
}
The operation user realizes :
public class UserService implements IUserService {
private IUserService userService;
@Override
public Integer addUser() {
System.out.println(" New users ");
userService.checkUser();
return 0;
}
@Override
public Boolean checkUser() {
System.out.println(" Check users ");
return Boolean.TRUE;
}
}
Run code adjustment :
public static void main(String[] args) {
UserService userService = new UserService();
LogHanler logHanler = new LogHanler(userService);
ClassLoader classLoader = userService.getClass().getClassLoader();
IUserService o = (IUserService)Proxy.newProxyInstance(classLoader, userService.getClass().getInterfaces(), logHanler);
userService.setUserService(o);
o.addUser();
}
The operation results are as follows :
Call method to start :[setUserService]
End of method call [setUserService]
Call method to start :[addUser]
New users
Call method to start :[checkUser]
Check users
End of method call [checkUser]
End of method call [addUser]
The result is the same as we expected ,checkUser Is called the . In fact, I just pass the call instance to UserService In service . We're calling... At the same time addUser I used the proxy example I passed in to call checkUser Method , Used before this call .
summary :AOP The agent is the same method used this There is no dynamic proxy implementation , Cause the transaction not to take effect .
版权声明
本文为[GreatorMan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220553102133.html
边栏推荐
- Unity C# 网络学习(四)
- Details related to fingerprint payment
- TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
- JS determines whether the numeric string contains characters
- Perfect test of coil in wireless charging system with LCR meter
- Getprop property
- [2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
- What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
- 【数据库】MySQL单表查询
- [winui3] Écrivez une copie du gestionnaire de fichiers Explorer
猜你喜欢
#define 定义常量和宏,指针和结构体
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
Perfect test of coil in wireless charging system with LCR meter
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
Record the ThreadPoolExecutor main thread waiting for sub threads
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
【数据库】MySQL单表查询
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
随机推荐
Graduation project
Raspberry pie + opencv + opencv -- face detection ------- environment construction
Record the ThreadPoolExecutor main thread waiting for sub threads
Special topic of data intensive application system design
What's the difference between error and exception
【数据库】MySQL多表查询(一)
List< Map> Replication: light copy and deep copy
直播带货表格模板-自动显示图片-自动关联系列商品
Knowledge points sorting: ES6
What are instruction cycles, machine cycles, and clock cycles?
深度学习笔记 —— 数据增广
Innovation training (II) task division
Deep learning notes - data expansion
L2-011 play binary tree (build tree + BFS)
信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer
MySQL memo (for your own query)
[2021] Spatio-Temporal Graph Contrastive Learning
js 判斷數字字符串中是否含有字符
No such file or directory problem while executing shell