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 -----Original Message----- From: Kyohyoh Choh [mailto:[EMAIL PROTECTED] Sent: Fri 4/20/2007 6:10 PM To: user-java@ibatis.apache.org Subject: How to set datasource thread save? 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>>