2012/2/25 sudarshan <sudar...@gmail.com>:
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 <sudar...@gmail.com>:
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 <kesav...@gmail.com>:
http://www.mybatis.org/spring/mappers.html#MapperScannerConfigurer
(note the page is for 1.1.0)
2012/2/27 Kesav Kumar Kolla <kesav...@gmail.com>:
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 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 <sudar...@gmail.com>:
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.