Java Master/Slave table transaction control (EJB3.0 solution)

Source: Internet
Author: User

/*************************************** ******************************/
Master/Slave table transaction control solution EJB3.0
Author: chinayaosir
QQ: 44633197 QQ Group: 8284865,000048
Blog http://blog.csdn.net/chinayaosir

0. Transaction Consistency Control explanation:
Bank Card transfer operation transactions:
The user transfers 1111222333444 from Michael Jacob's account 8000.00 to Michael Lee's account 2222333344445555!
The procedure is as follows:
0.1 verify that John's password and account are correct!
0.2 verify that account 2222333344445555 entered correctly!
0.3 Zhang San, enter the transfer amount of 8000.00!
0.4 calculation service fee 4.00
0.5 verify Zhang San account balance> = 8000.00 + 4.00

0.6 Li Si account 2222333344445555 plus 8000.00
0.7 Zhang San account 1111222333444 minus 8004.00

0.8 transaction control
If there are no errors in steps 2 from 0.6 to 0.7, the transfer is successful [Submit the transaction]!
If there are any errors in steps 2 from 0.6 to 0.7, the transfer fails to [roll back the transaction]!
/*************************************** ******************************/
1. master table and slave table transaction control requirements

Assume that the primary table has only one row of data, and the table has one to n rows of Data!
When we save or delete the master table and slave table together, the transaction requirements are as follows:
1.1.Create/Update transaction requirements
If the primary table "Create/Update" fails and the table "Create/Update" is successful;
Or the master table "Create/Update" fails while the table "Create/Update" fails.
The entire transaction is rolled back. The result is that the Master/Slave table fails to be saved, meeting the transaction control requirements.
For example, to complete A task, you must go through A-> B-> C-> D-> E-> F in six steps,
If one error occurs in step 6, the parts that are created or updated are forcibly canceled by the transaction rollback;
If no error occurs in step 6, mCreate/Update is successful, and the transaction is committed!

1.2.delete transaction requirements
If the primary table fails to be deleted and the table is deleted successfully;
Or the master table is deleted successfully and the slave table fails to be deleted.
The entire transaction is rolled back. The result is that the Master/Slave table delete fails to meet the transaction control requirements.

/*************************************** ******************************/
2. master table and slave table transaction control EJB client call Method
2.1 Create/update call: spdaotrans. SaveWithTransation (sphead, splist );
Sphead = master table, 1 row record (up to one record can be entered)
Splist = slave table, two rows of records (1-N records can be input, N belongs to the natural number)

2.2Delete call: spdaotrans. DeleteWithTransation ("PB100563 ");
"PB100563" = a key word PK used by both the master and slave tables,
The DeleteWithTransation () method consists of 1-2-3 steps to delete data to form an overall transaction!

2.3 source code reference
Package com;
// Import java basic package
Import java. util .*;
Import java. lang. String;
Import java. text. SimpleDateFormat;
Import javax. naming. InitialContext;

// Import entity and service package
Import shipping. entity .*;
Import shipping. shippingDAO. spDAOTransRemote;

Public class testTransation {
Public static void main (String [] args ){
************* ***********************
Try {
// 1. InitialContext and get spDAOTrans object
InitialContext ctx = JNDIContext. getInitContextByRemote ();
SpDAOTransRemote spdaotrans = (spDAOTransRemote) ctx. lookup ("spDAOTrans/remote ");
//************************************** **********************
// 2. Save with transation test value = OK
// SaveWithTransation (SSpMaster m, List <SSpDetail> d)
System. out. println ("SaveWithTransation (SSpMaster m, List <SSpDetail> d )");
System. out. println ("fill SSpMaster data ");
/// 2.1 fill SSpMaster
SSpMaster sphead = new SSpMaster ();
SSpMasterId spheadpk = new SSpMasterId ();
Spheadpk. setPsonumber ("PB100563 ");
Spheadpk. setSubcustom ("0000000000 ");
String s1 = "10/29/2010", s2 = "10/25/2010", s3 = "10/30/2010 ";
SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy ");
Date d1 = sdf. parse (s1 );
Date d2 = sdf. parse (s2 );
Date d3 = sdf. parse (s3 );

Sphead. setId (spheadpk );
Sphead. setPshipDate (d1 );
Sphead. setShippDate (d2 );
Sphead. setShip2Date (d3 );
Sphead. setCustomerid ("B. A. B .");
Sphead. setDestinatio ("COLUMBUS, OHIO ");
Sphead. setCustofPoe ("long beach ");
Sphead. setId (spheadpk );

/// 2.2 fill SSpDetail list
System. out. println ("fill SSpDetail data ");

SSpDetailId spdetailpk1 = new SSpDetailId ();
SSpDetail spdetail1 = new SSpDetail ();
SSpDetailId spdetailpk2 = new SSpDetailId ();
SSpDetail spdetail2 = new SSpDetail ();
ArrayList <SSpDetail> splist = new ArrayList <SSpDetail> ();

String sdate = "10/25/2010 ";
SimpleDateFormat sdfDetail = new SimpleDateFormat ("MM/dd/yyyy ");
Date d_shipdate = sdfDetail. parse (sdate );
/// 2.3 fill spdetailpk data
Spdetailpk1.setShippDate (d_shipdate );
Spdetailpk1.setPsonumber ("PB100563 ");
Spdetailpk1.setOcNumber ("B1000577 ");
Spdetailpk1.setItemnumber ("BY34317 ");

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.