Uploaded image for project: 'iBatis for .NET'
  1. iBatis for .NET
  2. IBATISNET-291

SqlMapper.OpenConnection() does not actually open connection

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • DataMapper 1.6.1
    • None
    • DataMapper
    • 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())

      { // session for some reason never used, could be due to logic in called methods }

      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)

      { _sqlMapper.CloseConnection(); }

      }

      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)

      { throw new DataMapperException("SqlMap could not invoke OpenConnection(). A connection is already started. Call CloseConnection first."); }

      ...

      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

      Attachments

        Activity

          People

            Unassigned Unassigned
            hamnered Jonas Hamnered
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: