JDBC transaction operations for databases

zhaozj2021-02-16  51

JDBC transaction operations for databases

Ginkou.fly 2002-9-4

1. Overview:

In JDBC database operation, a transaction is an indivisible working unit consisting of one or multiple expressions. We end the operations of the transaction by submitting commit () or rollback rollback (). The methods for transaction operation are located in the interface java.sql.connection.

2. Features: ★ In JDBC, transaction operation is automatically submitted. That is, an update expression for the database represents a transaction operation. After the operation is successful, the system will automatically call commit () to submit, otherwise rollback () will be called back and rollback. ★ In JDBC, automatic submission can be prohibited by calling setAutocommit (false). After that, the expression of multiple database operations can be used as a transaction. When the operation is completed, it will be submitted for the overall commit. If one of the expression operations fails, it will not be executed (), and the response will be generated. Abnormal; the rollback () can be called when the abnormal capture is captured. Doing so after maintaining multiple updates, the consistency of related data, examples are as follows:

Try {

CONN =

Drivermanager.getConnection

("JDBC: Oracle: Thin: @Host: 1521: SID", "Username", "UserPwd";

Conn.setAutocommit (false); // Prohibits automatic submission, set back point

STMT = conn.createstatement ();

Stmt.executeUpdate ("ALTER TABLE ..."); // Database Update Operation 1

Stmt.executeUpdate ("INSERT INTO TABLE ..."); // Database Update Operation 2

Conn.commit (); // Transaction Submit

} catch (exception ex) {

EX.PrintStackTrace ();

Try {

CONN. ROLLBACK (); // Roll back

} catch (exception e) {

E.PrintStackTrace ();

}

}

★ JDBC API supports the lock of the database, and provides five operational support, 2 lock density.

5 kinds of support:

Static int transaction_none = 0;

→ Prohibit transaction and lock.

Static int Transaction_read_uncommitted = 1;

→ Allow dirty reads, repeated reading and writing (Repeatable Reads) and image reading (PHNTOM)

Reads

Static int transaction_read_committed = 2;

→ Prohibit Dirty Reads, allowing Repeatable Reads and Image Reading (PHNTOM Reads)

Static int transaction_repeatable_read = 4;

→ Prohibit Dirty Reads and Repeatable Reads, allowing image reading and writing (Phntom Reads)

Static int Transaction_serializable = 8;

→ Prohibit Dirty Reads, Repeatable Reads and 2 Density of PHNTOM Reads:

The last item is locked, and the remaining 3 to 4 items are locked.

Dirty reads: When a transaction modifies the value of a data row, another transaction reads this row value. If the previous transaction rolls back, the latter service will get an invalid value (dirty data).

Repeatable Reads: When a transaction is read, another transaction is also modifying this data line. The previous transaction will get an inconsistent value when it is repeatedly read this line.

Image read and write (Phantomread): When a transaction is in a table in a table, another transaction is inserted into the data line that satisfies the query condition. The previous transaction will get an additional "image" value when repeatedly read the value of the condition.

JDBC sets transaction support and its lockout according to the default value provided by the database, of course, can also be manually set:

SetTransactionISolation (Transaction_Read_UNCommitted);

You can view the current settings of the database:

GetTransactioniSolation ()

It should be noted that the database and its drivers must support the corresponding transaction operations when performing the tripod setting.

As the value increases, the independence of its transaction is increased, and it is more effective to prevent conflicts between transaction operations; and also increase the overhead of the lock, reducing the concurrency between users accessing the database, the operation of the program Efficiency is also reduced. Therefore, it is necessary to balance the conflict between operation efficiency and data consistency. In general, it can be used for transaction_read_uncommitted methods for only the database, which can be used; for data queries than the updated operation, it can be used to use transaction_read_committed mode; TRANSAction_REPEATABLE_READ; in data consistency requirements Consider the last item again, because the table lock is involved, there is a large impact on the operation efficiency of the program.

In addition, the default value of the database driver on the Oracle is transaction_none, ie the transaction operation is not supported, so it is necessary to manually set in the program.

3. Small knot

JDBC provides support for database transaction operations, which is relatively complete, enhances the running efficiency of the program by transaction operation, maintaining data consistency.

转载请注明原文地址:https://www.9cbs.com/read-28164.html

New Post(0)