tags:

views:

84

answers:

2

I know the API state "A thread safe client for working with your SQL Maps", but I want to understand how it works a little better and was wondering if anyone was running this ina multi-threaded environment with transactions. For instance using:

void doSomeSql() throws SQLException{
  sqlMapper.startTransaction();
  sqlMapper.startBatch();
  final Map paramMap = new HashMap();
  paramMap.put("data", "data"); 
  Integer num = (Integer) sqlMapper.queryForObject("getRowCount", paramMap);//get row count
  sqlMapper.insert("insertData", paramMap); //insert row
  num = (Integer) sqlMapper.queryForObject("getRowCount", paramMap);//get row count again
  sqlMapper.executeBatch();
  sqlMapper.commitTransaction();
}

If this was used on where multiple threads can call this and there is only one shared sqlMapper object would there be some threads that are executing the batch because another thread called executeBatch()? This becomes more of an issue if I have lots of other methods doing delete's updates etc using the same sqlMapper in other threads.

I dont want start a transaction in one thread, and have another thread commit before the previous thread was done.

I understand I can synchronize on all this, but would rather not have to.

+1  A: 

Each thread will get its own DB connection, and transactions work the way you want: they're per DB connection too.

Brian
+2  A: 

Internally, org.ibatis.sqlmap.engine.impl.SqlMapClientImpl uses a ThreadLocal to store it's internal sessions, so even if many threads share the same SqlMapClient reference, the class uses a different internal object for each thread.

matt b