Replace content of a table within a transaction

Posts   
 
    
JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 21-Sep-2008 18:01:29   

Hi all,

Sometimes I have to replace the content of a complete tabel. When I try the following, the application seems to be "hanging":

Transaction transactionManager = new Transaction(IsolationLevel.ReadCommitted, "SavepointRollback"); XXXCollection xc = new XXXCollection();

        try
        {
            transactionManager.Add(xc);
            sc.DeleteMulti(null);


            xxxEntity xe;

            int lastEntry = xxxNewCollection.Count();

             for (int i = 0; i < lastEntry; i++)
            {
                xe = new xxxEntity();
                xe.Id = xxxNewCollection[i].Id;
                ..
                ..
                xe.Save();
                transactionManager.Add(xe);
            }
            transactionManager.Commit();
        }
        catch (Exception)
        {
            // abort, roll back the transaction
            transactionManager.Rollback();
            throw;
        }
        finally
        {
            // clean up. Necessary action.
            transactionManager.Dispose();
        }

Id is primary key and also present in the collection of records I try to delete. Any suggestions? Is there a better way to clear a complete table?

Best regards,

Jan

JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 21-Sep-2008 18:38:53   

Hi all,

Bit to quick with this question:

changed

                xe.Save();
                transactionManager.Add(xe);

in

                transactionManager.Add(xe);
                xe.Save();

and it works.

I still wonder if this is the best way to do this.

Jan

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Sep-2008 20:47:13   
changed

                    xe.Save();
                    transactionManager.Add(xe);

in

                    transactionManager.Add(xe);
                    xe.Save();

and it works.

Yep, the entity should be added to the transaction in order to be saved.

JayBee wrote:

I still wonder if this is the best way to do this.

Do you still get a hanging? As far as I can see your approach is OK wink

David Elizondo | LLBLGen Support Team
JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 21-Sep-2008 23:11:29   

No, it is ok now.

I was just wondering if deleting the content of a table in this way is the best way to do it.