This is a migrated thread and some comments may be shown as answers.

Question about connection management. Sqlite.

3 Answers 99 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jose Mejia
Top achievements
Rank 1
Jose Mejia asked on 24 Apr 2012, 09:57 AM
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.

3 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 26 Apr 2012, 04:17 PM
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!
0
Jose Mejia
Top achievements
Rank 1
answered on 27 Apr 2012, 12:17 PM
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.
0
Accepted
Ady
Telerik team
answered on 27 Apr 2012, 01:04 PM
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!
Tags
General Discussions
Asked by
Jose Mejia
Top achievements
Rank 1
Answers by
Ady
Telerik team
Jose Mejia
Top achievements
Rank 1
Share this question
or