- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# data matrix generator TRANSACTIONS in C#
CHAPTER 12 TRANSACTIONS Painting Data Matrix In C#.NET Using Barcode creator for .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications. www.OnBarcode.comData Matrix 2d Barcode Decoder In C# Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comFigure 12-1. A set of tables to update within a transaction So for the sample scenarios, your transaction will be composed of the following steps: 1. Create a new order for the customer in the order table 2. For each order item, decrease the on-hand amount for the item by the quantity being ordered. 3. Audit the attempt to secure the quantity. 4. Create an order item for the order. If there is not enough on hand for any item in the order, then the transaction should roll back. However, the audits should succeed whether the order is created or not. Here s the method to create the order. (See XAction.cs in the App_Code directory of the Web12 project.) public void AddOrder(int orderId, int customerID, DateTime orderDate) { using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required)) { string sql = "INSERT INTO [Order](OrderID, CustomerID, OrderDate) " + "VALUES (@orderID, @customerID, @orderDate)"; SqlConnection cn = new SqlConnection(connStr); SqlCommand cm = new SqlCommand(sql, cn); cm.Parameters.Add(new SqlParameter("@orderId", SqlDbType.Int)).Value = orderId; cm.Parameters.Add(new SqlParameter("@customerID", SqlDbType.Int)).Value = customerID; cm.Parameters.Add(new SqlParameter("@orderDate", SqlDbType.DateTime)).Value = orderDate; EAN / UCC - 14 Printer In Visual C#.NET Using Barcode creation for VS .NET Control to generate, create UCC-128 image in Visual Studio .NET applications. www.OnBarcode.com2D Printer In C# Using Barcode generation for .NET framework Control to generate, create 2D Barcode image in VS .NET applications. www.OnBarcode.comCHAPTER 12 TRANSACTIONS
Printing PDF-417 2d Barcode In Visual C# Using Barcode printer for .NET framework Control to generate, create PDF 417 image in Visual Studio .NET applications. www.OnBarcode.comQR Code Creation In Visual C# Using Barcode creator for .NET framework Control to generate, create QR Code JIS X 0510 image in VS .NET applications. www.OnBarcode.comcn.Open(); cm.ExecuteNonQuery(); cn.Close(); tx.Complete(); } } The method to create a new order item follows (also in XAction.cs in the App_Code directory of the Web12 project). public bool AddOrderItem(int orderId, int itemId, int quantity) { using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required)) { if (UpdateQuantity(itemId, quantity)) { string sql = "INSERT INTO OrderItem(OrderID, InventoryID, Quantity) " + "VALUES (@OrderID, @InventoryID, @Quantity)"; SqlConnection cn = new SqlConnection(connStr); SqlCommand cm = new SqlCommand(sql, cn); cm.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Int)).Value = orderId; cm.Parameters.Add(new SqlParameter("@InventoryID", SqlDbType.Int)).Value = itemId; cm.Parameters.Add(new SqlParameter("@Quantity", SqlDbType.Int)).Value = quantity; cn.Open(); cm.ExecuteNonQuery(); cn.Close(); tx.Complete(); return true; } else return false; } } Notice the first thing that this method does after enlisting in the transaction is to call the UpdateQuantity method (listed previously). If this method fails, it means you re out of stock on the item being ordered, and so this method will not create the order item. It also does not call Complete in this case, which in effect dooms the entire transaction. UpdateQuantity is what UPC - 13 Generation In C# Using Barcode generation for .NET Control to generate, create EAN13 image in .NET framework applications. www.OnBarcode.comISBN - 10 Printer In Visual C# Using Barcode generator for .NET framework Control to generate, create Bookland EAN image in VS .NET applications. www.OnBarcode.comCHAPTER 12 TRANSACTIONS
Data Matrix Creator In Visual C# Using Barcode creation for VS .NET Control to generate, create ECC200 image in Visual Studio .NET applications. www.OnBarcode.comData Matrix 2d Barcode Reader In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comcalls for the audit. The code creating the entry in the audit table follows (also in XAction.cs in the App_Code directory of the Web12 project). public void AuditItemUpdate(int itemId, int quantity) { using (TransactionScope tx = new TransactionScope(TransactionScopeOption.RequiresNew)) { string sql = "INSERT INTO InventoryAudit(InventoryID, Quantity, AttemptDate) " + "VALUES (@InventoryID, @Quantity, @AttemptDate)"; SqlConnection cn = new SqlConnection(connStr); SqlCommand cm = new SqlCommand(sql, cn); cm.Parameters.Add(new SqlParameter("@InventoryID", SqlDbType.Int)).Value = itemId; cm.Parameters.Add(new SqlParameter("@Quantity", SqlDbType.Int)).Value = quantity; cm.Parameters.Add(new SqlParameter("@AttemptDate", SqlDbType.DateTime)).Value = DateTime.Now; cn.Open(); cm.ExecuteNonQuery(); cn.Close(); tx.Complete(); } } Here you ll notice that the transaction option selected when creating the TransactionScope instance is RequiresNew. This breaks the work done in this method out into its own transaction. Since you want the audit to persist regardless of the outcome of the containing transaction, this work is done independent from the transaction of the caller. Using TransactionOption.Suppress would have the some effect, except the work done within this method would occur without a transaction at all. The entire transaction is then managed by this orchestration method, which gets handed a hash table of inventory IDs and quantities. (See XActionHost.aspx in the Web12 project.) private void PlaceOrder(int CustomerID, DateTime OrderDate, Hashtable OrderItems) { XAction dalTx = new XAction(); int OrderId = GetNextOrderID(); bool bSuccess = true; using (TransactionScope tx = new TransactionScope()) { dalTx.AddOrder(OrderId, CustomerID, DateTime.Now); foreach(int ItemId in OrderItems.Keys) Recognizing Data Matrix In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comPainting Barcode In Java Using Barcode generation for BIRT reports Control to generate, create Barcode image in BIRT applications. www.OnBarcode.comCHAPTER 12 TRANSACTIONS
Scanning EAN-13 Supplement 5 In Visual Basic .NET Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comBarcode Recognizer In Java Using Barcode Control SDK for BIRT reports Control to generate, create, read, scan barcode image in Eclipse BIRT applications. www.OnBarcode.com{ if (!dalTx.AddOrderItem(OrderId, ItemId, Convert.ToInt32(OrderItems[ItemId]))) { bSuccess = false; break; } } if (bSuccess) tx.Complete(); } if (bSuccess) { lblOutput.Text = "Success"; } else { lblOutput.Text = "Rolled back"; } } This is what you could call the root transaction object in COM+. It s the root of the call stack for the entire transaction. All of the work at lower levels in the stack is enlisted within the transaction created by this method (with the exception, of course, of the audit method, which declares its need for a transaction of its own). The entire call stack breaks down like this (see Figure 12-2). Scan UCC - 12 In Visual C# Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comScanning UPCA In Visual Basic .NET Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comPaint PDF417 In VS .NET Using Barcode generator for ASP.NET Control to generate, create PDF417 image in ASP.NET applications. www.OnBarcode.comDataBar Drawer In .NET Using Barcode creator for VS .NET Control to generate, create GS1 DataBar Stacked image in Visual Studio .NET applications. www.OnBarcode.comEncode Code 3 Of 9 In Java Using Barcode printer for Java Control to generate, create Code39 image in Java applications. www.OnBarcode.comMake PDF-417 2d Barcode In Java Using Barcode encoder for Java Control to generate, create PDF417 image in Java applications. www.OnBarcode.com |
|