These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]

Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Spring REST, MQ, and Hibernate multitenancy
PostPosted: Wed Feb 22, 2017 11:16 am 

Joined: Wed Feb 22, 2017 10:56 am
Posts: 1
I have a webapp using spring/hibernate. This has 2 tasks, serve REST calls, and also listen on message queue, and insert into DB.

I implemented MultiTenantConnectionProvider interface and CurrentTenantIdentifierResolver and
My bean which returns the current tenant (called by CurrentTenantIdentifierResolver) has WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES

Now, when i send REST call, my tenant gets set fine and data is returned.
When a message arrives on my queue, i tried using JPA to save(object), but getting error since the tenant is not set.
If i set my bean to :SCOPE_SINGLETON , then when i get message on my queue, i can insert properly, since i can manually set my bean's tenant value. But my REST call won't work, since i can
get parallel REST queries..

How do i solve this.. Appreciate any help.

 Post subject: Re: Spring/Hibernate multitenancy
PostPosted: Fri Feb 24, 2017 4:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Just use a ThreadLocal variable in the CurrentTenantIdentifierResolver Spring bean.

The REST Controller can have a Web @Filter to set the CurrentTenantIdentifierResolver according to what tenant you want to connect to.

Also, as the message might contain the tenant identifier, you could set the ThreadLocal variable on the Spring CurrentTenantIdentifierResolver bean so that when you process the message, you are also using the right tenant.

Just make sure that you always reset the ThreadLocal variable using try/finally blocks because Threads are usually provided by a ThreaPool, hence they are later reused.

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.