Sunday, June 19, 2005

[Opf3] How is Opf3 dealing with connections to the storage?

Opf3 is using ADO.NET connection pooling. The framework creates a new connection for each operation that is done on the storage. But, if the ADO.NET connection pool is not empty .NET takes the connection directly from that pool.

Example: If you are loading an ObjectSet with, let's say, > 1000 objects Opf3 creates only one connection and loads those objects using that connection. Internally when loading the ObjectSet, Opf3 creates an ObjectReader instance and populates the ObjectSet using that instance.

Transactions?
On transactions the behaviour work a little bit different. When initializing a transaction Opf3 creates one connection and caches that one until the transaction has been committed or rolled back. A transaction is always bound to one connection.


The biggest benefit of using the connection pool of ADO.NET is multiple ObjectReader at the same time. Without any problems (and on any storage) you can create multiple instances of the ObjectReader at the same time. That's only possible because each ObjectReader has his own connection.


// Get the first ObjectReader.
ObjectReader<User> or1 = context.GetObjectReader<User>("UserName Like {0}", "%John%");
// We don't close the first ObjectReader but get also a second instance.
ObjectReader<User> or2 = context.GetObjectReader<User>("UserName Like {0}", "%Smith");
// And a thirth ObjectReader working on calls.
ObjectReader<Call> or3 = context.GetObjectReader<Call>();
// ...

// Dispose the objects. The order is not important.
or2.Dispose();
or1.Dispose();
or3.Dispose();


The following example uses only one connection (because we use a transaction there). All objects in the ObjectSets are saved using the same connection!


context.StartTransaction();

try
{
   // Save all changes.
   context.PersistChanges(objectSet1);
   context.PersistChanges(objectSet2);
   // Commit the transaction
   context.Commit();
}
catch (Exception ex)
{
   context.Rollback();
   throw;
}

0 Comments:

Post a Comment

<< Home