Details of Java transactions and simple application examples

  • 2020-05-19 04:54:29
  • OfStack

Simple use of Java transactions

Java transactions are asked in some interviews.

In the interview, the first thing we should answer is that transaction can guarantee the data integrity and uniqueness.

If you're a bit more skilled: just say something about the principles (don't submit tasks before you start, submit them after they're all done,
If the task is disconnected in the middle, roll it back and undo the previous task), as an example of a simple 1 point (such as the problem of saving and withdrawing money).
For example, when the bank transfers 1,000 yuan from the A account to the B account, the system first reduces 1,000 yuan from the A account, and then increases 1,000 yuan for the B account. If all are executed successfully, the database is at 1 tropism; If only the amount of the A account has been modified without increasing the amount of the B account, then the database will be in a non-1 state and the previous operation will need to be cancelled.

This article takes a brief look at java transactions. When it comes to java transactions, we need to know that this is related to the database.

1. Let's look at a simple piece of code

Transactions are processed in JDBC mode

public int delete(int sID) { 
  // The class that implements the database connection 
  DataBaseConnection dbc = new DataBaseConnection(); 
  // Get the connection object 
  Connection con = dbc.getConnection(); 
  try { 
    con.setAutoCommit(false);//  To change the JDBC The default commit method for transactions  
    dbc.executeUpdate("delete from xiao where ID=" + sID); 
    dbc.executeUpdate("delete from xiao_content where ID=" + sID); 
    dbc.executeUpdate("delete from xiao_affix where bylawid=" + sID); 
    con.commit();// submit JDBC The transaction  
    con.setAutoCommit(true);//  restore JDBC The default commit method for transactions  
    return 1; 
  catch (Exception exc) { 
    con.rollBack();// The rollback JDBC The transaction  
    return -1; 

The code above is the execution of a relatively simple java transaction.
If the above three delete operations fail once, the task will be rolled back, which means that either 1 operation succeeds or nothing is done.

If there is no transaction management, the previous execution will be immediately updated in the database,
Where the execution fails, exit and no longer execute the following tasks, can not guarantee the data 1.

2. Basic concepts of Java transactions

Atomicity (Atomicity) : a transaction is a complete operation. Each step of the transaction is indivisible (atomic);
Either all of them or none of them
1 uniqueness (Consistency) : the data must be 1 uniqueness when the transaction is completed
Isolation (Isolation) : all concurrent transactions that modify data are isolated from each other, indicating that the transaction must be independent,

It should not depend on or affect other transactions in any way
Permanence (Durability) : after the transaction is completed, its changes to the database are permanently held, and the transaction log can keep the transaction permanent

Transaction description of java: if you perform multiple operations on the database, each execution or step is one transaction.
If the database operation is not performed at one step or an exception occurs and the transaction fails, then some transactions are executed and some are not executed,
Thus, there is a rollback of the transaction, canceling the previous operation...

In a database operation, a transaction is an indivisible unit of work consisting of one or more sql statements updating the database.
The entire transaction can only be committed to the database if all the operations in the transaction are completed normally. If one operation is not completed,
You have to undo the whole transaction.

For example, in the bank transfer transaction, suppose zhang 3 transfers 1000 yuan from his own account to li 4's account. The relevant sql statement is as follows:

update account set monery=monery-1000 where name='zhangsan'

update account set monery=monery+1000 where name='lisi'

The two statements must be treated as one completed transaction. Only if both are executed successfully can the transaction be committed.
If one sentence fails, the entire transaction must be undone.

Three methods to control transactions are provided in the connection class:
(1) setAutoCommit(Boolean autoCommit): sets whether the transaction is automatically committed;
(2) commit (); Commit transactions;
(3) rollback (); Undo transactions;

In jdbc api, the default is auto-commit transactions, that is, each sql statement that updates the database represents one transaction,
Once the operation is successful, commit () is automatically called to commit, otherwise rollback () is called to undo the transaction.

In jdbc api, you can disable auto-commit transactions by calling setAutoCommit(false).
You can then treat multiple sql statements that update the database as one transaction, and after all the operations are completed, call commit () for the overall commit.
If one of the sql operations fails, the commit () method is not executed, but the corresponding sqlException is generated,
At this point, you can catch the exception code block and invoke the rollback () method to undo the transaction.

1 generally speaking, a developer who specializes in developing databases must have a deep understanding of transactions,
But one-like programmers don't have to spend much time on this. Just have an understanding of the general effect.

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

Related articles: