The content of this section
- Transaction overview
- 1. New Object
- "Test submitted successfully"
- "Test failed Rollback"
- 2. Deleting objects
- 3. Updating objects
- 4. Save the updated object
- Conclusion
In the previous article we introduced the INSERT, Update, delete operation in NHibernate, which we look at in NHibernate. You can commit or roll back your actions through it.
Transaction Overview 1. Transactions in NHibernate (transactions)
Simple description: Requires ISession to use the transaction, do some work, commit or rollback the transaction.
Write code like this:
ITransaction tx = _session. BeginTransaction ();//Some save, update, delete and other operations Tx.commit ();
In fact, using a transaction in NHibernate uses a use-enforced resource cleanup and exception mechanism, generally like this:
using (ITransaction tx = _session. BeginTransaction ()) { try { //Some save, update, delete operations tx.commit (); } catch (hibernateexception) { tx. Rollback (); throw; }}
2. When do I use transactions?
The answer is: Use transactions at all times, even when reading and querying data. Because you do not know when the database operation failed, how to restore the original data. And NHibernate in the transaction (can pass TX.) Rollback () method) to help us accomplish these things.
Let's take a look at the example below, and we'll modify the insert, Update, and delete operations of the previous article:
1. New Object
public int createcustomertransaction (customer customer) { using (ITransaction tx = _session. BeginTransaction ()) { try { int newId = (int) _session. Save (customer); _session. Flush (); Tx.commit (); return newId; } catch (hibernateexception) { tx. Rollback (); Throw;}}}
This article takes the new object as an example, testing this method from two angles of successful commit and failure rollback.
"Test submitted successfully"
First write a test case, assuming that the test can run successfully:
[test]public void Createcustomertransactiontest () { var customer = new Customer () {Firstname = "yjing", Lastname = "Le E "}; int newidentity = _transaction. Createcustomertransaction (customer); var testcustomer = _transaction. Getcustomerbyid (newidentity); Assert.isnotnull (Testcustomer);}
To test this method, use TestDriven.NET integrated ncover (coverage of the analysis code) to see the code run coverage, and on this test method, right-click to select "Test with"-"coverage" as shown in:
When the Ncoverexplorer is opened automatically (viewing the analysis results of code coverage), we can see how the Createcustomertransaction method runs, and we find that this method successfully commits the operation and returns the new ID through the transaction. The results of the analysis are as follows:
"Test failed Rollback"
We are writing a failed rollback test because I think a "will truncate string or binary data" error is set, and the test expected exception must be specified on the test method.
[Test] [ExpectedException (typeof (Nhibernate.hibernateexception))]public void Createcustomerthrowexceptiononfailtest () { var customer = new Customer () { Firstname = "012345678901234567890123456789012345678901234567890123456789", Lastname = "Yjinglee" }; _transaction. Createcustomertransaction (customer);}
Similarly follow the above steps to test this method to see how the Createcustomertransaction method runs, because of an error (this is the "will truncate string or binary data" error), so the system throws a Hibernateexception exception, The system is rolled back at this time. The results of the analysis are as follows:
2. Deleting objects
We modify the code of the deleted object in the example above, as follows:
public void Deletecustomertransaction (customer customer) { using (ITransaction tx = _session. BeginTransaction ()) { try { _session. Delete (customer); _session. Flush (); Tx.commit (); } catch (hibernateexception) { tx. Rollback (); Throw;}}}
3. Updating objects
We modify the code of the updated object in the previous example as follows:
public void Updatecustomertransaction (customer customer) { using (ITransaction tx = _session. BeginTransaction ()) { try { _session. Update (customer); _session. Flush (); Tx.commit (); } catch (hibernateexception) { tx. Rollback (); Throw;}}}
4. Save the updated object
We modified the code in the previous example to save the updated object as follows:
public void Saveorupdatecustomerstransaction (ilist<customer> customers) { using (ITransaction tx = _session . BeginTransaction ()) { try { foreach (Customer C in Customers) _session. Saveorupdate (c); _session. Flush (); Tx.commit (); } catch (hibernateexception) { tx. Rollback (); Throw;}}}
Well, because space is limited, the above three methods here I do not test, you can refer to the steps to create object testing to test a few other methods!
Conclusion
Feeling this section of the content is very small, in the official documents of the NHibernate not much to explain the transaction, their own brains have squeezed so many things. However, in this section to lead you to learn the test tool testdriven.net another function is how to see the code run coverage, or a little harvest oh. The next section wants to continue in-depth discussion on the topic of concurrency control in the NHibernate, and has not yet thought how to write, I hope you will give comments and suggestions on this series. Thank you for your support!
Go NHibernate Tour (6): Explore the business in NHibernate