Sign in to follow this  
Followers 0
a237428367

When my app running in multithreading mode , when i select or add or delete or update record,both must use Transaction?

4 posts in this topic

Hi ,

I have a question when my app running in multithreading mode.

So when i select or add or delete or update record. I also must use db.BeginTransaction() and db.CommitTransaction , right ?

now my code like this :

----------------------------------------------------------------------------------------

DB = new Database(instanse, true);

When delete all record and add new record :

----------------------------------------------------------------------------------------

try
{
DB.BeginTransaction();
var list = DB.Select<Entity>(p => 1 == 1, true);
foreach (var item in list)
{
_perstOddsCacheDB.DeleteRecord(item);
}
foreach (var matchID in _Live.Keys)
{
string[] data = _oddsLive[matchID];
Entity entity = CreateEntity(matchID, data);
DB.AddRecord(entity);
}
DB.CommitTransaction();
}
catch (Exception ex)
{
DB.RollbackTransaction();
}

----------------------------------------------------------------------------------------

When Add a record :

----------------------------------------------------------------------------------------

try
{
DB.BeginTransaction();
Entity odds = DB.Select<Entity>(p => p.MatchID == matchID, true).FirstOrDefault();
if (odds == null)
{
Entity entity = CreateEntity(matchID, data);
DB.AddRecord(entity);
}
else
{
odds.CurrentOdds = data;
}
DB.CommitTransaction();
}
catch (System.Exception ex)
{
DB.RollbackTransaction();
}

----------------------------------------------------------------------------------------

When Delete A record:

----------------------------------------------------------------------------------------

try
{
DB.BeginTransaction();
Entity odds = DB.Select<Entity>(p => p.MatchID == matchID, true).FirstOrDefault();
if (odds == null)
{
Entity entity = CreateEntity(matchID, data);
DB.DeleteRecord(entity);
}
DB.CommitTransaction();
}
catch (System.Exception ex)
{
DB.RollbackTransaction();
}

----------------------------------------------------------------------------------------

When Select Record :

----------------------------------------------------------------------------------------

try
{
DB.BeginTransaction();
List<string[]> list = DB.Select<LiveOddsEntity>(p => p.CurrentOdds[OddsFieldEnum.MATCH_SEASON_NO] == seasonNo && p.CurrentOdds[OddsFieldEnum.MATCH_SEASON_NAME] == seasonName && p.CurrentOdds[OddsFieldEnum.ROUND_NUMBER] == matchDay).Select(r => r.CurrentOdds).ToList();
DB.CommitTransaction();
return list;
}
catch (Exception ex)
{
DB.RollbackTransaction();

return new List<string[]>();
}

----------------------------------------------------------------------------------------

Is it right ?

Thanks,

Best regards.

Share this post


Link to post
Share on other sites

If you update record, you should use Store or Modify to inform Perst that record was changed.
Using Modify is more efficient if you update object multiple times: this method just marks it as modified and will save it at transaction commit (or if it is swapped out of memory).

Perst supports both implicit and explicit memory deallocation (garbage collection).
GC can be expensive and cause long delays. To explicitly deallocate object you should invoke Deallocate method.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0