3
Transaction management in JDBC int fundtransfer(source account no.,destination account no.,amount); { b1=getBal(destination_account_no); b2=getBal(source_account_no); b2=b2-amount; b1=b1+amount; s1=update account set bal=’b1’,where accont_no=’destination _account_no’. s2=update account set bal=’b2’ where account-no=’source_account_no’; execute(s1); Y execute(s2); X Account } In the above scenario we are doing two database operation Updating account table one record and another is also updating account table another record. we have to assume fund transfer operation is completed successfully only when these two operation are successfully but we can get the problem in the above scenario some times. Assume that 1 st update statement is executed successfully and due to some region (Network failure ,gateway failure, database server busy, database server crashed ) second operation is failed . This gives you the inconsistent result . Region for problem:---- By default auto commit is true with all the database collections. i.e. with one connection .if you submit one query to the database engine there will be executed and immediately commit will be issued, with one connection when you submit multiple query the commit will be issue multiple times. Solution :--- To solve the above discuss problem you can use Transaction. Transaction is nothing but performing multiple database operation has one unit with all –nothing / all or nothing preposition. when all the operations . in the transaction (unit)are successful then transaction is successfully and any one operation in the unit is failed then transaction is failed. When the transaction is successfully you have to issue commit and when the transaction is failed you have to issue Rollback when you manage the transaction properly ,transaction guarantee ACID property A Atomicity C Consistency I Isolation D Durability Type of transaction :-------- Based on the number of resources involving in the transaction you can divide the transaction into two parts:---------- Local Transaction Distributed Transaction Mysql Acc_no acc_type acc_name bal 11 SA A 10k 12 SA

02_New Microsoft Word Document

  • Upload
    phani

  • View
    215

  • Download
    1

Embed Size (px)

DESCRIPTION

klm

Citation preview

Transaction management inJDBCint fundtransfer(source account no.,destination account no.,amount);{b1=getBal(destination_account_no);b2=getBal(source_account_no);b2=b2-amount;b1=b1+amount;s1=update account set bal=b1,!"ere accont_no=destination _account_no.s2=update account set bal=b2 !"ere account-no=source_account_no;e#ecute(s1);Ye#ecute(s2);X $ccount %&n t"e abo'e scenario !e are doing t!o database operation (pdating account table one record and anot"er is alsoupdating account table anot"er record. !e "a'e to assume fund transfer operation is completed successfull) onl)!"en t"ese t!o operation are successfull) but !e can get t"e problem in t"e abo'e scenario some times.$ssumet"at 1stupdatestatement ise#ecutedsuccessfull)andduetosomeregion(*et!or+failure,gate!a)failure, database ser'er bus), database ser'er cras"ed ) second operation is failed . ,"is gi'es )ou t"e inconsistentresult .Region for problem----- B) default auto commit is true !it" all t"e database collections. i.e. !it" one connection.if )ou submit one .uer) to t"e database engine t"ere !ill be e#ecuted and immediatel) commit !ill be issued,!it"one connection !"en )ou submit multiple .uer) t"e commit !ill be issue multiple times. Solution ---- ,o sol'e t"e abo'e discuss problem )ou can use ,ransaction. ,ransaction is not"ing but performingmultiple databaseoperation "as one unit !it" all /not"ing 0 all or not"ing preposition. !"en all t"e operations . int"e transaction (unit)are successful t"en transaction is successfull) and an) one operation in t"e unit is failedt"en transaction is failed.1"en t"e transaction is successfull) )ou "a'e to issue commit and !"en t"e transaction is failed )ou "a'e toissue 2ollbac+ !"en )ou manage t"e transaction properl) ,transaction guarantee $3&4 propert)$ $tomicit)33onsistenc)& &solation4 4urabilit) Type of transaction --------- Based on t"e number of resources in'ol'ing in t"e transaction )ou can di'ide t"e transaction into t!o parts----------- 5ocal ,ransaction 4istributed ,ransactionLocal Transaction -------- !"en onl) one resource is in'ol'ing in t"e transaction t"en it is called as local transaction.NOTE ---- resource can be a database or database_connection. 64B3 support local ,ransaction.Distributed Transaction ------ !"en multiple resources are in'ol'ing in t"e transaction t"en it is called distributed transaction .64B3 dont support 4istributed ,ransaction .You can manage the transaction with one of the following technology and framework------ 64B3 7ibernate 86BLocal Transaction 9pring:ou can manage t"e 4istributed ,ransaction !it" t"e "elp of 86B and 9pring.D.T 4.,D.T 4.,Mysl $cc_no acc_t)pe acc_name bal 11 9$ $ 1;+12 9$B