Using SubSonic Within Transactions
First, you must add reference to System.Transaction.dll
And here is an example code:
//use the product object to test the transaction scope
In this example, "Rollback" is inferred if you don't call "Complete()". So if there's no error in any of your code, all your calls to the database will be handled within the same transaction scope. Also - you need to be sure that your DTC is running. On my Vista install the DTC service was set to Manual and I had to turn it on to make it work properly. This is my first time using System.Transaction so if you notice any errors please let me know.
It's always nice when you find out that you don't need to code anything to support a very cool feature like this :).
(Collected from Rob Conery)
And here is an example code:
//use the product object to test the transaction scope
51 using (TransactionScope scope = new TransactionScope()) {
52
53 try {
54 Product p1 = new Product(1);
55 p1.QuantityPerUnit = "10 boxes x 30 bags";
56 p1.Save("Unit Test");
57
58 Product p2 = new Product(2);
59 p2.QuantityPerUnit = "24 - 42 oz bottles";
60 p2.Save("Unit Test");
61
62 Product p3 = new Product(3);
63 p3.QuantityPerUnit = "12 - 5500 ml bottles";
64 p3.Save("Unit Test");
65
66 scope.Complete();
67
68 } catch (System.Data.SqlClient.SqlException x) {
69
70 //trap/trace/log as needed
71 throw x;
72 }
73
74 }
In this example, "Rollback" is inferred if you don't call "Complete()". So if there's no error in any of your code, all your calls to the database will be handled within the same transaction scope. Also - you need to be sure that your DTC is running. On my Vista install the DTC service was set to Manual and I had to turn it on to make it work properly. This is my first time using System.Transaction so if you notice any errors please let me know.
It's always nice when you find out that you don't need to code anything to support a very cool feature like this :).
(Collected from Rob Conery)
Comments
Post a Comment