当前位置:网站首页>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 camera rotation with sliding effect (rotation)
- List< Map> Replication: light copy and deep copy
- Special topic of data intensive application system design
- 和谐宿舍(线性dp / 区间dp)
- Opencv + clion face recognition + face model training
- View analysis of scenic spots in ArcGIS
- 独立站运营 | FaceBook营销神器——聊天机器人ManyChat
- The vscode ipynb file does not have code highlighting and code completion solutions
- Deep learning notes - object detection and dataset + anchor box
- Leetcode -- heuristic search
猜你喜欢
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
MySQL - index
【数据库】MySQL基本操作(基操~)
redis数据类型有哪些
【数据库】表的查看、修改和删除
Arduino UNO r3+LCD1602+DHT11
深度学习笔记 —— 语义分割和数据集
Leetcode 1547: minimum cost of cutting sticks
泰克示波器DPO3054自校准SPC失败维修
Introduction to raspberry pie 3B - system installation
随机推荐
信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮
Jetpack -- lifecycle usage and source code analysis
COM in wine (2) -- basic code analysis
redis数据类型有哪些
Wechat payment function
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
[2021] Spatio-Temporal Graph Contrastive Learning
Painless upgrade of pixel series
The unity camera rotates with the mouse
The programmer starts the required application with one click of window bat
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
[database] MySQL multi table query (I)
Perfect test of coil in wireless charging system with LCR meter
Unity攝像頭跟隨鼠標旋轉
[winui3] write an imitation Explorer file manager
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Innovation training (II) task division
Innovation training (IV) preliminary preparation - server
用LCR表完美测试无线充电系统中的线圈