This project has moved. For the latest updates, please go here.

Now we see how LunaOrm manage Datasource Transaction. To understand this wee have to understand the LunaTransactionBox.

LunaTransactionBox is ... as a box. When you create them every Luna Object created in them is putted inside. When you Commit the transactionbox all change is committed. 

The best mode is to see code... very simple.

EXAMPLE

This code load a list of Customer from Datasource, and update the city for any Customer. Simple and not Intelligent thing, but is for example :)

 

Using Tb As LUNA.LunaTransactionBox = LUNA.LunaContext.CreateTransactionBox()

  Try
    Using Mgr As New CustomersDAO

      Dim lCustomer As List(Of Customer) = Mgr.GetAll("LastName")

      For Each C As Customer In lCustomer
        Tb.TransactionBegin()
        C.City = "Rome"
        C.Save()
        Tb.TransactionCommit()
      Next

    End Using
  Catch ex As Exception
    Tb.TransactionRollBack()
    Throw ex
  End Try

End Using

 

As you can see Code is very simple. There is only a few thing to understand to use them. So:

 

1) CREATE EVER LUNATRANSACTIONBOX BEFORE ANY OTHER THING

Every time you use a LunaTransactionBox, you have to create them as first thing. Not first in your code, but first in the object list that you want to are included in transaction.

When you create a TransactionBox, the first object that use them active a new parallel Datasource Connection to Database. Transactionbox manage automatically the connection life, you have to do nothing else use them. 

Every DaoClass manage his datasource connection. So every time you access Data LunaEngine check if there is FOR YOUR THREAD and in the specific StackTrace of the DAOObject an active LunaTransactionBox. If true DAO Object use the LunaTransactionBox Connection and transaction instead a normal connection.

So is important that you Create at first the LunaTransactionBox,

 

2) USE THE USING INSTRUCTION TO CREATE TRANSACTIONBOX

As you can see in this example I suggest to use EVER the USING instruction, because LunaTransactionBox, terminate Connection and dispose all object when is called his Dispose. With Using command you are sure that this happen when you want. 

 

TRANSACTION METHOD

TB.TransactionBegin()

As in normal Transaction when you want to iniziate the real transaction call this method

TB.TransactionCommit()

As in normal Transaction use them to save change to database.

TB.TransactionRollback()

As in normal Transaction use them to undo change and terminate transaction.

 

NOTE

Note that you can use only a Transactionbox  with multiple TransactionBegin / TransactionCommit instead. 

 

ISOLATION LEVEL

How can you manage or change Isolation Level for Transaction? 

When you generate your code you can find a file named LunaContext.vb in your LunaUserClass\ClassBase folder. With this file you can extend the LunaContext (that is a Partial Class in LunaEngine).

So at first you find this row of code:

Public Shared Property IsolationLevel As Data.IsolationLevel = IsolationLevel.ReadUncommitted

With this you can change Isolation Level for Transaction that by default is ReadUncommitted.

 

IMPORTANT 

I think in a future version of LunaEngine the better thing is to put this settings inside app.config or web.config if you want to change them. 

Last edited Mar 13, 2014 at 11:19 AM by Lunadix, version 1