Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: SessionFactory configured for multi-tenancy, but no tenant identifier specified

  1. #11
    Join Date
    Jul 2010
    Location
    Rome, Italy
    Posts
    5

    Default

    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:

    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;
    				}
    			}
    		}
    	}
    }
    No more references to the transactionManager. I set directly tenant identifier in the resolver.

    Here is the code of the resolver:

    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;
    		}
    	}
    
    }
    This is my solution without rewriting Spring transaction manager.

    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
    Last edited by deggesim; Sep 28th, 2012 at 02:37 AM.

  2. #12
    Join Date
    Sep 2012
    Posts
    1

    Default

    Nice write up and explanation!
    Is it possible to create a sample maven app so we can see how the whole thing is combined together?
    Last edited by maksimu; Sep 28th, 2012 at 02:29 PM. Reason: Changed "manen" to "maven"

  3. #13
    Join Date
    Jul 2010
    Location
    Rome, Italy
    Posts
    5

    Default

    manen?

    Maybe maven?

  4. #14
    Join Date
    Jul 2008
    Posts
    1

    Default

    Also vote for a small maven app. Got stuck at *ConnectionProvider implementation.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •