What is a transaction? Simply put, it's what you do or do. Term representation refers to a program execution unit that accesses and possibly updates various data items in a database.
In a database, a transaction can be an SQL statement, a set of SQL statements, or an entire program.
A very classic example: transfer from Account A to B. This process needs to complete two steps: 1) subtract the money from the A account and 2) Add the appropriate amount to the B account. Quite simply, these two steps are indispensable. If it goes wrong, the consequences are disastrous. What do we do? This leads to the usefulness of the transaction.
The SQL statement is as follows, and when there is no error, the commit transaction was successfully transferred. If an error occurs, the rollback is performed. Database design is also simple, a money table, two fields Bankname (account name) and Totalmoney (total assets).
Begin Trybegin Tran --Start transaction update money set totalmoney=totalmoney-100 where bankname= ' A '--a account minus Update money set totalmoney=totalmoney+100 where bankname= ' B '--b account plus 100commit Tran --Commit transaction End Trybegin Catchrollback Tran --transaction rollback END catch
Transfer from A to B is also an example of a transfer.
The specific code is as follows:
private void Button1_Click (object sender, EventArgs e) {sqltransaction Sqltrans = null; SqlConnection con = new SqlConnection ("server=.; database=bank;uid=sa;pwd=123456; "); try {con. Open (); Start transaction Sqltrans = Con. BeginTransaction (); SqlCommand cmd = new SqlCommand ("", con, Sqltrans); Cmd.commandtimeout = 120; Cmd.commandtype = System.Data.CommandType.Text; String cuta = "Update money set [email protected] where bankname= ' A '"; String addb = "Update money set [email protected] where bankname= ' B '"; Assigning values to parameters SqlParameter paras = new SqlParameter ("@count", Txtmoney.text); Cmd. Parameters.Add (paras); Cmd.commandtext = Cuta; Cmd. ExecuteNonQuery (); Cmd.commandtext = ADDB; Cmd. ExecuteNonQuery (); throw new Exception ("Test exception.the transaction must rollback"); Used to test the transaction rollback//COMMIT transaction sqltrans.commit (); } catch (Exception ex) {//Transaction rollback sqltrans.rollback (); Console.Write (ex. Message); } finally {if (con. state! = System.Data.ConnectionState.Closed) con. Close (); } console.readline (); }
Now look at the situation when the transaction rolls back: Return the line of code that you have watched above back to normal and run again. Reduce a account money and increase the money of the B account, two operations are not executed, so even if the error, it will not cause more serious consequences, just to get the program back to the state before the execution.
By looking at the example above, we can understand the properties of the transaction, such as atomicity, consistency, isolation and persistence. These four properties are often called acid properties.
1) atomicity Atomic Nature . This means that the transaction is an inseparable unit of work, and all operations included in the transaction are either done or not. This is also at the heart of it.
2) Consistency consistency. The transaction must be to change the database from one consistency state to another. This is very similar to atomic nature.
3) Isolation isolation. Execution of one transaction cannot be disturbed by other transactions.
4) Durability durability . When a transaction is committed, its changes to the data in the database should be permanent. If the transfer is successful, the data is updated to the database.
Sentiment: business, this noun has been heard very early. It is difficult to understand that it is to practice it now. So that a similar content is always around the walk away, also missed a lot of learning opportunities. The sooner you know, the more opportunities for hands-on practice.
Look at the database again--(4) transaction