Online ordering with ASP and COM +

Source: Internet
Author: User
Tags commit error handling insert requires


We already understand the transaction and know how to manipulate it from the ASP, as an example below. This section begins with the definition of a business requirement and its associated requirements, and is then designed and implemented by using ASP and COM + transactions.
19.5.1 Business Needs
In this example, the business need is the front-end part of the order Processing system. Order Processing system to generate orders, but also to adjust the inventory quantity. It is important that you adjust the inventory quantity correctly each time you accept an order.
Since this example consists of two tasks (generating orders and adjusting inventory quantities), it is necessary to place these tasks in a separate transaction. This allows you to take advantage of COM + functionality. COM + can provide all the major work for the transaction, all we have to do is tell COM + to commit or terminate the transaction.
19.5.2 Design
This example can be decomposed into a typical three-layer structure, and is also suitable for Microsoft's DNA structure. At the top level, this example is an ASP user interface (UI) that looks at the various products in the SQL Server 7.0 database. However, the ASP code does not interact with the database, which retrieves data from a recordset form that is never connected by a COM component.
Retrieves an order through the ASP user interface, which invokes a transactional COM component that inserts an order into the database while also adjusting the inventory quantity.
1. Data Services
As mentioned above, the database resides in SQL Server 7.0. The reason for choosing SQL Server is that it supports COM + (DTC) transactions. You can also use SQL Server 6.5 if you want.
In this example, there are two tables, one is the product table, the other is the order table. For simplicity, the table names used in databases called ASP transactions are orders and products, respectively, as shown in Figure 1 9-1 4 and Figure 1 9-1 5.

You should include some sample data in the Products table so that you can return to the ASP page.
Most applications are built on more complex database designs, and their databases contain stored procedures, triggers, and scheduling tasks. However, as an example, the above table is sufficient.

We have provided the script in the downloadable source code of this book, which creates these tables and inserts some data into the products datasheet.
2. Business logic
The business logic for this example will be encapsulated in an ActiveX DLL built by VB. The DLL contains two classes: One class gives the caller the ability to retrieve a list of available products, and another class submits an order. The class that retrieves the product list is non-transactional, and the class of the update list is transactional to ensure that two tables are changed at the same time.
19.5.3 implementation
Now open VB and create a new ActiveX DLL project. In VB, the name of the project is ASP Trans, and the name of the default class is set to order. Add a class model, called products.
If you want to be able to set the MTS Transactionmode to 2-requires Transaction in the Order class. When the order class is put into COM +, it automatically sets the component's transaction Support property to required. Remember that this default setting does not take effect until the component is added to a COM + application from the Add File dialog box.
Before you add code to a class, you must first set the correct reference, and in the References dialog box, the reference is set to the Microsoft ActiveX Data Objects 2.5 Library and the COM + Services Type Library, as shown in Figure 1 9 -1 6 shows.

You can now add code to these classes.
1. Product components
First, add a function named GetProducts in the Products class. The GetProducts method has no parameters and returns an disconnected ADO recordset.

Next, add all the code for error handling, committing, or terminating the transaction. Note that this function only returns data and does not update any resources. However, we still want to use the environment to notify COM + to reclaim these resources:


The newly added code completes the GetProducts function, which executes an ordinary SELECT statement on the products datasheet.
Note the use of the client pointers is completely different from the server-side pointers. When working with an disconnected recordset, you must use a client pointer; it notifies ADO to get all the data immediately, rather than reading a bit of data at a time. Once the data is retrieved, the code disconnects the recordset and returns the value to the caller.
2. Order Component
Next, set up a method for processing user orders. This method, named PlaceOrder, is an ordinary subroutine that requires four parameters to describe the order. PlaceOrder is not a function, nor does it return a value.

The four parameters required by PlaceOrder are the user's last name and name, the product's I d, and the quantity. The PlaceOrder program will do two main things: add a record to the order datasheet and adjust the inventory quantity for the product.
The following increases the error handling and transaction support code. As with any other method, if an error occurs, it is transferred to the Error_Handler section and the transaction is rolled back, and if there is no error, the transaction is committed:


Now, add code to insert a record in the orders datasheet and adjust the product inventory quantity. This can be done simply by creating an ADO Connection object and executing an INSERT statement and an UPDATE statement. The following code can be executed explicitly with an ADO Command object, but for simplicity's sake, this example uses only one ADO Connection object:

These are all the code required by the ActiveX DLL. For this business logic, the only thing left is to compile the D l l and install it into a COM + application.
When installing a DLL in COM +, remember to adjust the transaction support property in the Order component to notify COM + that a transaction is required when invoked. In the Transactions tab, adjust the setting to required, as shown in Figure 1 9-1 7.

The transaction support property of the products component should be configured to not supported because it performs a read-only operation on the database.
3. User interface
The user interface in this example is very simple, consisting of two pages: one to collect user order information, another to submit orders, and to inform the user whether their orders were successfully accepted.
The orders.asp page displays a variety of products to the user, allowing them to generate an order. First add a form to the page that contains the Order entry control:


Next, add code to call the ActiveX component and retrieve the product list, which is done by creating an instance of this class by using the CreateObject function of the server object. Once a reference to the class instance is established, the code can retrieve the recordset and display the contents in a list box:


Now that you have an ASP page that captures user input, the rest of the work is to process the order. This passes through another called PlaceOrder. ASP's ASP page is complete. PlaceOrder. ASP is a transactional ASP page that invokes the PlaceOrder routines in an ActiveX DLL. The PlaceOrder routine updates the database by increasing the order records in the database and adjusting the product inventory quantity. Add the Placeorder.asp page below, set the transaction instruction at the top of the page, and add the on Transactioncommit and OnTransactionAbort routines:

Below, fill in the transaction routine code to tell the user the result of the order request. To do this, insert the Response.Write statement in the routine.


19.5.4 Program Validation
Verify this example. Add a set of orders to the system. Open SQL Server and observe the records in the orders datasheet, as shown in Figure 1 9-1 8.

To verify the transaction process, open the Component Services MMC plug-in again and select the Transaction statistics node under the Distributed Transaction Coordinator node in the requested computer. The transaction statistics monitor listens for D T C commit and terminate transactions, and displays the rollup results, as shown in Figure 1 9-1 9.

This chapter describes the concept of transaction processing and how to manipulate transactions in an IIS application by using Component Services (COM +) of Windows 2000.
Microsoft has provided developers with a powerful array of services that applications can use to create. These services, such as transaction processing (DTC), allow developers to focus on solving business problems without having to worry about low-level communication between services.




Related Article

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.