In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What is a SpringBoot transaction? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
Important concept
Auto-submit mode
For mysql databases, the database is in auto-commit mode by default. Each statement is in a separate transaction, and when the statement is executed, the transaction is implicitly committed if it succeeds, and the transaction is implicitly rolled back if it fails. For normal transaction management, a set of related operations is in a transaction, so the autocommit mode of the database must be turned off, as shown below:
Check whether the auto-commit command is enabled (ON means to enable auto-commit, a value of 1 direction off means to disable auto-commit, and a value of 0):
Show variables like 'autocommit'
When autocommit is turned off, the user remains in a transaction until a commit commit or rollback statement is executed to end the current transaction and restart a new transaction.
DataSource connection = masterDataSource.getConnection (); connection.setAutoCommit (false)
Transaction isolation level
The isolation level refers to the degree of isolation between several concurrent transactions. Five constants representing the isolation level are defined in the TransactionDefinition interface:
1. TransactionDefinition.ISOLATION_DEFAULT: this is the default value, which means that the default isolation level of the underlying database is used. For most databases, this value is usually TransactionDefinition.ISOLATION_READ_COMMITTED.
2. TransactionDefinition.ISOLATION_READ_UNCOMMITTED: this isolation level indicates that one transaction can read data modified by another transaction that has not yet been committed. This level does not prevent dirty, non-repeatable and phantom reads, so the isolation level is rarely used. For example, PostgreSQL does not actually have this level.
3. TransactionDefinition.ISOLATION_READ_COMMITTED: this isolation level means that one transaction can only read data that another transaction has committed. This level prevents dirty reading, which is the recommended value in most cases.
4. TransactionDefinition.ISOLATION_REPEATABLE_READ: this isolation level means that a transaction can execute a query repeatedly throughout the process, and the records returned each time are the same. This level prevents dirty reading and non-repeatable reading.
5. TransactionDefinition.ISOLATION_SERIALIZABLE: all transactions are executed one by one, so that interference between transactions is completely impossible, that is, this level prevents dirty reading, unrepeatable reading, and phantom reading. But this will seriously affect the performance of the program. This level is not usually used.
Transaction propagation behavior
The so-called transaction propagation behavior means that if a transaction context already exists before the start of the current transaction, there are several options to specify the execution behavior of a transactional method. The following constants that represent propagation behavior are included in the TransactionDefinition definition:
1. TransactionDefinition.PROPAGATION_REQUIRED: if there is a transaction, join it; if there is no transaction, create a new transaction. This is the default value.
2. TransactionDefinition.PROPAGATION_REQUIRES_NEW: create a new transaction and suspend the current transaction if it exists.
3. TransactionDefinition.PROPAGATION_SUPPORTS: if there is a transaction, join the transaction; if there is no transaction, continue to run in a non-transactional manner.
4. TransactionDefinition.PROPAGATION_NOT_SUPPORTED: runs in a non-transactional manner, suspending the current transaction if there is a current transaction.
5. TransactionDefinition.PROPAGATION_NEVER: runs in a non-transactional manner, and throws an exception if there is a transaction.
6. TransactionDefinition.PROPAGATION_MANDATORY: if there is a transaction, join it; if there is no transaction, throw an exception.
7. TransactionDefinition.PROPAGATION_NESTED: if there is a transaction, create a transaction to run as a nested transaction of the current transaction; if there is no transaction, this value is equivalent to TransactionDefinition.PROPAGATION_REQUIRED.
Protection point (Savepoints)
First of all, know that savepoint is for fallback, there is no limit to the number of savepoint, savepoint is similar to snapshots in virtual machines. Savepoint is a point in a transaction. Used to cancel part of a transaction, and when the transaction ends, all save points defined in the transaction are automatically deleted.
When rollback is executed, you can fall back to the specified point by specifying a SavePoint.
Several important operations of fallback transactions
1. Set SavePoint savepoint a
2. Transaction rollback to an after canceling SavePoint a
3. Cancel all transactions rollback
Note: this fallback transaction must be used before commit
Public class UserRepository {private DataSource masterDataSource; private Connection connection = null; @ Autowired public void setMasterDataSource (DataSource masterDataSource) {this.masterDataSource = masterDataSource;} @ Transactional public boolean save (User user) {try {connection = masterDataSource.getConnection (); connection.setAutoCommit (false) / / set protection point Savepoint saveUser = connection.setSavepoint ("saveUser"); PreparedStatement prepareStatement = connection.prepareStatement ("insert into user (id,name,age) values"); prepareStatement.setLong (1, user.getId ()) PrepareStatement.setString (2, user.getName ()); prepareStatement.setInt (3, user.getAge ()); prepareStatement.execute (); try {update (user) } catch (Exception e) {System.out.println ("error." + e); / / rollback to the protection point connection.rollback (saveUser);} connection.commit ();} catch (SQLException e) {e.printStackTrace () } finally {try {connection.close ();} catch (SQLException e) {e.printStackTrace ();}} return true } public boolean update (User user) {System.out.println ("save user:" + user); try {PreparedStatement prepareStatement = connection.prepareStatement ("update user set name =?, age =? Where id =?) "); prepareStatement.setLong (3, user.getId ()); prepareStatement.setString (1," Wang Dana "); prepareStatement.setInt (2,100); prepareStatement.execute (); connection.commit () } catch (SQLException e) {e.printStackTrace ();} return true;}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.