Detailed explanation of Java and Oracle implementation transaction (JDBC transaction) examples

Detailed explanation of Java and Oracle implementation transaction (JDBC transaction) examples

J2EE supports JDBC transactions, JTA transactions and container transactions. Here is how to implement JDBC transactions.

JDBC transactions are controlled by the connection object. It provides two transaction modes: self active commit and manual commit. The default is self active commit.

Self active submission is: in JDBC. In a connection object connection. By default, the operation of each SQL statement is regarded as a transaction (that is, the operation will be updated to the database immediately after each SQL statement is run).

Manual submission means that when you need to run multiple SQL statements at one time and form multiple SQL statements into a transaction (that is, either all operations are successful or all operations are rolled back), you have to submit manually.

See an example:

When it is necessary to insert and update these two operations at the same time, either both operations succeed, assuming that one fails. Then undo all operations.

From a macro point of view, for example, the following four steps are required:

1. Set transaction mode to manual commit transaction:

conn.setAutoCommit(false);

2. Set the isolation level of transactions:

conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

3. Commit transaction:

conn.commit();

4. Assuming an exception occurs, roll back the transaction:

conn.rollback();

From the above, JDBC transaction is more convenient to use, but because it is controlled by the connection object, its disadvantage is that the scope of transaction is only limited to the connection of one database, and multiple databases cannot be operated in the same transaction.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>