Database component Hxj.data (17) (transaction)

Source: Internet
Author: User
Tags commit try catch

The transaction is upgraded first, so you'll have to download the new version again.

Components provide a simple transaction and do not have too many packages.

Let's start with the previous example:

using (DbTrans trans = DbSession.Default.BeginTransaction())
{
   DbSession.Default.Update<Products>(Products._.ProductName, "apple", Products._.ProductID == 1, trans);
   DbSession.Default.Update<Products>(Products._.ProductName, "egg", Products._.ProductID == 2, trans);
   trans.Commit();
}

Trans.commit (); Must be submitted, or the execution will not succeed.

If you use try catch with the following wording:

DbTrans trans = DbSession.Default.BeginTransaction();
try
{
   DbSession.Default.Update<Products>(Products._.ProductName, "apple", Products._.ProductID == 1, trans);
   DbSession.Default.Update<Products>(Products._.ProductName, "egg", Products._.ProductID == 2, trans);
   trans.Commit();
}
catch
{
   trans.Rollback();
}
finally
{
   trans.Close();
}

The INSERT, update, and Delete methods all provide dbtransaction parameters

So also just add, modify, delete can commit the transaction.

stored procedures and direct SQL execution can also add transactions.

For example:

DbTrans trans = DbSession.Default.BeginTransaction();
DbSession.Default.FromProc("Ten Most Expensive Products").SetDbTransaction(trans);

Add a transaction through the Setdbtransaction method.

The same is true of Fromsql.

You can also set the level of the transaction as follows:

Dbtrans trans = DbSession.Default.BeginTransaction (isolationlevel.readcommitted);

In general, the components of the transaction package is relatively simple, it does not appear to be particularly convenient to write, pending further improvement.

In the use of the process feel inconvenient or need to improve the hope that we can put forward, thank you.

The following section describes the batch process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.