Ok, I followed your suggestion.
I implemented my CurrentTenantIdentifierResolver and used a ThreadLocal to store tenantIdentifier (always by the advice).
It is the new Advice:
No more references to the transactionManager. I set directly tenant identifier in the resolver.Code:@Aspect @Order(1) public class TenantAdvice { @Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)") public void openSession() { } @Before("openSession()") public void injectTenant(JoinPoint jp) { MethodSignature signature = (MethodSignature) jp.getSignature(); Method method = signature.getMethod(); Annotation[][] paramAnnots = method.getParameterAnnotations(); Object[] args = jp.getArgs(); int i = 0; for (Annotation[] annotations : paramAnnots) { Object arg = args[i++]; for (Annotation annotation : annotations) { if (annotation instanceof Tenant) { String tenantIdentifier = arg.toString(); MyCurrentTenantIdentifierResolver.setTenantIdentifier(tenantIdentifier); break; } } } } }
Here is the code of the resolver:
This is my solution without rewriting Spring transaction manager.Code:public class MyCurrentTenantIdentifierResolver implements CurrentTenantIdentifierResolver { private static ThreadLocal<String> threadLocal = new ThreadLocal<String>(); public static void setTenantIdentifier(String tenantIdentifier) { threadLocal.set(tenantIdentifier); } @Override public String resolveCurrentTenantIdentifier() { return threadLocal.get(); } @Override public boolean validateExistingCurrentSessions() { String tenantIdentifier = threadLocal.get(); if (tenantIdentifier != null) { return true; } else { return false; } } }
EDIT: post written without reading your last answer. Thank you for your code. It will be a good hint when I'll decide to use Spring Security![]()


Reply With Quote
