ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kyohyoh Choh <C...@jp.ibm.com>
Subject RE: How to set datasource thread save?
Date Mon, 23 Apr 2007 05:46:07 GMT




Hello
Yes, if we use setDatasource() of sqlMapClientTemplate for a singleton DAO,
datasource is changed for all threads of the DAO instance.
Routing many SqlMapClient to a single DAO is interesting, would you please
post your config file? Also I want to know whether you specified
sqlmapconfig file for each sqlmapclient or not.

Thank you very much



                                                                           
             "Chris Lamey"                                                 
             <clamey@localmatt                                             
             ers.com>                                                   To 
                                       <user-java@ibatis.apache.org>       
             2007/04/22 05:42                                           cc 
                                                                           
                                                                   Subject 
             Please respond to         RE: How to set datasource thread    
             user-java@ibatis.         save?                               
                apache.org                                                 
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




The Spring SqlMapClient is defintely thread safe.  I've had thousands of
concurrent threads hitting a single SqlMapClient and each one has proper
transaction handling and caching.  I use Abator generated DAOs, not the
Spring ones, but my guess is that the Spring DAOs are thread-safe too
because they are widely used.

The issue is that using iBATIS with Spring doesn't have a clear 'best
practice' way of dynamically switching between DataSources at runtime.

My guess is that if you go with a singleton DAO instance, have hundreds of
concurrent threads hitting it, and change the datasource at runtime for
each thread, you are then changing out the datasource for all threads
hitting the DAO at that point.

What you are doing now is creating a new DAO instance for each call per
thread.  This allows each thread to have its own DAO on its stack, which
limits the touch points betwen threads.  I remember that you said you are
not using caching in the SqlMapClient, which is good, because if you were
this setup would not work.  The SqlMapClient doesn't know you are switching
out DataSources and would merge all the results into one cache for all
DataSources.

Creating a new DAO for each call per thread is not something I would
personally want in my code.  In my opinion, there's no reason DAOs can't be
singetons and to have so many created and thrown away is an indication of a
design problem.  Plus I have lots of caching in iBATIS and cannot afford to
stop using the cache.  So I created that RoutableSqlMapClient class based
on Spring's AbstractRoutableDataSource, which dynamically routes calls by
the singleton DAOs to any number of SqlMapClients, each with their own
DataSource, cache, and transaction handling.  Any number of threads with
the same DataSource can be in their respective SqlMapClient/DataSource
combo, as long as the DataSource doesn't have to change within the run of a
thread, all is well.

Cheers,
Chris







Hello all

Our application use Spring+iBATIS, need to switch among multiple
datasources based on user's request datasourceid parameter. We are testing
setDataSource() method of SqlMapClientDAOSupport class. We found if we set
our DAO class which extends SqlMapClientDAOSupport to be singleton
class(Spring's default value), the datasource can't set correctly when
users access concurrently. If we set singleton=false, everything goes will.
The user's datasourceid parameter is hold as thread local. Does this mean
SqlMapClientDAOSupport is not thread save? This is my first time to use
Spring+iBATIS, if I miss something, please forgive me and point out.

Sippete of our DAO class

            DataSource datasource = null;

            //get datasource from ApplicationContext
            datasource = (DataSource) ctx.getBean(datasourceID);

            //set datasource
            SqlMapClientTemplate sqlMapClientTem = null;
            sqlMapClientTem = getSqlMapClientTemplate();
            sqlMapClientTem.setDataSource(datasource);
            domain = sqlMapClientTem.queryForObject(selectID , inobj);

Thank you


[添付ファイル "winmail.dat" は Kyohyoh Choh/Japan/IBM が削除しました]


Mime
View raw message