Transaction works time out

Posts   
 
    
chaoyueg
User
Posts: 3
Joined: 18-Aug-2009
# Posted on: 18-Aug-2009 04:44:49   

Dear all: I loop the transaction,when the collection do not have multi same value, the transaction works well, however if the collection have multi same value, the page turn out " time out" error. I need your help . Thx.

The C#.NET code samethign like below.

Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "Create"); Collection<string> collection=new Collection(){'xxxxx','xxxx'}; try { for (int i=0;i<6;i++) { ProductEntity productEntity = new ProductEntity(Collection[i]); if (!productEntity.IsNew) { productEntity.TempStock = productEntity.TempStock - 2; //trans.Add(productEntity); productEntity.Save(); } } trans.Commit(); } catch (Exception e) { trans.Rollback(); throw; }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Aug-2009 06:24:34   

chaoyueg wrote:

have multi same value

What you mean when you say "same value"?

chaoyueg wrote:

Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "Create");
Collection<string> collection=new Collection(){'xxxxx','xxxx'};
try
{
   for (int i=0;i<6;i++)
                    {
                        ProductEntity productEntity = new ProductEntity(Collection[i]);
                        if (!productEntity.IsNew)
                        {
                  productEntity.TempStock = productEntity.TempStock - 2;
                                //trans.Add(productEntity);
                                productEntity.Save();
                            }
               }
 trans.Commit();
 }
catch (Exception e)
{
            trans.Rollback();
                throw;
}

Maybe it's because you are fetching when a transaction is started. What if you try to save the collection at the end of the loop?:

Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "Create");
Collection<string> collection=new Collection(){'xxxxx','xxxx'};
ProductCollection collectionToSave = new ProductCollection();
try
{
   for (int i=0;i<6;i++)
                    {
                        ProductEntity productEntity = new ProductEntity(Collection[i]);
                        if (!productEntity.IsNew)
                        {
                                productEntity.TempStock = productEntity.TempStock - 2;                                                              
                                collectionToSave.Add(productEntity);
                        }
               }

collectionToSave.Save();
 }
catch (Exception e)
{
            trans.Rollback();
                throw;
}

When you save a collection, a transaction is started inside.

Some threads related to timeout: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=12327 http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=15817

David Elizondo | LLBLGen Support Team
chaoyueg
User
Posts: 3
Joined: 18-Aug-2009
# Posted on: 18-Aug-2009 08:31:33   

thx for your reply! [have multi same value] i mean same string appeared multi times in the collection<string> collection, like Collection<string> collection=new Collection(){"1","2","3","1","2"};

and i followed your guid ,tested it , below

ProductCollection productCollectionToSave= new ProductCollection(); try { for (int i=0;i<6;i++) { ProductEntity productEntity = new ProductEntity(Collection[i]); if (!productEntity.IsNew) { productEntity.TempStock = productEntity.TempStock -2; collectionToSave.Add(productEntity); } }

trans.Add(productCollectionToSave); productCollectionToSave.SaveMulti();


however, it also turn out [time out] error,

is it because the sample product is appeared in the transaction multi times,so that it may be in chaos?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 18-Aug-2009 10:38:20   

Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "Create"); Collection<string> collection=new Collection(){'xxxxx','xxxx'}; try { for (int i=0;i<6;i++) { ProductEntity productEntity = new ProductEntity(Collection[i]); if (!productEntity.IsNew) { productEntity.TempStock = productEntity.TempStock - 2; //trans.Add(productEntity); productEntity.Save(); } } trans.Commit(); } catch (Exception e) { trans.Rollback(); throw; }

Your original code has some issues: 1- Collection[i] ...this must be a Typo and you meant collection[i] with small case c, right? 2- You are looping 6 times on a collection that only has 2 elements, I bet this shold give an IndexOutOfRangeException. 3- If the collection has some strings with the same value, then you are processing the same entities more than once, is this what you want? 4- Besides you should always avoid fetching elements inside a transaction. 5- Note: that if you are using a collection, then you don't need to use a transaction. 6- Instead of fetching entities one by one, you can fetch them all at once.

Please try the following code:

string[] stringArray = {"xxxxx", "xxxx"};
    
PredicateExpression filter = new PredicateExpression ();
filter.Add(ProductFields.Id == stringArray);

ProductCollection collectionToSave = new ProductCollection();
collectionToSave.GetMulti(filter);

foreach(ProductEntity product in collectionToSave)
{
            productEntity.TempStock = productEntity.TempStock - 2;                                  
}
collectionToSave.Save();
chaoyueg
User
Posts: 3
Joined: 18-Aug-2009
# Posted on: 18-Aug-2009 13:00:52   

thanks Walaa. i must add the loop to a transaction or add a transaction to the loop. i may try other method to fix it. thx very much.