Java + Oracle implement transactions-JDBC transactions, oraclejdbc

Source: Internet
Author: User

Java + Oracle implement transactions-JDBC transactions, oraclejdbc

J2EE supports JDBC transactions, JTA transactions, and container transaction. Here we will explain how to implement JDBC transactions.


A jdbc transaction is controlled by a Connection object. It provides two transaction modes: Automatic commit and manual commit. The default mode is automatic commit.

Automatic submission is: in JDBC, in a Connection object Connection, by default, each SQL statement is executed as a transaction (that is, the operation is immediately updated to the database after each SQL statement is executed ).

Manual submission is: when you need to execute multiple SQL statements at a time and combine multiple SQL statements into one transaction (that is, either all succeeded or all operations rolled back, you have to manually submit it.


Let's look at an example:

Import java. SQL. *; public class TransactionTest {public static void main (String [] args) throws SQLException {try {Class. forName ("oracle. jdbc. driver. oracleDriver "); Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "TransactionTest", "123456"); // set the transaction mode to manual commit transaction: conn. setAutoCommit (false); // sets the transaction isolation level. Conn. setTransactionIsolation (Connection. TRANSACTION_REPEATABLE_READ); // execute the insert operation String sqlInsert = "insert into t_user (userName, userPassword) values (?,?) "; Pstmt = conn. prepareStatement (sqlInsert); pstmt. setString (1, "danny"); pstmt. setString (2, "123"); pstmt.exe cuteUpdate (); // execute the modify operation String sqlUpdate = "update t_user set userPassword =? Where userName =? "; Pstmt = conn. prepareStatement (sqlUpdate); pstmt. setString (1, "123456"); pstmt. setString (2, "danny"); pstmt.exe cuteUpdate (); // submit the transaction conn. commit ();} catch (Exception e) {// roll back the transaction conn if a transaction Exception occurs. rollback ();} finally {if (pstmt! = Null) {pstmt. close ();} if (conn! = Null) {conn. close ();}}}}

When you need to insert and update the two operations at the same time, either of the two operations is successful. If one operation fails, all operations are revoked.


The following four steps are required:

1. Set the transaction mode to manual transaction commit:

Conn. setAutoCommit (false );

2. Set the transaction isolation level:

Conn. setTransactionIsolation (Connection. TRANSACTION_READ_COMMITTED );

3. Submit the transaction:

Conn. commit ();

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

Conn. rollback ();


From the above perspective, JDBC transactions are easy to use, but because they are controlled by the Connection object, its disadvantage is that the transaction scope is limited to only one database Connection, the same transaction cannot operate on multiple databases.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.