我们在利用Spring的ibatistemplate的时候,采用这样的方式:
getSqlMapClientTemplate().queryForList("TestSpace.qryTest");
而SqlMapClientTemplate的queryForList,最终其调用execute方法,execute中,SqlMapSession session = client.open();
这样导致每执行一条sql,都要开启一个ibatis会话,感觉这样的话,就不存在会话的概念了,请高手帮忙把这块的疑问澄清一下,谢谢。
getSqlMapClientTemplate().queryForList("TestSpace.qryTest");
而SqlMapClientTemplate的queryForList,最终其调用execute方法,execute中,SqlMapSession session = client.open();
这样导致每执行一条sql,都要开启一个ibatis会话,感觉这样的话,就不存在会话的概念了,请高手帮忙把这块的疑问澄清一下,谢谢。
1.queryForObject:应用开发时调用模板的方法
public Object queryForObject(final String statementName, final Object parameterObject)
{
return execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) {
return executor.queryForObject(statementName, parameterObject);
}
});
}
public Object execute(SqlMapClientCallback action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Assert.notNull(this.sqlMapClient, "No SqlMapClient specified");
SqlMapSession session = this.sqlMapClient.openSession();
Connection ibatisCon = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
}
Connection springCon = null;
try {
ibatisCon = session.getCurrentConnection();
if (ibatisCon == null) {
springCon = DataSourceUtils.getConnection(getDataSource());
session.setUserConnection(springCon);
if (logger.isDebugEnabled()) {
logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
}
}
return action.doInSqlMapClient(session);
}
catch (SQLException ex) {
throw getExceptionTranslator().translate("SqlMapClient operation", null, ex);
}
finally {
DataSourceUtils.releaseConnection(springCon, getDataSource());
}
}
finally {
// Only close SqlMapSession if we know we've actually opened it
// at the present level.
if (ibatisCon == null) {
session.close();
}
}
}
是否每次调用都重新开启一个会话 是看你用的连接池实现
这里通过回调 重点是让你避免直接创建Session 只要你不乱来 其他的就不用管..