Question about connection management. Sqlite.

4 posts, 1 answers
  1. Jose Mejia
    Jose  Mejia avatar
    107 posts
    Member since:
    May 2009

    Posted 24 Apr 2012 Link to this post

    Hello.

    Suppose I've got the following code:

    using (EntitiesModel model = new EntitiesModel(Settings1.Default.ConnString))
      {
        model.Connection.StoreConnection.StateChange += ConnectionStateChange;
        model.Connection.StoreConnection.Disposed += ConnectionDisposed;
     
         ....
     
      }
     
    private static void ConnectionDisposed(object sender, EventArgs e)
    {
       Console.WriteLine("Disposed");
    }
     
    private static void ConnectionStateChange(object sender, StateChangeEventArgs e)
     {
       Console.WriteLine("Connection state changed from {0} to {1}", e.OriginalState, e.CurrentState);
     }


    But I don't see any messages from disposing and state changing, but as I understand when context is disposed this messages should appear? Or maybe I'm wrong? Can you explain, please.


    Thanks in advance.
  2. Ady
    Admin
    Ady avatar
    589 posts

    Posted 26 Apr 2012 Link to this post

    Hello Jose,

     At  the moment 'OAConnection.StoreConnection' property returns a thin wrapper over the actual ADO connection instance (SQLiteConnection in case you are working with the SQLite backend) ; this is the LoggingDbConnection. This type is defined in the 'Telerik.OpenAccess.Runtime' assembly.
    The actual event handler is added to this instance and not the ADO connection instance. We will fix this so that the actual ADO connection instance is returned.

    In the meantime, as a workaround, you can add a reference to the 'Telerik.OpenAccess.Runtime' assembly and wrap the call to 'model.StoreConnection.StoreConnection' as follows -

    var connection = LoggingDbConnection.UnWrap(model.StoreConnection.StoreConnection)

    connection.StateChange += ConnectionStateChange;
    connection.Disposed += ConnectionDisposed;

    This should fix the problem.
    Some additional information on how connections are handled.

    OpenAccess maintains a connection pool from which connections are obtained by the context when they are required and returned back after using it.  When you obtain the actual ADO connection using 'OAConnection.StoreConnection', OpenAccess disowns the ADO connection because it does not have any control on what operations might be performed on the connection. So if the OAConnection instance is disposed in user code the underlying ADO will be closed , but it is the users responsibility to dispose the actual ADO connection once he has finished using it. Here is a what happens at each step of your code -

     using (EntitiesModel model = new EntitiesModel(Settings1.Default.ConnString))
    {
        using(OAConnection oaConnection = model.Connection)//obtain a connection from the connection pool
        {
            DbConnection adoConnection = oaConnection.StoreConnection;//remove the connection from the pool and disown it. Users responsiblity to close and dispose the adoConnection
            adoConnection.StateChange += ConnectionStateChange;
            adoConnection.Disposed += ConnectionDisposed;
     ....
        }//dispose OAConnection.adoConnection closed but not disposed
    }

    Note that if you do not dispose the OAConnection via the using block, then the adoConnection is not closed.

    Hope this clarifies things a bit more. Your Telerik points have been updated
    Do get back in case you need further assistance.

    Regards,
    Ady
    the Telerik team
    Follow @OpenAccessORM Twitter channel to get first the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
  4. Jose Mejia
    Jose  Mejia avatar
    107 posts
    Member since:
    May 2009

    Posted 27 Apr 2012 Link to this post

    Thank you very much for clear explanation!

    So, to be precise, it's desirable not to touch StoreConnection property in order not to get problems
    and leave as much as possible on OA management?

    Thanks in advance.
  5. Answer
    Ady
    Admin
    Ady avatar
    589 posts

    Posted 27 Apr 2012 Link to this post

    Hi Jose,

     Yes, you are right. You should avoid using the store connection directly. Most of what you can do with the ADO connection can be done using the OAConnection. We have fixed the StoreConnection property and it now returns the ADO connection. We are releasing a service pack today and this fix is available in it.

    Do get back in case you need further assistance.

    Greetings,
    Ady
    the Telerik team
    Follow @OpenAccessORM Twitter channel to get first the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Back to Top