Unusual exception.Could not obtain transaction-synchronized Session for current thread

Dear bro. I met this exception with springboot 1.5.3 + hibernate5.
And ,i used the @EnableTransactionManagement at the start application class. Have the transaction aop configuration as follow code…

@Aspect
@Configuration
public class TransactionManagerConfig {
private static final String AOP_POINTCUT_EXPRESSION="execution (* *..service..*Service*.*(..))";
	    @Autowired
	    @Qualifier("transactionManager")
	    private HibernateTransactionManager transactionManager;
......
......

It worked very nice until yestardy.And then,i create a service named c.a.b.service.MessageCoreService.

@Service
public class MessageCoreService {
	@Autowired
	private MessageSourceResolverMapper mapper;
	@Autowired
	private TenantIdentifierResolver tir;
	public Map<String, MsgDetail> fetchCode(String code, Locale locale) {
		System.err.println("Thread.currentThread().getName() at MessageCoreService:" + Thread.currentThread().getName());
		System.err.println("request teanantId:" + tir.resolveCurrentTenantIdentifier());
		
		System.err.println("locale3 at MessageCoreService:" + locale.toString());
		MsgDetail enMsg = mapper.resolveCode(code, MessageSourceResolver.DEFAULT_LOCALE);
		MsgDetail localeMsg = mapper.resolveCode(code, locale.toString());
		Map<String, MsgDetail> ret = new HashMap<String, MsgDetail>();
		ret.put("defaultLocale", enMsg);
		ret.put("requestLocale", localeMsg);
		return ret;
	}

In the controller, do a test first.

        @Autowired
	private MessageCoreService msgService;
	@RequestMapping("/test")
	@ResponseBody
	public Map<String, MsgDetail> test() {
		System.err.println("111111111111111111111111111111111:");
		TenantIdentifierResolver.tempOverideTenantId(ZAppUtils.getInitDatabaseId()); // change to init database
		ServiceResult<Module> rslt = new ServiceResult<Module>();
		Locale locale = new Locale("en", "US");
		System.err.println("Thread.currentThread().getName() at ResourcesModuleController:" + Thread.currentThread().getName());
		System.err.println("request teanantId:" + tir.resolveCurrentTenantIdentifier());
		return msgService.fetchCode("e90001.5", locale);

Then, the hibernate exception “Could not obtain transaction-synchronized Session for current thread” occured.

I checked over and over,everything is ok.But it’s aways occured.
I don’t understand any of this. And then,i coped c.a.b.service.MessageCoreService. to c.a.c.service.MessageAdminService. Yes,just change a direcory,rename it.
In the controller,add a method.

       @Autowired
	private MessageCoreAdminService msgAdminService;
	@RequestMapping("/test2")
	@ResponseBody
	public Map<String, MsgDetail> test2() {
		System.err.println("111111111111111111111111111111111:");
		TenantIdentifierResolver.tempOverideTenantId(ZAppUtils.getInitDatabaseId()); // change to init database
		ServiceResult<Module> rslt = new ServiceResult<Module>();
		Locale locale = new Locale("en", "US");
		System.err.println("Thread.currentThread().getName() at ResourcesModuleController:" + Thread.currentThread().getName());
		return msgAdminService.fetchCode("e90001.5", locale);
	}

And then ,an incredible scene appeared. When i access the test2,it worked normally, but the test method ,it still show me the “transaction-synchronized Session” exception. Oh ,I feel like I’m breaking down. I can’t resolve the problem.
Pls help.

1 Like

Check the logs (compile and execution) your Aspect is probably not applied for some reason: typo, config, …

I am also getting this error . Not sure why ?

14:21:15.232 INFO — [ main] com.cisco.wx2.atlas.client.AdminClient [ITCLIENT_f0665172-6a94-44bb-8847-b8caea8f660f] Request failed with status line HTTP/1.1 500 while processing request, (url = http://localhost:8080/atlas-server/admin/api/v1/manageUsers , trackingId: ITCLIENT_f0665172-6a94-44bb-8847-b8caea8f660f , error = ‘Could not obtain transaction-synchronized Session for current thread; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread’ )
14:21:15.232 INFO — [ main] c.c.w.a.i.c.IntegrationClientRetryAspect [ITCLIENT_f0665172-6a94-44bb-8847-b8caea8f660f] isRetryableException: statusCode 500

ClientException: HTTP Status: 500 errorCode: null message: GET failed: HTTP/1.1 500 (url = http://localhost:8080/atlas-server/admin/api/v1/manageUsers, request/response TrackingId = ITCLIENT_f0665172-6a94-44bb-8847-b8caea8f660f, error = ‘Could not obtain transaction-synchronized Session for current thread; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread’)
at com.cisco.wx2.client.Client.clientException(Client.java:718)
at com.cisco.wx2.client.Client.checkResponse(Client.java:646)
at com.cisco.wx2.client.Client.lambda$execute$4(Client.java:401)
at com.cisco.wx2.client.Client.execute(Client.java:442)
at com.cisco.wx2.client.Client.execute(Client.java:400)
at com.cisco.wx2.client.Client.execute(Client.java:395)
at com.cisco.wx2.client.Request.execute(Request.java:228)
at com.cisco.wx2.atlas.client.AdminClient.getManageUser(AdminClient.java:430)