Spring Hibernate - Could not obtain transaction-synchronized Session for current thread

本小妞迷上赌 提交于 2019-12-22 10:29:12

问题


Why am I posting this?

After several hours of search / analysis / getting paranoid I would like to share my fault with you - I nailed down my cause of the problem.

When did it happen?

  • within the application server (in my case embedded tomcat) - tests had no problem
  • on two specific @Services on which the methods got properly annotated with @Transactional

What was not the problem?

  • Missing @EnableTransactionManagement in the @Configuration
  • Some weired AOP issue, eg having no default constructor in a Service not implementing an interface (AFAIK this problem does not exist anymore since Spring 4.0)

What was the problem?

I am using a CustomPermisionEvaluator for Security eg for securing methods like this:

@RequestMapping(value = "/{name}", method = RequestMethod.GET)
@PreAuthorize("hasPermission(null, 'SERVICE.ASSOCIATION.FIND_BY_NAME')")
public ResponseEntity<AssociationResult> findByName(@PathVariable String name) {
    //do stuff
}

For doing this I have to extend the GlobalMethodSecurityConfiguration to get my CustomPermissionEvaluator into business. In there I used @Autowiring to get access to the beans needed for the permission evaluation. And guess what - this were those 2 @Services which the TransactionInterceptor ignored.

tl;dr

Do not autowire beans into GlobalMethodSecurityConfiguration => they will not get intercepted properly afterwards.

来源:https://stackoverflow.com/questions/31059372/spring-hibernate-could-not-obtain-transaction-synchronized-session-for-current

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