0

当我阅读 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 有什么区别?

4

1 回答 1

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();
于 2012-08-24T18:38:43.600 回答