omar wrote:
I am porting some of my code from DataAccessAdapter to UOW and want to verify the transactional behaviour of UOW:
1- UOW.Commit(adapter, False) will not commit the uow elements. Is that the same as not calling uow.commit at all.
example:
If myFlag=True Then
uow.Commit(adapter,True)
End if
is the same as
uow.Commit(adapter, myFlag)
No. Commit performs the actions you've been storing inside the UoW. Not calling Commit therefore doesn't do anything. Calling Commit without telling it to autocommit the transaction does perform the actions but the transaction isn't committed yet (only the UnitOfWork) and therefore you can perform more actions inside the same transaction.
2- UOW.Commit(adapter, True) will commit the uow elements BUT if any element fails then all uow elemnts are rolledback
It will perform all actions in the UoW. If something fails, it will rollback its current transaction, which is the transaction the adapter is in, IF autocommit is true. So if you've passed adapter to other UoW's and not specified 'true' with Commit, those actions are rolled back as well.
You can use savepoints in between the UoW calls. :
Dim adapter As New DataAccessAdapter()
adapter.StartTransaction(IsolationLevel.ReadCommitted, "SaveUoWs")
uow1.Commit(adapter, False) ' don't commit the transaction yet
' save the transaction at this point.
adapter.SaveTransaction("uow1");
'...
uow2.Commit(adapter, False) ' don't commit the tranasaction yet
' save the transaction at this point.
adapter.SaveTransaction("uow2");
'...
uowN.Commit(adapter, True) ' commit all
THis is of course a bogus piece of code but it illustrates what's going on. If in between something is going wrong, you can roll back to a savepoint, like uow1 and skip uow2 for example, but still keep the actions performed by uow1.
- Is an exception the only cause a uow elemnt would fail? If NO then would a UOW object raise an exception if it fails?
UoW's just execute actions like you would do that too in your code. So if something is seriously wrong, like a query crashes inside the DB, an exception is received and the whole transaction is aborted, IF autoCommit is set to True. If autocommit was set to false, the transaction isn't rolled back, because the caller apparently wanted to do more with the transaction and has commit code in place, which comes with rollback code as well, normally, so it then won't roll back the transaction. In all cases the exception is rethrown. If a save fails but no exception is thrown, the UoW isn't aborting the transaction. This is the case if validation code reports a failure but doesn't throw an exception, for example if validation isn't critical for a transaction but is critical for that particular entity.
3- If a UOW.Commit raises an exception, my exception handler does NOT need to rollback the UOW object because it does that for us?
If you specify true for autoCommit then yes. If you specify false for autocommit, you have an adapter.Commit() somewhere in your own code anyway, which also has a catch and an adapter.Rollback.