OpenAccess relies on lazy loading and change tracking to make is as easy as possible for the user to work with persistent data. But we need a point in time where we can collect all changes in memory and execute the insert, update and delete statements to persist the changes. Typically this is the job of a transaction.
The easiest way is to never let OpenAccess run without a started transaction. In this case you only have to call commit or rollback.
As you can see with one line of code executed after you finished your business operation everything is persisted. With this setup you lose one OpenAccess feature, if no transaction is started the ObjectScope is in read-only mode. Read only scopes help you to find write access to persistent classes where it is not allowed in your work flow. If a use case is only reading you should not start a transaction, if a use case has the allowance to write just start a transaction.
Microsoft .NET comes with a build-in transaction framework. OpenAccess supports this framework as well and it is the way to integrate OpenAccess into distributed transactions. The integration can be used in explicit or implicit mode. The implicit mode will register OpenAccess in a running transaction when the first write operation on persistent data is executed.
The explicit mode needs a registration of OpenAccess in the running transaction.
If you use the System.Transactions integration you have a 2 phase commit immediately, the object scope and the underlying ADO.NET connection are both registered in the transaction. This guarantees that both resources are handled together and you that can reuse the same scope in another transaction.
If you do not want this overhead and if you do not need 2-phase commit you should use one of the two OpenAccess transaction scenarios.
Subscribe to be the first to get our expert-written articles and tutorials for developers!