SpringBoot Affairs

Key Concepts
automatic commit mode
for mysql database, by default, the database is in auto-commit mode. Each statement in a separate transaction, when this statement is finished, if executed successfully implicit commit the transaction, on failure of the implicit transaction is rolled back. For normal transaction management, is in a group of related operations in a transaction, it is necessary to turn off auto-commit mode database, the following is by:

        Check whether to automatically submit command (ON indicates turning automatic submission is 1, OFF means closed automatically filed, the value 0):

show variables like 'autocommit';
      after turn off the automatic submission, the user stays in a transaction until the execution of a commit or rollback statement submitted before the end of the current transaction to re-start a new business.

Connection = masterDataSource.getConnection the DataSource ();
connection.setAutoCommit (to false);
 
transaction isolation level
isolation level refers to the degree of isolation between the plurality of concurrent transactions. TransactionDefinition interface defines five levels of isolation represent constants:

TransactionDefinition.ISOLATION_DEFAULT: This is the default, use the default isolation level of the underlying database. For most databases, usually this value is TransactionDefinition.ISOLATION_READ_COMMITTED. 
TransactionDefinition.ISOLATION_READ_UNCOMMITTED: This represents a transaction isolation level data can be read another transaction modified but not yet committed. This level can not prevent dirty reads, non-repeatable read and phantom read, the isolation level is rarely used. For example, PostgreSQL actually does not have this level. 
TransactionDefinition.ISOLATION_READ_COMMITTED: The isolation level represents a transaction can only read data another transaction has been submitted. This level prevents dirty reads, which is the recommended value in most cases. 
TransactionDefinition.ISOLATION_REPEATABLE_READ: The isolation level indicates that a transaction can execute a query repeatedly throughout the process, and each returned record are the same. The levels prevent dirty reads and non-repeatable read. 
TransactionDefinition.ISOLATION_SERIALIZABLE: All transactions executed one by one in sequence, it is impossible to produce interference between such matters, that is to say, this level prevents dirty reads, non-repeatable reads and phantom reads. But this will seriously affect the performance of the program. Under normal circumstances you do not need this level.
Transaction propagation behavior of 
the propagation of the so-called transaction means that if before the start of the current transaction, a transaction context already exists, then there are several options you can specify execution behavior of a transactional approach. Comprising the following several represents the propagation constant in the behavior definition TransactionDefinition:

TransactionDefinition.PROPAGATION_REQUIRED: If the current transaction exists, it is added to the transaction; if no transaction, create a new business. This is the default value. 
TransactionDefinition.PROPAGATION_REQUIRES_NEW: create a new transaction, if the current transaction exists, put the current transaction pending. 
TransactionDefinition.PROPAGATION_SUPPORTS: If the current transaction exists, it is added to the transaction; if no transaction, non-transactional way places continue to run. 
TransactionDefinition.PROPAGATION_NOT_SUPPORTED: non-transaction run, if the current transaction exists, put the current transaction pending. 
TransactionDefinition.PROPAGATION_NEVER: non-transaction run, if the current transaction exists, an exception is thrown. 
TransactionDefinition.PROPAGATION_MANDATORY: If the current transaction exists, it is added to the transaction; if no transaction, an exception is thrown. 
TransactionDefinition.PROPAGATION_NESTED: If the current transaction exists, create a transaction to run as the current nested transaction transaction; if no transaction, the value is equivalent to TransactionDefinition.PROPAGATION_REQUIRED.
Protection point (Savepoints)
we must first know savepoint rollback is done, there is no limit the number of savepoint, savepoint and similar virtual machine snapshots. Savepoint is little affairs. To cancel part of the transaction, when the transaction ends automatically delete all saved points as defined in the transaction.

 When performing rollback, you can fall back to a specified point by specifying the save point. 
 Several important operation to roll back a transaction 

Set point savepoint a save 
after save cancel a transaction rollback to a point 
to cancel all transaction rollback 
 Note: This transaction is rolled back, must not commit prior to use of;

 

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);
//设置保护点
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("出错了。。"+e);
//回滚至保护点
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 {
PrepareStatement = Connection.prepareStatement PreparedStatement ( "the SET name = Update the User, the WHERE Age = the above mentioned id =???)");
PrepareStatement.setLong (3, user.getId ());
prepareStatement.setString (1, "Wang take" );
prepareStatement.setInt (2, 100/0);
prepareStatement.execute ();

Connection.commit ();

} the catch (SQLException E) {
e.printStackTrace ();
}

return to true;
}
}
 
------ ----------
Disclaimer: this article is the original article CSDN bloggers' code Shala Di ", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/ysl19910806/article/details/95760850

http://market.szonline.net/amaz/10713.html
http://market.szonline.net/amaz/10711.html
http://market.szonline.net/amaz/10709.html
http://market.szonline.net/amaz/10708.html
http://market.szonline.net/amaz/10707.html
http://market.szonline.net/amaz/10706.html
http://market.szonline.net/amaz/10705.html
http://market.szonline.net/amaz/10704.html
http://market.szonline.net/amaz/10702.html
http://market.szonline.net/amaz/10689.html
http://market.szonline.net/amaz/10675.html
http://market.szonline.net/amaz/10662.html
http://market.szonline.net/amaz/10650.html
http://market.szonline.net/amaz/10638.html
http://market.szonline.net/amaz/10626.html
http://market.szonline.net/amaz/10625.html
http://market.szonline.net/amaz/10624.html
http://market.szonline.net/amaz/10623.html
http://market.szonline.net/amaz/10622.html
http://market.szonline.net/amaz/10618.html
http://market.szonline.net/amaz/10616.html

Guess you like

Origin www.cnblogs.com/zjhcap/p/11533224.html