官术网_书友最值得收藏!

Transactions

A transaction is a group of one or more statements run as a unit. If the default value of auto-commit is set to true, then each Statement that would be run represents a transaction. After each statement is run, changes to the database are made with the auto-commit set to true. Set the auto-commit to false, if a developer requires a group of statements to be run together. Changes to the database are not made till each of the statement has run. If auto-commit is set to false, the changes to the database are committed with the commit() method. The commit() method commits the SQL statements run after the previous commit to the database was made. The group of statements run between two consecutive commits to the database represents a transaction. The rollback() method rolls back the changes made in the current transaction. A transaction may be required to be rolled back, if an error or a SQLException is generated.

connection.rollback();

While one transaction is modifying a database table, another transaction could be reading from the same table. The type of read can be dirty-read, a non-repeatable read, or a phantom read. A dirty-read occurs when a row has been modified by a transaction, but has not been committed, and is being read by a different transaction. If the transaction that modifies the row rolls back the transaction, then the value retrieved by the second transaction would be erroneous. A non-repeatable transaction occurs when one transaction reads a row while the other transaction modifies it. The first transaction re-reads the row obtaining a different value. A phantom read occurs when one transaction retrieves a result set with a WHERE condition, while the other transaction adds a row that meets the WHERE condition. The first transaction re-runs to generate a result set that has an additional row. The default transaction level can be obtained with the getTransactionLevel() method:

int transactionLevel=connection. getTransactionIsolation();

The different transaction isolation levels are listed in following table:

The transaction isolation level can be set with the setTransactionIsolation(int level) method:

connection.setTransactionIsolation(level);
主站蜘蛛池模板: 连州市| 武威市| 娱乐| 东平县| 嘉峪关市| 那坡县| 潼关县| 鹤岗市| 绿春县| 安康市| 霍山县| 都匀市| 姚安县| 神农架林区| 商洛市| 师宗县| 花莲市| 青河县| 巧家县| 伊川县| 曲水县| 正阳县| 永春县| 旌德县| 北川| 日喀则市| 珠海市| 鄂伦春自治旗| 洞口县| 淮北市| 襄垣县| 马山县| 温州市| 鄂伦春自治旗| 米林县| 汉寿县| 星座| 黎川县| 吉木萨尔县| 中方县| 兰州市|