Day two: Shopping Cart 2.0-Transaction management

Source: Internet
Author: User

Transaction Management Core:

When you need to change several database tables at the same time, such as shopping cart Click Submit Order Need to change: Order Details table, order tracking form, order Information table. If you want to change the words, you need to change the three tables, or change them at the same time. When one table change fails, the other two tables cannot be changed.

To do this, you need to have a Workmanager class, and change the three tables, and you need to basedao the conn in JDBC from Workmanager, and you need to make sure that the same connection is used for three different sub-DAO. Then change the connection submission method to manual commit, when the three child DAO are done, and then manually commit the changes with connection so that the same connection is used.

Pubulic class WorkManager () {

Each element in the list<goodsdetails> car here is the source of the different order details, because an order may have multiple order details. So incoming car

public void Orderwork (OrderInfo or,list<goodsdetails> car,orderzz ordzz) {

The use of this basedao.url,basedao.username,basedao.userpwd here is to ensure that the Basedao has been initialized and that the driver has been loaded

Connection conn = drivermanager.getconnection (BASEDAO.URL,BASEDAO.USERNAME,BASEDAO.USERPWD);

try{

Record the way the original connection was submitted and change back to its previous state after you have finished managing things

Boolean flag = Conn.getautocommit ();

Change the way Conn is submitted for manual submission

Conn.setautocommit (flase);

New Orderinfodao (conn). InsertOrder (ORD);

New Orderzzdao (conn). Insertorderzz (ORDZZ);

Use the elements inside the car to generate order details and invoke Subdao to manipulate the database

for (int i=0;i<car.size (); i++) {

String g_id = Car.get (i). getgs.getgood_id;

String count = Car.get (i). GetCount;

Invoking the tool class to produce ORDERMXID

String ordermx_id = new Createid.createordermxid;

String order_id = Ord.getorderid;

Ordermx ordmx = new Ordermx (ordermx_id,order_id,g_id,count);

New Ordermxdao (conn). INSERTORDERMX (ORDMX);

}

Connection Manual Submission

Conn.commit ();

Change to original submission method

Conn.setcommit (flag);

}catch (Exception e) {

try{

When the error is fetched, rollback the operation to ensure that all databases are not changed

Conn.rollback;

}catch{

Rollback failure

}

}

}

}

Note: 1, you need to change the constructor in the JDBC Basedao. Add a parameter constructor public Basedao (Connection conn) {this.conn = conn};

and generates Conn in the parameterless constructor. To ensure that the constructor, whether it is called with or without parameters, can be used normally

2, the resource is closed, the original Basedao each time the operation is completed, the resource is closed, but in the matter management, the resource needs to be closed before all changes to the database need to be completed.

Review: JDBC steps:

1. Load driver:

Class.forName ("Com.microsoft.sqlsever.jdbc.SQLServerDriver");

2. Connect to the database:

Connection conn = drivermanager.getconnection ("jdbc:sqlserver://localhost:1433;databasename=shopping", "sa", "123") ;

3. Create the command execution object:

Statement stmt = Conn.createstatement ();

4. Execution of commands

Resultset rs = stmt.excutequery (SQL);

Stmt.excuteupdate (SQL);

5. Releasing Resources

Closeres ();

    

Day two: Shopping Cart 2.0-Transaction management

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.