MyBatis 3 and multiple Data Sources

1,533 views
Skip to first unread message

daver

unread,
Jul 7, 2010, 4:40:33 PM7/7/10
to mybatis-user
Hi MyBatis team,

Question for you about the ibatis.xml file:

The definition of the ibatis-3.dtd shows this:

<!ELEMENT environments (environment+)>
<!ATTLIST environments
default CDATA #REQUIRED
>

<!ELEMENT environment (transactionManager,dataSource)>
<!ATTLIST environment
id CDATA #REQUIRED
>

So, if I read that correctly, that means I can have a SINGLE data
source per environment...And clearly, one environment is what you'd
run in (e.g. production, dev, qa).

Therefore, am I correct in saying that if I want to connect to two
data sources (e.g. an Oracle database and a Sybase one) at the same
time, I really need to use TWO ibatis.xml files, each used to
initialize a SqlSessionFactory separately? Or is there something else
I'm missing?

Thanks,
-Dave

daver

unread,
Jul 8, 2010, 6:24:09 PM7/8/10
to mybatis-user
Never mind...found it upon more careful inspection:

One important thing to remember though: While you can configure
multiple environments, you can
only choose ONE per SqlSessionFactory instance.

So if you want to connect to two databases, you need to create two
instances of SqlSessionFactory, one
for each. For three databases, you’d need three instances, and so on.
It’s really easy to remember:

One SqlSessionFactory instance per database

Björn Raupach

unread,
Jul 9, 2010, 5:58:04 AM7/9/10
to mybati...@googlegroups.com
Might be offtopic, but its interesting to know how people deal with different datasources.

In our scenario we have multiple webapps - that share the same code - deployed in tomcat.
We use mybatis and SQL Server for data storage. At the moment every customer has one
database and one webapp in tomcat. Only the name of the webapp is different.
On every new release of the application I end up redeploying the application for every customer.
That becomes very tiresome...

The only thing that is different for every client is database user, password and name of database.
Database schema and application code is identical. So I am looking for a way to have one webapp
deployed in tomcat that accesses a different database depending on the request, e.g. http://www.example.org/A
accesses database A and http://www.example.org/B uses database B.

Is this a good approach? I think its possible (is it?) to inject the database name in every sql statement but how
to set up SqlSessionFactory?

Björn

Daniel López

unread,
Jul 9, 2010, 7:38:22 AM7/9/10
to mybati...@googlegroups.com
I haven't had to deal with this case, yet, but I guess it depends on
how you "inject" your SQLSessions in your code.
In our case, we store the SqlSessionFactory in the servlet context
under a given name, and the business logic specifies that name to
obtain an SQLSession to start working. So for us it would be as easy
as storing the different SQLSessionFactory objects in the context and
depending in the URL, retrieve one or another from the business logic.
Of course, if you have the SQLSessionFactory name hardcoded in the
business logic (through an annotation) it might become more
problematic, and if you use Spring to inject the SQLSession objects,
you might have to check how it deals with such use case. There is no
single solution for everything, as there is no single way to retrieve
the SQLSessionFactory from wherever your app stores it.

I hope that at least, it gives you some ideas.

S!
D.

2010/7/9 Björn Raupach <raupach...@googlemail.com>:

Björn Raupach

unread,
Jul 9, 2010, 8:12:10 AM7/9/10
to mybati...@googlegroups.com
Sorry I replied to you not to the list :-)
Reply all
Reply to author
Forward
0 new messages