How to use keepConnectionOpen in DataAccessAdapter constructor

Posts   
 
    
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 22-Aug-2007 16:11:58   

Adapter / v.2.0

Hi,

I want to use the following constructor of DataAccessAdapter:

DataAccessAdapter(string connectionString, bool keepConnectionOpen, CatalogNameUsage catalogNameUsageSetting, string catalogNameToUse)

I'm not sure now how to set the keepConnectionOpen property. If the connection is kept open, does that mean I have to close the connection manually in every case? I actually don't see a reason to automatically close the connection after executing a statement. It would be great if somebody could outline the usage of that property in a few sentences. I read the corresponding article in the documentation, but that didn't clarify my question.

What's the default value for keepConnectionOpen?

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 22-Aug-2007 16:23:43   

Moved to generated code

Frans Bouma | Lead developer LLBLGen Pro
Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Aug-2007 16:29:16   

The connection is closed by default, i.e. returned to the connection pool.

If you are going to use the same DataAccessAdapter to perform multiple database actions in a row, without a user intervention, then for performance considerations you should keep the connection open and manually call adapater.CloseConnection() at the end.

Otherwise you shouldn't keep the connection opened.

KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 22-Aug-2007 16:32:26   

Thanks!

Is it a MUST to call **adapater.CloseConnection() **?

What happens if I just call adapter.dispose()?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Aug-2007 16:45:39   

Dispose will close the connection for you.

        #region IDisposable
        /// <summary>
        /// Implements the IDispose' method Dispose.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        /// <summary>
        /// Implements the Dispose functionality. If a transaction is in progress, it will rollback that transaction.
        /// </summary>
        /// <param name="isDisposing">Flag which signals this routine if a dispose action should take place (true) or not (false)</param>
        protected virtual void Dispose(bool isDisposing)
        {
            // Check to see if Dispose has already been called.
            if(!_isDisposed)
            {
                if(isDisposing)
                {
                    // Dispose managed resources.
                    if(_physicalTransaction != null)
                    {
                        if(_isTransactionInProgress)
                        {
                            Rollback();
                        }
                        else
                        {
#if !CF
                            _physicalTransaction.Dispose();
#endif
                            _physicalTransaction = null;
                        }
                    }

                    DisposePostponedDisposeCandidates();

                    if(_activeConnection != null)
                    {
                        // closing the connection will abort (rollback) any pending transactions
                        if(_activeConnection.State == ConnectionState.Open)
                        {
                            _activeConnection.Close();
                        }
#if !CF 
                        _activeConnection.Dispose();
#endif
                        _activeConnection = null;
                    }
                    if( this.InSystemTransaction )
                    {
#if !CF && !MONO
                        // set transaction and resource manager to null, so the adapter goes out of scope as soon as the System.Transactions.Transaction is going out of scope
                        _systemTransactionResourceManager = null;
                        _systemTransaction = null;
                        _systemTransactionEnlistment = null;
#endif
                    }
                    _isDisposed = true;
                }
            }
        }
        #endregion
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 22-Aug-2007 17:00:45   

Thanks a lot! That answers my question simple_smile