ibatis中SqlMapClient和SqlMapSeesion有什么不同?

时间:2012-03-07 08:18:56

标签: ibatis spring-orm

当我读到ibatis-sqlmap-2.3.4时,我发现它们都实现了SqlMapExecutor。

SqlMapClientImpl使用提供线程安全的localSqlMapSession进行插入。

但是在spring2.5.6中,SqlMapClientTemplate的execute方法使用SqlMapClientImpl,如下所示:

  SqlMapSession session = this.sqlMapClient.openSession();
  ...
  return action.doInSqlMapClient(session);

openSession方法每次都返回一个新的SqlMapSessionImpl。

我的问题是:

为什么SqlMapClientTemplate使用sqlMapSeesion而不是sqlMapClient?

为什么在SqlMapClientTemplate中未使用sqlMapClient的localSqlMapSession?像这样使用:

 return action.doInSqlMapClient(this.sqlMapClient);

SqlMapClient和SqlMapSeesion之间有什么不同?

1 个答案:

答案 0 :(得分:3)

关于你的第一个问题,spring-orm在评论中解释:

// We always need to use a SqlMapSession, as we need to pass a Spring-managed
// Connection (potentially transactional) in. This shouldn't be necessary if
// we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
// we still need it to make iBATIS batch execution work properly: If iBATIS
// doesn't recognize an existing transaction, it automatically executes the
// batch for every single statement...

ibatis的SqlMapClient和SqlMapSession之间的区别答案可以在SqlMapClient接口的注释中找到:

/**
* Returns a single threaded SqlMapSession implementation for use by
* one user.  Remember though, that SqlMapClient itself is a thread safe SqlMapSession
* implementation, so you can also just work directly with it.  If you do get a session
* explicitly using this method <b>be sure to close it!</b>  You can close a session using
* the sqlMapSession.close() method.
* <p/>
*
* @return An SqlMapSession instance.
*/
public SqlMapSession openSession();