在使用Spring+Ibatis集成的时候,使用如下方式配置复杂类型的时候,总是报异常
异常如下:
先说解决方式,主要是在Spring配置文件中,一定要为SqlMapClientFactoryBean配置DataSource属性,不能光为DAO配置dataSoure
源码分析如下:
经代码跟踪发现问题发生在类org.springframework.orm.ibatis.SqlMapClientTemplate中的:
public Object execute(SqlMapClientCallback action) throws DataAccessException {
Assert.notNull(this.sqlMapClient, "No SqlMapClient specified");
SqlMapSession session = this.sqlMapClient.openSession();
Connection ibatisCon = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("Opened SqlMapSession [" + this.sqlMapClient + "] 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();
}
}
}
在这段代码里,spring从SqlMapSession得openSession()方法中得到了当前的session, 设定了
它的连接, ibatis就用这个session得到了test与testDetail的内容,但在得到gossip的内容时,
ibatis使用了如下的方法得到session:
类com.ibatis.sqlmap.engine.impl.SqlMapClientImpl中:
public Object queryForObject(String id, Object paramObject) throws SQLException {
return getLocalSqlMapSession().queryForObject(id, paramObject);
}
protected SqlMapSessionImpl getLocalSqlMapSession() {
SqlMapSessionImpl sqlMapSession = (SqlMapSessionImpl) localSqlMapSession.get();
if (sqlMapSession == null || sqlMapSession.isClosed()) {
sqlMapSession = new SqlMapSessionImpl(this);
localSqlMapSession.set(sqlMapSession);
}
return sqlMapSession;
}
在这个方法中可以看出,对于SqlMapClientImpl来说,session应当会被保存在localSqlMapSession中,
在使用时再得到. 问题出现了, spring也是从这个类得到session的, 但在得到session方法:
public SqlMapSession openSession() {
SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
sqlMapSession.open();
return sqlMapSession;
}
中, 这个session根本就没有保存进localSqlMapSession, 于是在得到gossip的内容时, 使用的是一个
新的session. 而这个新的session没有调用过setUserConnection, 其对应的transaction也就是初始值null,
而且ibatis在后面的
类com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate中
public Object queryForObject(SessionScope session, String id, Object paramObject, Object resultObject) throws SQLException {
Object object = null;
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(session);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(session, autoStart, trans);
RequestScope request = popRequest(session, ms);
try {
object = ms.executeQueryForObject(request, trans, paramObject, resultObject);
} finally {
pushRequest(request);
}
autoCommitTransaction(session, autoStart);
} finally {
autoEndTransaction(session, autoStart);
}
return object;
}
protected Transaction autoStartTransaction(SessionScope session, boolean autoStart, Transaction trans) throws SQLException {
Transaction transaction = trans;
if (autoStart) {
session.getSqlMapTxMgr().startTransaction();
transaction = getTransaction(session);
}
return transaction;
}
根本没有检查transaction是不是null, 于是就有上面的NullPointerException了.
于是将spring方法的代码改为:
public Object execute(SqlMapClientCallback action) throws DataAccessException {
Assert.notNull(this.sqlMapClient, "No SqlMapClient specified");
SqlMapSession session = this.sqlMapClient.openSession();
Connection ibatisCon = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("Opened SqlMapSession [" + this.sqlMapClient + "] for iBATIS operation");
}
Connection springCon = null;
try {
ibatisCon = this.sqlMapClient.getCurrentConnection();
if (ibatisCon == null) {
springCon = DataSourceUtils.getConnection(getDataSource());
this.sqlMapClient.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(this.sqlMapClient);
}
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();
}
}
}
一切就好了.
ibatis的openSession在上一版本时,每次打开的都是同一session, 因为它原来的代码在每次打开前会去localSqlMapSession中查找,如果没有才会打开个新的,而且会在返回前放入localSqlMapSession中,这被认为是一个BUG,所以在这个版本上改为返回一个新的session.而在这个session关闭时,才会把这个session返回给sessionPool中.
而在spring中,如楼上我的贴子所说,新打开的session没有进入localSqlMapSession,在打开一对多的表的子表时, 于是从sessionPool中得到一个没有被spring设置过setUserConnection的session,从而Transaction trans = getTransaction(session);这里没有得到缺省的事务,那么 trans = autoStartTransaction(session, autoStart, trans);代码里就会去找这个session的txManager来启动一个事务,但txManager又是null,于是就会发出nullpoint异常了.
这时关键就是session的txManager是什么时候设置的. 我们可以看一下spring的SqlMapClientFactoryBean的代码就会发现,只有设置了它的dataSource属性时,才会去设置session的txManager, 所以这里就可以看出在一对多表时,SqlMapClientFactoryBean的dataSource属性一定要设置, 这样能保证在一对多时才不会出问题.