Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
MyBatis spring in a multi tenant application
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  23 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
sudarshan  
View profile  
 More options Feb 24 2012, 5:11 am
From: sudarshan <sudarsha...@gmail.com>
Date: Fri, 24 Feb 2012 02:11:22 -0800 (PST)
Local: Fri, Feb 24 2012 5:11 am
Subject: MyBatis spring in a multi tenant application
Hi needed some help in using MyBatis spring in a multi tenant
application ...

Is it possible ? Especially since i do not see how
"MapperScannerConfigurer" can be configured with sqlSessionFactory at
run time.

Thanks
Sudarshan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo  
View profile  
 More options Feb 24 2012, 4:00 pm
From: Eduardo <eduardo.macar...@gmail.com>
Date: Fri, 24 Feb 2012 13:00:10 -0800 (PST)
Local: Fri, Feb 24 2012 4:00 pm
Subject: Re: MyBatis spring in a multi tenant application
I am not sure if that can be done. MapperScannerConfigurer is a bean
that works integrated in Spring startup cycle.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sudarshan  
View profile  
 More options Feb 25 2012, 12:19 pm
From: sudarshan <sudarsha...@gmail.com>
Date: Sat, 25 Feb 2012 09:19:30 -0800 (PST)
Local: Sat, Feb 25 2012 12:19 pm
Subject: Re: MyBatis spring in a multi tenant application
Could suggest any alternative way to support multi tenancy ?

On Feb 25, 2:00 am, Eduardo <eduardo.macar...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Feb 25 2012, 12:53 pm
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Sat, 25 Feb 2012 18:53:27 +0100
Local: Sat, Feb 25 2012 12:53 pm
Subject: Re: MyBatis spring in a multi tenant application
How would you do it if using JdbcTemplate?

2012/2/25 sudarshan <sudarsha...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kesav Kumar Kolla  
View profile  
 More options Feb 25 2012, 4:49 pm
From: Kesav Kumar Kolla <kesavko...@gmail.com>
Date: Sat, 25 Feb 2012 13:49:58 -0800 (PST)
Local: Sat, Feb 25 2012 4:49 pm
Subject: Re: MyBatis spring in a multi tenant application

I've same scenario where I need to query different databases based on what
user chooses in UI.  I have both prod database and dev database but one
single UI app.  In the UI if user choses dev then in runtime I should
connect to dev database.  Currently the datasource assignment is happening
in MapperConfiguration there is no control what soever at runtime.  Regular
mybatis has support of environments but in spring integration there is no
notion of environments.

-kesav


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sudarshan  
View profile  
 More options Feb 26 2012, 4:57 am
From: sudarshan <sudarsha...@gmail.com>
Date: Sun, 26 Feb 2012 01:57:42 -0800 (PST)
Local: Sun, Feb 26 2012 4:57 am
Subject: Re: MyBatis spring in a multi tenant application
JdbcTemplate will take into its constructor the required Data source,

I am guessing SqlSessionFactoryBean is also capable of taking in the
required Data source.

However what about MapperScannerConfigurer, it does its magic at
spring startup ...

How does one get around this ?

Should i ditch spring-mybatis and move onto just mybatis ??

On Feb 25, 10:53 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Feb 26 2012, 5:09 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Sun, 26 Feb 2012 11:09:01 +0100
Local: Sun, Feb 26 2012 5:09 am
Subject: Re: MyBatis spring in a multi tenant application

> JdbcTemplate will take into its constructor the required Data source,

You got it.

JdbcTemplate can be constructed with a datasource but you cannot
inject that datasource during during startup. You must create
JdbcTemplate instances by hand, thus not using spring as a factory.

MyBatis can work in the same way. You can build SqlSessionFactories
from a datasource using the Java api.

But MapperScannerConfigurer is built to run during spring startup. It
is not a bean you can instantiate with a new() but a piece that spring
will call during startup.

This does not apply to all applications that have more than one
datasource, just to those where you have to decide with datasource you
will connect to based on some other info.

So I would say that MapperScannerConfigurer does not fit. Don't know
if you will be able to use other parts of MyBatis-Spring. It will be
great if you could post your results if you do any investigation on
this topic.

2012/2/26 sudarshan <sudarsha...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Feb 26 2012, 10:39 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Sun, 26 Feb 2012 16:39:42 +0100
Local: Sun, Feb 26 2012 10:39 am
Subject: Re: MyBatis spring in a multi tenant application
I am afraid that both Spring environments and MyBatis environments are
resolved during startup.

You can inject different datasources in Spring using new Spring 3.1
environments. I have not used them yet but my understanding is that
profiles cannot be activated after startup.

The same happens with MyBatis environments. An SqlSessionFactory can
just use one and the environment is selected during startup.

But, as said before, you can always create an SqlSessionFactory out of
a datasource at any time.

Anyway, there is always room for inprovement so comments, ideas (and
mostly patches :) ) are always appreciated!

2012/2/25 Kesav Kumar Kolla <kesavko...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kesav Kumar Kolla  
View profile  
 More options Feb 27 2012, 1:20 pm
From: Kesav Kumar Kolla <kesavko...@gmail.com>
Date: Mon, 27 Feb 2012 10:20:48 -0800 (PST)
Local: Mon, Feb 27 2012 1:20 pm
Subject: Re: MyBatis spring in a multi tenant application

Is there any way we can configure multiple mapperscannerconfigurer each
with different sqlsessionfactory?  That might solve the problem.  I tried
to configure multiple scannaer in spring but couldn't find any difference.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Feb 27 2012, 1:23 pm
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Mon, 27 Feb 2012 19:23:43 +0100
Local: Mon, Feb 27 2012 1:23 pm
Subject: Re: MyBatis spring in a multi tenant application
Yes, you can. MapperScannerConfigurer can take a sqlSessionFactory or
an sqlSessionFactoryName as parameters (depending on the version).

http://www.mybatis.org/spring/mappers.html#MapperScannerConfigurer

(note the page is for 1.1.0)

2012/2/27 Kesav Kumar Kolla <kesavko...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kesav Kumar Kolla  
View profile  
 More options Feb 27 2012, 3:09 pm
From: Kesav Kumar Kolla <kesavko...@gmail.com>
Date: Mon, 27 Feb 2012 12:09:55 -0800 (PST)
Local: Mon, Feb 27 2012 3:09 pm
Subject: Re: MyBatis spring in a multi tenant application

I don't think this will work.  As MapperScannerConfigurer if it finds the
bean with the same name then it probably will not register another bean.
 So configuring multiple scanners with different session factories will not
solve the problem.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo  
View profile  
 More options Feb 27 2012, 3:16 pm
From: Eduardo <eduardo.macar...@gmail.com>
Date: Mon, 27 Feb 2012 12:16:34 -0800 (PST)
Local: Mon, Feb 27 2012 3:16 pm
Subject: Re: MyBatis spring in a multi tenant application
Right Kesav. You can use more than one scanner but not scan the same
package with more than one.

On 27 feb, 21:09, Kesav Kumar Kolla <kesavko...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick R  
View profile  
 More options Feb 27 2012, 4:43 pm
From: Rick R <ric...@gmail.com>
Date: Mon, 27 Feb 2012 16:43:59 -0500
Local: Mon, Feb 27 2012 4:43 pm
Subject: Re: MyBatis spring in a multi tenant application

On Mon, Feb 27, 2012 at 1:20 PM, Kesav Kumar Kolla <kesavko...@gmail.com>wrote:

> Is there any way we can configure multiple mapperscannerconfigurer each
> with different sqlsessionfactory?  That might solve the problem.  I tried
> to configure multiple scannaer in spring but couldn't find any difference.

Yes this is easy and it's what I'm doing now ...

<bean id="transactionManagerOne"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSourceOne"/>
</bean>

<bean id="sqlSessionFactoryOne"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceOne"/>
 <property name="typeAliasesPackage"
value="net.learntechnology.empmaint.business.dbone.domain"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="net.learntechnology.empmaint.business.dbone.mapper"/>
 <property name="sqlSessionFactory" ref="sqlSessionFactoryOne"/>
</bean>

As long as you organize by package you'll be fine. You could also register
an annotation which is how I did it in the past...
for example

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.foo.media.service.mapper" />
<property name="annotationClass"
value="com.foo.media.service.annotation.NocrMCH" />
 <property name="sqlSessionFactory" ref="nocrMchSessionFactory"/>
 </bean>

Using this approach you can keep all your mappers in one package and just
annotate them at the top - this seems wasteful though since I bet each
MapperScannerConfigurer will then have to scan over the entire package each
time (assuming you put them all in the same package.)

It seems best to just take the first approach I've shown and as long as you
organize your domain by the db you'd be ok.

I thought the OPs issue was dealing with it at runtime though? That's an
issue I've never had to tackle. It's easy as shown above to use multiple
DBs within the app - but configuring a single Mapper to be used against a
different DB at run time - no clue there.

--
Rick R


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sudarshan  
View profile  
 More options Feb 28 2012, 2:13 am
From: sudarshan <sudarsha...@gmail.com>
Date: Mon, 27 Feb 2012 23:13:43 -0800 (PST)
Local: Tues, Feb 28 2012 2:13 am
Subject: Re: MyBatis spring in a multi tenant application
Thanks for the replies :)

My issue is mainly with Multi Tenancy, when a tenant logs in, i need
to be able to redirect him to a DB specific to him.

Hibernate provides a mechanism for this, more details can be seen here

http://relation.to/Bloggers/MultitenancyInHibernate

Hibernate 4 goes a step further and provides a explicit API for the
same, would like to know your thoughts on the above ?

Similar to Hibernate , It would be nice if Mybatis did not have to
create multiple sessionfactories, as the schema for all the DB's are
the same.

On Feb 28, 2:43 am, Rick R <ric...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Feb 28 2012, 2:41 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Tue, 28 Feb 2012 08:41:06 +0100
Local: Tues, Feb 28 2012 2:41 am
Subject: Re: MyBatis spring in a multi tenant application
Well, that is not exactly buiit in support :)

You may do that quite easily implementing your own datasource that
wraps the real datasources and switches between them base on a
ThreadLocal variable.

Something like

public class MultiTenantDataSource extends DataSource {

    private Map<DataSource> delegates;

    public Connection getConnection() throws SQLException {
        final String tenantId = TenantContext.getTenantId()
        DataSource tenantDataSource = delegates.get(tenantId);
        return tenantDataSource.getConnection();
    }

}

Note that this must be done at Spring level, not at MyBatis level. I
mean, both spring & mybatis must see the same datasource (in this case
an instance of MultiTenantDataSource).

Be careful with transactions, make sure you do not mix two datasources
in the same transaction.

2012/2/28 sudarshan <sudarsha...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Butler  
View profile  
 More options Feb 28 2012, 7:16 am
From: Jeff Butler <jeffgbut...@gmail.com>
Date: Tue, 28 Feb 2012 07:16:46 -0500
Local: Tues, Feb 28 2012 7:16 am
Subject: Re: MyBatis spring in a multi tenant application

Spring has built in support for this with the AbstractRoutingDatasource. It
worked perfectly with Ibatis , I assume it will work with MyBatis too.

Jeff Butler


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sudarshan  
View profile  
 More options Feb 28 2012, 11:29 pm
From: sudarshan <sudarsha...@gmail.com>
Date: Tue, 28 Feb 2012 20:29:24 -0800 (PST)
Local: Tues, Feb 28 2012 11:29 pm
Subject: Re: MyBatis spring in a multi tenant application
Thanks that is what i was looking for :)

"Be careful with transactions, make sure you do not mix two
datasources
in the same transaction. "

I am not a 100% sure, however I think i am doing exactly what you
outlined above too :)

I use Hibernate for all my writes and Mybatis for all my reads.

Within a transaction, if there is a write (hibernate) and then read
(mybatis) , Mybatis does not seem to be able to read the stuff written
by hibernate in that transaction

However once the transaction in committed it can do the read.

"Be careful with transactions, make sure you do not mix two
datasources
in the same transaction. "

Can you elaborate a little more on the consequences ?, does it have
anything to do with the "TransactionSynchronizationManager" ?

Any tips on how to get around it ...

On Feb 28, 12:41 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eduardo Macarron  
View profile  
 More options Feb 29 2012, 12:59 am
From: Eduardo Macarron <eduardo.macar...@gmail.com>
Date: Wed, 29 Feb 2012 06:59:22 +0100
Local: Wed, Feb 29 2012 12:59 am
Subject: Re: MyBatis spring in a multi tenant application
Sudarshan, you like living on the edge!! ;)

Each time a transaction is created the connection and the mybatis
session are stored in springs transaction manager.

If you are using database1 to read but switch to database2 when
committing, neither spring nor mybatis will note that, but you will
have inconsistences and connection leaks.

This gets far far more complex if you are mixing two technologies in
the same transaction (hibernate and mybatis). It may work, but
honestly, I have no idea! You will have to try it.

If it does not work. The first workaround is to take MyBatis reads out
of transactions (no @Transaction annotations). But note that this may
cause db inconsistences because you will be losing read locks.

The second is taking a decission... all in MyBatis or all in Hibernate.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sudarshan  
View profile  
 More options Feb 29 2012, 2:03 am
From: sudarshan <sudarsha...@gmail.com>
Date: Tue, 28 Feb 2012 23:03:04 -0800 (PST)
Local: Wed, Feb 29 2012 2:03 am
Subject: Re: MyBatis spring in a multi tenant application

I not that much of a adrenaline junkie :) I guess i put up my requirement
in a confusing fashion.

The talk about multiple databases is only for Multi Tenancy,so when a user
logs in based on the login info I should be able to point to a particular
database.

Now once I have pointed to the correct database I use Hibernate to do all
writes and Mybatis to do all reads.

I basically do not use Hibernate API's and the write side domain model to
read of the DB.

Both writes and reads will happen on same DB, Just that the DB should
change based on Login


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sudarshan  
View profile  
 More options Feb 29 2012, 11:44 am
From: sudarshan <sudarsha...@gmail.com>
Date: Wed, 29 Feb 2012 08:44:00 -0800 (PST)
Subject: Re: MyBatis spring in a multi tenant application

In order to solve my multi tenancy requirement i have started to use
AbstractRoutingDataSource

and it plays well with both hibernate and mybatis :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kesav Kumar Kolla  
View profile  
 More options Mar 1 2012, 10:49 am
From: Kesav Kumar Kolla <kesavko...@gmail.com>
Date: Thu, 1 Mar 2012 07:49:07 -0800 (PST)
Local: Thurs, Mar 1 2012 10:49 am
Subject: Re: MyBatis spring in a multi tenant application

Yup this solved my issue of connecting to different datasource at runtime.
 I implemented a threadlocal based routing datasource and that solved my
problem.
Thanks for pointing this out.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Natália Oliveira  
View profile  
 More options Oct 26 2012, 11:40 am
From: Natália Oliveira <nataliasoliv...@gmail.com>
Date: Fri, 26 Oct 2012 08:40:23 -0700 (PDT)
Local: Fri, Oct 26 2012 11:40 am
Subject: Re: MyBatis spring in a multi tenant application

I tried that with AbstractRoutingDataSource but it doesn't seems to change
the datasource

my PersistencyDAO is extending from SqlSessionDaoSupport. Do I have to
change it to JdbcDaoSupport?

I'm using MyBatis XML maps.

Any help?

Em quinta-feira, 1 de março de 2012 12h49min07s UTC-3, Kesav Kumar Kolla
escreveu:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dq  
View profile  
 More options Oct 29 2012, 1:40 pm
From: dq <d...@kayak.com>
Date: Mon, 29 Oct 2012 10:40:03 -0700 (PDT)
Local: Mon, Oct 29 2012 1:40 pm
Subject: Re: MyBatis spring in a multi tenant application

I ran into the spring initialization problem because of the Autowired final
methods in SqlSessionDaoSupport (setSqlSessionFactory and
setSqlSessionTemplate)

It's beyond me why these are final.  I ended up re-building the
spring-mybatis jar and modified SqlSessionDaoSupport so those aren't final
so I can override them in my Mapper subclass.  They're still Autowired but
now I can specify qualifiers so it can find just the one I want.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »