cool hit counter Java + Oracle implementation of transactions - JDBC transactions_Intefrankly

Java + Oracle implementation of transactions - JDBC transactions


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/46675171

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

JDBC transactions are controlled by the Connection object, which provides two transaction modes: auto-commit and manual commit, with auto-commit being the default.

Auto-commit is just that: in JDBC, in a Connection object Connection, each SQL statement execution is treated as a transaction by default (i.e., the operation is updated to the database immediately after each SQL statement execution).

A manual commit is just that: when multiple SQL statements need to be executed at once to form a transaction out of multiple SQL statements (i.e., either all succeed or roll back all operations), you have to commit manually.

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 transaction mode to manual commit transaction.
                conn.setAutoCommit(false);
				
                 // Set the isolation level of the transaction.
                conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
                
 		// Perform an insert operation
                String sqlInsert = "insert into t_user(userName,userPassword) values(?,?)";               
                pstmt = conn.prepareStatement(sqlInsert);
                pstmt.setString(1, "danny");
                pstmt.setString(2, "123");
                pstmt.executeUpdate();
				
 		// Perform modification operations
                String sqlUpdate = "update t_user set userPassword=? where userName = ?";
                pstmt = conn.prepareStatement(sqlUpdate);
                pstmt.setString(1, "123456");
                pstmt.setString(2, "danny");               
                pstmt.executeUpdate();
				
                 //Submitted transactions
                conn.commit();
				
            } catch (Exception e) {                
                 // Roll back the transaction if an exception occurs
                conn.rollback();
            }finally{                
                if (pstmt!=null) {
                    pstmt.close();
                }
                if (conn!=null) {
                    conn.close();
                }
            }
}
}

When two operations, insert and update, need to be executed simultaneously, either both operations succeed or, if one fails, all operations are undone.

At a macro level, the following four steps are required.

1. To set the transaction mode to manual commit transactions.

conn.setAutoCommit(false);

2. Set the isolation level of the transaction.

conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

3. Submission services.

conn.commit();

4. If an exception occurs, roll back the transaction.

conn.rollback();

From the above, JDBC transactions are easier to use, but because it is controlled by the Connection object, it has the disadvantage that the scope of the transaction is limited to a single database connection, and multiple databases cannot be manipulated in the same transaction.


Recommended>>
1、That experience with Apache and Mysql in xampp
2、Redishash type operations
3、Interview Share 2018 Alibaba Front End Interview Summary Questions Answers
4、Handbook for Lesson 9 of Dumb Ways to Learn Python
5、A brief tutorial on SED commands

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号