Spring at the synchronized keyword in the failure of the method @transactional comment

Error Method 1:

@Service
public class SynchronizedService{
    @Transactional
    public synchronized void method() {
            ...
    }
}

Error Method 2:

@Service
public class SynchronizedService{
    @Transactional
    public void methodTwo() {
        synchronized(this){
                    ...
        }
    }
}

In the above two methods, the synchronization method / code block are invalid.

the reason:

Because of Spring AOP, the transaction method @Transactional annotation is generated by a proxy class Spring handled. So, in the synchronized block and executing the proxy classes to commit the transaction void, the other thread is a chance to enter the synchronization code block. As a result, there is likely to have access to data expired, resulting concurrency issues.

solution:

1. Remove @Transactional comment.

2. The synchronized keyword mentioned controller layer.

Guess you like

Origin www.cnblogs.com/helios-fz/p/10991136.html