In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Transaction ACID property sequence number description 1 Atomicity atomicity means that the transaction is an indivisible unit of work, and either all or none of the operations in the transaction occur. 2 Consistency transactions must transform the database from one consistency state to another. (3) the isolation of Isolation transactions is that when multiple users visit the database concurrently, the transactions opened by the database for each user can not be disturbed by the operation data of other transactions, and multiple concurrent transactions should be isolated from each other. 4 Durability persistence means that once a transaction is committed, its change to the data in the database is permanent, and then even if the database fails, it should not have any effect on it.
Related methods of Connection operating transactions the serial number method functions 1void setAutoCommit (boolean autoCommit) to set whether the transaction commits automatically
If set to false, the transaction is committed manually. 2void commit () manually commit the transaction 3void rollback () rollback (when an exception occurs, all code that has been successfully executed needs to fall back to the state it was before the transaction started. ) 4Savepoint setSavepoint (String name) creates a SavePoint in the current transaction
1. Use transaction package com.rk.db.g_transaction;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import com.rk.db.utils.JDBCUtil;/** * / / transfer, use transaction * @ author RK * * / public class Demo01 {public static void main (String [] args) {Connection conn = null Try {conn = JDBCUtil.getConnection (); / / 1. Set the transaction to manually commit conn.setAutoCommit (false); boolean flag = true / indicates whether there is a SQL exception transferMoney (conn, 100, "Zhang San", "Li Si", flag);} catch (SQLException e) {System.out.println ("transfer failed!") ; try {/ / 2. If an exception occurs, you need to roll back the transaction conn.rollback (); System.out.println ("rollback operation succeeded!") ;} catch (SQLException ex) {ex.printStackTrace () }} finally {/ / 3, all operations were executed successfully, commit transaction try {conn.commit () System.out.println ("execution complete!") ;} catch (SQLException e) {e.printStackTrace ();} JDBCUtil.closeQuietly (conn) }} / * simulated bank transfer * @ param conn database connection * @ param moneyNum transfer amount * @ param userAdd received Money user * @ param userSub payout Money user * @ param flag whether to simulate a SQL Exception exception, true indicates that it appears False means that * @ throws SQLException * / private static void transferMoney (Connection conn, long moneyNum, String userAdd, String userSub, boolean flag) throws SQLException {PreparedStatement pstmtAdd = null does not appear. PreparedStatement pstmtSub = null; try {String sqlAddMoney = "update T_Bank set money=money+? Where username=? "; pstmtAdd = conn.prepareStatement (sqlAddMoney); pstmtAdd.setLong (1, moneyNum); pstmtAdd.setString (2, userAdd); pstmtAdd.executeUpdate () If (flag) {throw new SQLException ("Simulation SQL execution error");} String sqlSubMoney = "update T_Bank set money=money-?" Where username=? "; pstmtSub = conn.prepareStatement (sqlSubMoney); pstmtSub.setLong (1, moneyNum); pstmtSub.setString (2, userSub); pstmtSub.executeUpdate ();} finally {JDBCUtil.closeQuietly (pstmtAdd) JDBCUtil.closeQuietly (pstmtSub);}
2. Use the transaction to roll back to the specified code segment package com.rk.db.g_transaction;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Savepoint;import com.rk.db.utils.JDBCUtil / * / transfer, using transactions, rollback to the specified code snippet * @ author RK * * / public class Demo02 {public static void main (String [] args) {Connection conn = null; Savepoint sp = null; try {conn = JDBCUtil.getConnection () / 1. Set the transaction to manually commit conn.setAutoCommit (false); transferMoney (conn, 1000, "Li Si", "Zhang San", false) / / if failed, roll back to this location sp = conn.setSavepoint (); boolean flag = true / indicates whether there is a SQL exception transferMoney (conn, 500, "Zhang San", "Li Si", flag);} catch (SQLException e) {System.out.println ("transfer failed!") ; try {/ / 2. If an exception occurs, you need to roll back (rollback to the specified code snippet) conn.rollback (sp); System.out.println ("rollback to the specified location successfully!") ;} catch (SQLException ex) {ex.printStackTrace () }} finally {/ / 3, all operations were executed successfully, commit transaction try {conn.commit () System.out.println ("execution complete!") ;} catch (SQLException e) {e.printStackTrace ();} JDBCUtil.closeQuietly (conn) }} / * simulated bank transfer * @ param conn database connection * @ param moneyNum transfer amount * @ param userAdd received Money user * @ param userSub payout Money user * @ param flag whether to simulate a SQL Exception exception, true indicates that it appears False means that * @ throws SQLException * / private static void transferMoney (Connection conn, long moneyNum, String userAdd, String userSub, boolean flag) throws SQLException {PreparedStatement pstmtAdd = null does not appear. PreparedStatement pstmtSub = null; try {String sqlAddMoney = "update T_Bank set money=money+? Where username=? "; pstmtAdd = conn.prepareStatement (sqlAddMoney); pstmtAdd.setLong (1, moneyNum); pstmtAdd.setString (2, userAdd); pstmtAdd.executeUpdate () If (flag) {throw new SQLException ("Simulation SQL execution error");} String sqlSubMoney = "update T_Bank set money=money-?" Where username=? "; pstmtSub = conn.prepareStatement (sqlSubMoney); pstmtSub.setLong (1, moneyNum); pstmtSub.setString (2, userSub); pstmtSub.executeUpdate ();} finally {JDBCUtil.closeQuietly (pstmtAdd) JDBCUtil.closeQuietly (pstmtSub);}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.