Spring-boot + Shiro invalidate affairs

Today in the development process, encountered a situation, the transaction is a transaction, other service projects in the same transaction, the transaction may be that there is a failure.

It excludes various situations

1. Verify that the database engine is innoDB

2. whether the method is public

3. This refers to transaction management configuration spring-tx.xml

Excluding these cases, it was found or not, it is entirely a little puzzled.

After some investigation, it found and Shiro combination of problems caused.

 

Check the following explanation:

Since ShiroFilterFactoryBean realized FactoryBean interface, so it can be initialized in advance. And because SecurityManager, SecurityManager implementation class depends on the Realm, Realm implementation classes are also dependent on UserService, so raise all relevant bean initialization in advance.

ShiroFilterFactoryBean -> SecurityManager -> Realm implementation class -> UserService

But this time was just the ApplicationContext registerBeanPostProcessors registration phase BeanPostProcessor processor, this time AnnotationAwareAspectJAutoProxyCreator not yet registered to the BeanFactory, UserService not enjoy transaction!

Personal understanding is probably because the use of the Realm in the service, the equivalent of instantiated once, because it is a singleton, behind all adopt this reference. Due to initialize objects have not been able to manage the transaction, resulting in this issue .

Solution:

In the Realm using Mapper implementation, rather than directly using the Service object. Disadvantages: direct interaction with the database, and no logic to interact Service as well as cache
initialization time to join @Lazy comment on the Realm of Service statement, delay Realm implementation Service object, so as to ensure the actual initialization of the Service will be BeanPostProcessor interception, create a proxy object that has transactional capabilities

Guess you like

Origin www.cnblogs.com/sunxun/p/11309451.html