Spring @Async: null hibernate session on LAZY collection

时间秒杀一切 提交于 2019-11-30 20:20:16
ElderMael

Spring's transaction context is preserved using ThreadLocals. This means that your SessionFactory is only available to the thread dispatching your request thus, if you create a new thread, you will get a null and a corresponding exception.

What your @Async method does is use a TaskExecutor to run your method in another thread. So the problem described above is happening with your service.

I quote from the Spring's JpaTransactionManager docs:

PlatformTransactionManager implementation for a single JPA EntityManagerFactory. Binds a JPA EntityManager from the specified factory to the thread, potentially allowing for one thread-bound EntityManager per factory. SharedEntityManagerCreator and @PersistenceContext are aware of thread-bound entity managers and participate in such transactions automatically. Using either is required for JPA access code supporting this transaction management mechanism.

If you want to preserve your annotation then you should take a look at Hibernate CurrentSessionContext and somehow manage the sessions yourself.

See this question for more info.

e.gunay

I had the same problem, spent few days trying to find a solution, finally got a solution. I would like to share the details I found for those who might have the same issue.

1st - Your @Async-annotated method should be declared in a separate bean rather than the @Controller- or @RestController-annotated bean.

2nd - You certainly need to declare the method @Transactional which is called from within @Async declared method. However, the very first method called from @Async method has to be defined @Transactional. I had the @Transactional method in second or third level in the method execution stack therefore the problem was not solved and I spent two days trying to figure out what was going on.

So the best thing to do is

@Controller
ControllerBean {

    @Autowired
    AsyncService asyncService;

    public controllerMethod() {
        asyncService.asyncMethod();
    }
}

@Service
AsyncService {
    @Autowired
    TransactionalService transactionalService;

    @Async
    public asyncMethod() {
        transactionalService.transactionalMethod();
    }
}

@Service
TransactionalService {
    @Autowired
    SomeOtherService someOtherService;

    @Autowired
    EntityRepository entityRepository;

    @Transactional
    public transactionalMethod() {
        Entity entity = entityRepository.findOne(12345);

        someOtherService.doWork(entity);
    }
}

@Service
SomeOtherService {

    @Autowired
    EntityRepository entityRepository;

    @Transactional
    public doWork(Entity entity) {
        // fetch lazy properties, save, etc. without any session problems...
        entity.getLazyProperties(); 

        entityRepository.save(entity);
    }
}
Xstian

In normal circumstances (without @Async) a transaction gets propagated through the call hierarchy from one Spring component to the other.

When a @Transactional Spring @Component calls a method annotated with @Async this does not happen. The call to the asynchronous method is being scheduled and executed at a later time by a task executor and is thus handled as a 'fresh' call, i.e. without a transactional context. If the @Async method (or the component in which it is declared) is not @Transactional by itself Spring will not manage any needed transactions.

Try to annotate the method that calls the @Async method, and tell us if worked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!