Transactions are used in the business layer. Many experts call them lightweight transactions. The Code is as follows, which is very simple.
Public class companybusiness
{
Private readonly companydao Dao = new companydao ();
Public bool add (Company, rating, ref int companyid)
{
Transactionoptions Options = new transactionoptions ();
// Variable data can be read during the transaction, but cannot be modified. You can add new data during a transaction.
Options. isolationlevel = isolationlevel. repeatableread;
Using (transactionscope scope = new transactionscope (transactionscospontion. required, options ))
{
Try
{
Dao. Add (Company, rating, ref companyid );
If (company. categoryid! = 0)
{
New servicecategorybusiness (). updateservicecompanynumber (company. categoryid );
}
If (company. addressid! = 0)
{
New areabusiness (). updateservicecompanynumber (company. addressid );
}
// Submit the transaction
Scope. Complete ();
Return true;
}
Catch (exception ex)
{
Throw ex;
}
}
}
}
After testing, the system will automatically roll back after failure.