Hi,
We are using iBATIS 3.0 with Spring and iBatisWorkShop.
We inherits our DAO classes from SqlSessionDaoSupport from iBatisWorkShop.
I tried to use iBATIS SQL session like in the following code
SqlSession session = null;
try {
session = getSqlSessionFactory().openSession(ExecutorType.REUSE,
false); // NOT auto-commit
MapperInterface mapper = session.getMapper(MapperInterface.class);
// do several interface method calls
...
session.commit();
} catch (Exception e) {
if (session != null) session.rollback();
} finally {
if (session != null) session.close();
}
Here getSqlSessionFactory() is the method of SqlSessionDaoSupport, which
returns DefaultSqlSessionFactory object.
There are two issues with this code:
1) The JDBC connection associated with the SQL session has auto-commit equal
true, even the openSession() method called with autoCommit equal false. So all
statements are committed automatically.
2) The SqlSession.close() method does not close the database connection or
return the connection to the connection pool. It happens because the
openSession() method returns DefaultSqlSession object, which does not support
this functionality.
iBatisWorkShop uses the SqlSessionUtils.closeSqlSession(SqlSession,
SqlSessionFactory) method to close SQL session and correctly return the
database connection to the connection pool.
An example below using this method works fine.
SqlSession session = null;
boolean autoCommit = false;
Connection conn = null;
try {
session = getSqlSessionFactory().openSession(ExecutorType.REUSE, false);
conn = session.getConnection();
autoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
...
session.commit();
} catch (Exception e) {
if (session != null) session.rollback();
} finally {
if (conn != null) {
try {
conn.setAutoCommit(autoCommit);
} catch (SQLException e) {
}
}
if (session != null) {
SqlSessionUtils.closeSqlSession(session, getSqlSessionFactory());
}
}
So, are these two issues bugs in iBATIS or incomplete implementation of
SqlSessionFactory in iBatisWorkShop?
Thanks,
Dmitry