Nested UnitOfWorks / Save operations for objects which are not part of UnitOfWork

Hi all,

I am currently facing a problem related to UnitOfWork. Within the Using block of a UnitOfWork, I would like to perform a save operation for an object which is not part of the UnitOfWork. The save opersion is executed with no result in the database, but also no error or similar. I tried to nest UnitOfWork by creating another UnitOfWork within the first one, but this also did not help. The commit operation of the nested UnitOfWork has no effect.

Background to what I am trying to do: In a custom table, I store some opertaions, which have to be done based on some conditions. These operations can be marked as UnitOfWork. I have the following challenge:

  1. On the one hand, while performing these operations and putting them in the UnitOfWork I want to report the status of the operation (in progress, waitToCommit, error, etc.) as soon as the operation is done, and can't wait until I perform the commit operation.
  2. On the hand, the operations stored in the table must be perform as a UnitOfWrok

To do this, I need to perfrom a save operation of the IEntity object, for which I have to report the status. The object is not part of the UnitOfWok and the save operation needs to be done within the Using block, as mentioned above.

It seems that with starting the UnitOfWork a transaction is started and all database operations are executed when the commit operation is executed, even for objects which are not part of the UnitOfWork? Is this correct?

Is there any possibility to interrupt the transaction and to restart it again? Or to start the transaction only for the UnitOfWork, so that other database operations can be done independently?

As a workaround, I created a new collection, in which I collect the operations I want to perform as a UnitOfWork separately, and add them later to a UnitOfWork at once. Doing so I am able to perform db operations. But here I have the disadvantage that errors while committing these operations can't not be catched and handled before. My question here (if the above requirements are not possible): How can I check if the commit operation would result in error or not? How can I make the same check, which is done by IUnitOfWork.Put, without putting the object in the UnitOfWork? Is there any something like TryPut or TryCommit? As far I understood, the IUnitOfWork.Put operation checks if errors would occur.

Thanks a lot for your supprt in advance!

Parents
  • How can I check if the commit operation would result in error or not? How can I make the same check, which is done by IUnitOfWork.Put, without putting the object in the UnitOfWork? Is there any something like TryPut or TryCommit? As far I understood, the IUnitOfWork.Put operation checks if errors would occur.

    To clarify here I mean with check performing e.g. OnSaving scripts.

  • This is what a development colleague told me about this.

    "UnitOfWork objects start a transaction in the underlying session. Only one physical transaction per session against the database is possible. All other inner transactions (from additional units of work, etc.) are only logical transactions that increment/decrement a transaction counter. To get changes into the database without paying attention to the transaction, you would have to open a second session.

     

    There is no TryPut or TryCommit, as errors could come from different parts of the system at this point (business logic, referential integrity, database triggers, etc.).

     

    The customer’s reporting table could be accessed with the modifier “with (nolock)”, so that the changes of uncommitted transactions could already be seen. The downside is, that these changes may never be in effect when an error and the subsequent rollback occurs."

  • To get changes into the database without paying attention to the transaction, you would have to open a second session.

    Thank you Markus so much for your answer. Can you be please here more specifig. Do you mean to open a second session using 1IM API? Do you have here a code example to do that?

    The customer’s reporting table could be accessed with the modifier “with (nolock)”, so that the changes of uncommitted transactions could already be seen.

    This is still unclear for me. What do you mean by customer's reporting table?

    There is no TryPut or TryCommit, as errors could come from different parts of the system at this point (business logic, referential integrity, database triggers, etc.).

    mm, but when you I put an object in the UnitOfWork, there are some checks done already, so TryPut would also do the same but without really putting the object at the end in case of successfully performing the check. Anyhow, as a summary such a functionality does not exist.

  • One more question please: When I put an object in the UnitOfWork, its property IsReadOnly is changed automatically to True. The same is true if I save the object in case of not using a UnitOfWork. Is it possible to reset this property. Thing is I would like to work with same object and save it step-wise, without the need to reloard it every time to reset the property IsReadOnly to false again. Is this possible?

  • You cannot save only parts of an entity object. If you need to do so, for example, change 3 properties execute a put (to UOW) or a save operation, try with the next 3 properties, etc., then you need to code this on your own.

Reply Children
No Data