Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Spring REST, MQ, and Hibernate multitenancy
PostPosted: Wed Feb 22, 2017 11:16 am 
Newbie

Joined: Wed Feb 22, 2017 10:56 am
Posts: 1
Hi,
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.
But,
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.


Top
 Profile  
 
 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: 1291
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.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.