A good memory is better than a bad pen. 24-java Processing database transactions (2)-Dirty data

Source: Internet
Author: User


read " dirty " Data refers to a transaction t1 Modifies a data and writes it back to disk, transaction t2 After reading the same data, t1 t1 t2 The data that is read is inconsistent with the data in the database, t2 The data read is " dirty " data, that is, incorrect data.

Dirty data is very common in more complex, interactive systems.

1. Preparation of database transactions with Java

to have an app that can access the database. The following examples are based on Oracle .

CREATE TABLE Ffm_account (

ID int PRIMARY KEY,

Name varchar (32),

Money int

);

Test data:

Insert into Ffm_account (Id,name,money) VALUES (1, ' A ', 1000);

Insert into Ffm_account (Id,name,money) VALUES (2, ' B ', 1000);

2. JDBC using transactions in

When the JDBC program obtains a connection object to the database, by default the Connection object submits the SQL statement sent on it to the database automatically. If you want to turn off this default submission method and have multiple SQL execute in a single transaction, use the following JDBC control transaction statements

Connection.setautocommit (FALSE);// open Transaction (start transaction)

connection.rollback ();// rolling back a transaction (rollback)

connection.commit ();// COMMIT TRANSACTION (commit)

3. JDBC dirty data Using transaction examples and source code for reading dirty data

in the JDBC code to demonstrate the bank transfer case, there are two bank accounts, A and B, each with 1000 dollars, a to the C account transfer 100, and then to read a account of the money, read a account only 900, but the C account does not exist, then the money should be a transfer failure.

JAVA Source code:

Package com.transaction;

Import java.sql.Connection;

Import java.sql.PreparedStatement;

Import Java.sql.ResultSet;

Import com.db.EasyC3p0;

/**

 * There are two bank accounts, a and B, each with a total of 1000 dollars;

*a Transfer 100 dollars to the C account, then read the money in account A, read a account of only 900 dollars,

 * but the C account does not exist, then the money should be the transfer failure.

*

* @author Fan Fangming

*/

public class Easydirtydata {

publicstatic void Main (string[] args) {

Connectionconn = null;

preparedstatementstmt = null;

Resultsetrs = null;

try{

Conn =easyc3p0.getconnection ();

       // notifies the database to open a transaction (start transaction)

Conn.setautocommit (FALSE);

String Sqlallmoney = "Select sum (Money) as money from Ffm_account";

String Sqla_money = "Select money from Ffm_account";

stmt = Conn.preparestatement (Sqlallmoney);

rs = Stmt.executequery ();

if (Rs.next ()) {

System.out.println (" the total amount of the system before the transfer is:" +rs.getint ("money"));

}

stmt = Conn.preparestatement (Sqla_money);

rs = Stmt.executequery ();

if (Rs.next ()) {

System.out.println (" The amount of a before the transfer is:" + rs.getint ("money"));

}

       // simple analog A to C account transfer:

String Sqla = "Update ffm_account set money=money-100 wherename= ' A '";

stmt = Conn.preparestatement (Sqla);

Stmt.executeupdate ();

       // There is no C account in the system

String SQLC = "Update ffm_account set money=money+100 wherename= ' C '";

stmt = Conn.preparestatement (SQLC);

Stmt.executeupdate ();

Conn.commit ();

       // simple analog a toward C account end

       // after the transfer is over, look at the account status

stmt = Conn.preparestatement (Sqlallmoney);

rs = Stmt.executequery ();

if (Rs.next ()) {

System.out.println (" the total amount in the system after the execution of the transfer is:" +rs.getint ("money"));

}

stmt = Conn.preparestatement (Sqla_money);

rs = Stmt.executequery ();

if (Rs.next ()) {

System.out.println (" after the transfer is executed, the amount of a is:" + rs.getint ("money");

}

}catch (Exception e) {

E.printstacktrace ();

}finally{

Easyc3p0.close (conn, stmt, RS);

}

}

}

4. Run Results

The total amount in the system before the transfer is: 2000

before the transfer is executed, the amount of a is: 1000

After the transfer is executed, the total amount in the system is: 1900

after the transfer is executed, the amount of a is: 900

the money in the system went, a: My money, pay me back!!!!!!!!!

A good memory is better than a bad pen. 24-java Processing database transactions (2)-Dirty data

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.