Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
DataMapper 1.6.1
-
None
-
None
Description
The second sqlMap.OpenConnection() in the following sample causes an exception for DataMapper 1.6.1.
ISqlMapper sqlMap = Mapper.Get();
using (IDalSession session = sqlMap.OpenConnection())
using (IDalSession session = sqlMap.OpenConnection())
{
}
The exception is:
IBatisNet.DataMapper.Exceptions.DataMapperException: SqlMap could not invoke OpenConnection(). A connection is already started. Call CloseConnection first.
at IBatisNet.DataMapper.SqlMapper.OpenConnection() in C:\Devel\lib\iBatis\IBatisNet.DataMapper\SqlMapper.cs:line 204
The reason is as far as I can tell that sqlMap.OpenConnection no longer opens the underlying connection. Instead this is done "on demand".
The problem is that if the session is never used before being disposed, SqlMapSession.Dispose() does not properly cause the SqlMapper._sessionStore.Dispose() to be called:
[from IBatisNet.DataMapper.SqlMapSession.Dispose()]
if (_isTransactionOpen == false)
{
if (_connection.State != ConnectionState.Closed)
}
This finally causes the second sqlMap.OpenConnection() call to throw an exception since the
[from IBatisNet.DataMapper.SqlMapper]
public ISqlMapSession OpenConnection()
{
if (_sessionStore.LocalSession != null)
...
A possible work-around is to manually call session.OpenConnection() inside the using block.
Possible fixes could obviously be:
- to make sqlMap.OpenConnection always open the db connection (like in earlier versions, and as the name says)
- alter SqlMapSession.Dispose() to somehow cause a SqlMapper._sessionStore.Dispose() even if the connection is closed