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

[Solved] Handle transactions.

1 Answer 138 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.
Andrey
Top achievements
Rank 1
Andrey asked on 22 Dec 2010, 02:45 PM

Hello, Telerik team!
I have a library DBHandler.dll which is develoed by someone (this man is not working with us now).
this library contains a class DBFacade
This class contains two methods (if I understand right - for handle transactions)
GetInstance()
NewInstance()

If I uderstand right I can get a new transaction (NewInstance()) or get existing (GetInstance())
Explain, please - if it right?
Why he write so??
What I should do if I want to use more than one transaction?
Thanks!

public class DbFacade
{
private static DbFacade instance;
private readonly IObjectScope scope;
private readonly ITransaction tx;
  
private DbFacade()
{
scope = ScopeProvider.ObjectScope();
  
tx = scope.Transaction;
if (!tx.IsActive)
{
tx.Begin();
}
}
  
public static DbFacade NewInstance()
{
return new DbFacade();
}
  
public static DbFacade GetInstance()
{
if (instance == null)
{
instance = new DbFacade();
}
  
return instance;
}
  
private IEnumerable<T> RetreiveData<T>(IEnumerable<T> result)
{
//...
}
  
public void Put<T>(T entity)
{
//... 
}
  
public void Remove<T>(T entity)
{
//...
}
  
//...
//... 
}

1 Answer, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 23 Dec 2010, 07:46 PM
Hi Andrey,

After looking at the code, we can conclude that the author wanted to encapsulate some of the tasks around the transaction handling in a scope. The constructor of the DbFacade class eagerly tries to start a transaction and we suspect there is logic which activates a transaction (invokes Transaction.Begin()) immediately after a Commit() operation is performed. There are transaction properties and namely AutomaticBegin that allow this behavior to happen without the need to call the Begin() method.
Here are some useful links which discuss transaction and ObjectScope management issues:
Working with Data

How To: Manage Object Scope Transactions

How To: Manage a Long-running Object Scope

Since the ScopeProvider.ObjectScope() method is called in the constructor of the DbFacade, the author's intention probably was to use one and the same object scope for managing the persistence logic. This is convenient in web sites when you want several UI controls to work with related and uncommitted objects. If the UI controls use different scopes for populating their data sources, the changes made by one scope will not be immediately visible to other scopes which have not reread their persistent objects from the database.

Within an ObjectScope you can start a new Transaction only if the previous one has finished and is not active. So, if you want to use multiple transaction activations, you should perform either a commit or a rollback and then invoke the Transaction.Begin() method. In the code of the DbFacade class there should be some logic written for the disposal of the scope. If the only Commit() is executed there, then the author of the class most probably wanted to make bulk updates to the database. To activate a new transaction in the existing scope instance you should first check if the DbFacade class has Transaction.Begin() somewhere else in the methods, otherwise you should implement your own logic for managing new transactions. What you should do is basically write something like the following:
// flush the changes
if (tx.IsActive)
{   
   tx.Commit();
}
. . .
// start a transaction again
if (!tx.IsActive)
{   
   tx.Begin();
}
There is one ITransaction instance per scope and its state is changed from inactive to active and vice versa.

Have a look at the links and get back to us, should you have further questions. 

Regards,
Petko_I
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
General Discussions
Asked by
Andrey
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Share this question
or