Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Transaction processing of JDBC programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. the basic concept of transaction

The database is a shared resource used by multiple users. When multiple users access data concurrently, multiple users will access the same data at the same time in the database. If the concurrent operation is not controlled, incorrect data may be read and stored, and the consistency of the data may be destroyed.

Transaction is the basic unit of concurrency control. A transaction is a sequence of operations. Either all of these operations are performed or none are performed, and it is an indivisible unit of work. For example, bank transfers: deduct money from one account and collect money from another account, both operations are either performed or not performed, so they should be regarded as a transaction.

A transaction is the unit in which a database maintains data consistency, which can be maintained at the end of each transaction.

For programmers, database transactions can be regarded as a set of sql statements, this set of sql statements is a logical unit of work, they are inseparable, and their execution results should permanently modify the contents of a database as a whole or cancel changes to the database as a whole.

Second, the four basic characteristics of affairs

1. Atomicity

Atomicity means that the operations contained in a transaction are treated as a logical unit in which either all the operations succeed or all fail. This also means that all elements in the transaction are committed or rolled back as a whole. All elements of a transaction are inseparable and are a complete operation.

two。 Consistency

Consistency means that the database is in a consistent state before and after the transaction, and the integrity constraints of the database are not broken.

3. Isolation

Isolation means that multiple transactions that modify the database are isolated from each other. This shows that transactions must be independent and should not affect other transactions in any way.

4. Persistence

Persistence means that the impact on the database after the transaction is completed is permanent, and the change actually modifies the database and remains even if the database fails.

III. Transaction statements related to SQL

Start transaction: BEGIN TRANSACTION

Commit transaction: COMMIT TRANSATION

Rollback transaction: ROLLBACK TRANSATION

Package com.eduask.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement / * the logic to be tested to save the basic information of a user Tom and the address information of Tom is inserted into the user table and the address table by two java methods respectively. When inserting the address table, it artificially creates an abnormal observation result * / public class TransactionTest {/ / create a getConnection () to obtain the connection public static Connection getConnection () {Connection conn = null of the database. Try {Class.forName ("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/testdemo", "root", "root");} catch (Exception e) {e.printStackTrace () } return conn;} public static void insertUserData (Connection conn) throws SQLException {String sql = "insert into tbl_user (id,name,password,email)" + "values" Statement st = conn.createStatement (); int count = st.executeUpdate (sql); System.out.println ("insert + count +" records into the user ") } public static void insertAddressData (Connection conn) throws SQLException {String sql = "insert into tbl_address (id,city,country,user_id)" + "values"; Statement st = conn.createStatement () Int count = st.executeUpdate (sql); System.out.println (+ count + "records inserted into the address table");} public static void main (String [] args) {Connection conn = null; try {conn = getConnection () Conn.setAutoCommit (false); insertUserData (conn); insertAddressData (conn); conn.commit () } catch (SQLException e) {System.out.println ("= catch SQL exception ="); e.printStackTrace (); try {conn.rollback () / / rollback the transaction to ensure the consistency of the database data System.out.println ("= transaction rollback success =");} catch (Exception e2) {e2.printStackTrace () }} finally {try {if (conn! = null) {conn.close () }} catch (Exception e3) {e3.printStackTrace ();}

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report