Quick Question re: UOW

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 24-Oct-2007 22:31:29   

I have a routine that has the potential to add entities to a UOW multiple times, which need to be saved to the database multiple times.

I have a table that has a "seed" column, and the column has a unique constraint on it. When a user changes a seed value, the logic cylces through the complete set of entities and fixxes the gaps in their order, starting at 1. However, because changes to the entities may conflict with existing records in the database, I first loop through the entities and set the seed to a negative number, then save and loop through again to make them positive (and save again).

What I'd like to do is ensure that both saves happen in the same transaction, and (best case) would be to use a UOW.

Is this possible, or am I barking up the wrong tree?

Hope that made sense. simple_smile

Thanks,

Phil

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Oct-2007 10:51:02   

What I'd like to do is ensure that both saves happen in the same transaction, and (best case) would be to use a UOW.

The UOW uses a Transaction to perform its work. Either you pass a transaction to it, or it uses it's own transaction.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 25-Oct-2007 16:00:24   

Walaa wrote:

What I'd like to do is ensure that both saves happen in the same transaction, and (best case) would be to use a UOW.

The UOW uses a Transaction to perform its work. Either you pass a transaction to it, or it uses it's own transaction.

What I was asking is really not about the transaction (I understand how they work with UOWs), but whether the same entity collection can be saved twice (with different values) with the same UOW.

I think I know why what I'm seeing is confusing me--since the entity collections reference the same objects in memory, they shouldn't get saved more than once to the database. I think I need a special routine an not a UOW to accompish what I'm trying to do.

Thanks,

Phil

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Oct-2007 16:36:22   

You may avoid using the same collection for both updates. You can update the entities directly in the database using something like:

UPDATE myTable
SET seed = 0-seed
WHERE some condition.

Which can be executed by using the adapter.UpdateEntitiesDirectly() method.

Then you may save the collection containing the new values. Now these 2 actions can be contained in a UOW or you can use a Transaction.