About Transactions in spring

1. What is a transaction

In the real business scenarios we often encounter the problem frequently modified data read. At the same time, different service logic changes to the same data table,

This conflict is likely to result in data is not repairable confusion, so we need to manage the data transaction.

 

Problems caused by concurrent transactions

(1) The first category is missing Update: When revocation of a transaction, update the data to other committed transactions covered

(2) dirty read: a read transaction to update the other data uncommitted transactions.

(3) also called virtual phantom reads read: query a transaction is performed twice, the second result set contains no first or some rows have been deleted data, resulting in inconsistent results twice, just another transaction in this two inquiries middle insertion or deletion of data caused.

(4) Non-repeatable read: a transaction data of the same line is read twice, the result of the results of different states, the intermediate just another transaction to update the data, the results of two different, can not be trusted.

 

2. The transaction isolation level five

DEFAULT: default transaction isolation level database

READ_UNCOMMITTED: read uncommitted. Not solve any concurrency issues

READ_COMMITTED: Read Committed. Solve dirty read, there is a non-repeatable read and phantom reads.

REPEATABLE_READ: Repeatable Read. Solve dirty reads, non-repeatable read, there is a phantom reads.

SERIAIZABLE: serialization. Impure in concurrency issues.

3. The transaction propagation behavior

REQUIRED: The specified method must execute within a transaction. If the current transaction exists, it is added to the current transaction; if no transaction will create a new business.

SUPPORTS: Specifies the way to support the transaction, if the transaction can not be executed. For queries. If there is a transaction on the implementation of the transaction do not have to perform transaction

MANDATORY: There must be a transaction, the transaction did not throw an exception

REQUIRES_NEW: always new transaction, if the current method have affairs, but also create a new transaction before the transaction suspension

NOT_SUPPORTED: Pure does not perform transaction services, general futile

NEVER: there is a transaction to throw an exception: too much good

NESTED: If there is a transaction, it is nested transaction execution, the transaction would not create a new

4. Spring Service agent factory

 <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> 

  <Property name = "dataSource" ref = "myDataSource" /> specified database data source

</bean> 
<! - Registration proxy factory classes ->  
<Bean class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id = "stockServiceProxy"> Creating a proxy object
   <! - Specifies the Transaction Manager ->  
  <property name="transactionManager" ref="transactionManager"/> 
    <! - target audiences ->  
    <Property name = "target" ref = "stockService" /> specified service object
    <! - attribute specifies the transaction (the transaction interface definition three constant value) -> 
    <property name="transactionAttributes"> 
      <props> 
      ! <-! Specific transaction attribute specifies the business method -> <- key = "business method name, can use wildcards *" value: specified transaction values ​​often defined interfaces (isolation level, propagation behavior, timeout) readOnly: optimization indicator (to the database to see) ->     
        <Prop key = "select *"> ISOLATION_DEFAULT, PROPAGATION_REQUIRED </ prop> propagation behavior isolation level
        <Prop key = "find *"> ISOLATION_DEFAULT, PROPAGATION_SUPPORTS, readOnly </ prop> readOnly is read-only data representing the efficiency can be improved
        <Prop key = "A specific method specified service object"> ISOLATION_DEFAULT, PROPAGATION_REQUIRED, -StockException </ prop> 
      </props>
     </property> 
</bean>

 Spring use transaction annotation

  @Transactional

  You need to open the annotation-driven transactions

  <bean class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" the above mentioned id = "transactionManager"> <Property name = "dataSource" ref = "myDataSource" /> </ bean>
  <-! open transaction annotation-driven transaction-manager : Specifies the transaction manager bean ID ->
   <TX: transaction-Driven-Annotation Manager = "the transactionManager" />

  @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollbackFor={StockException.class})
  @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)

Guess you like

Origin www.cnblogs.com/zhengminghui/p/11330298.html