Java Transaction Processing

Source: Internet
Author: User
Tags aop

When it comes to transactions, it is nothing more than commit commit and rollback rollback of a transaction.

A transaction is a sequence of operations in which either all succeeds, the transaction commits, or one operation fails and the transaction rolls back.

To know the 4 characteristics of a transaction acid. That is, atoms, consistent, isolated, persistent.

Atomicity (atomicity): A transaction is an inseparable unit of work, many operations in a transaction, either done or not.

Consistency (consistency): Transactions must shift the database from one consistent state to another. Consistency and atomicity are closely related.

Isolation: (isolation) The execution of a transaction cannot be disturbed by other transactions, that is, the operations inside a transaction and the data used are isolated from other transactions that are concurrent, and the transactions performed concurrently cannot interfere with each other.

Persistence (Durability): Persistence, also known as permanence, means that once a transaction is committed, his changes to the data in the database should be permanent. The next operation or failure should not have any effect on alignment.

Set up transactions manually

try{
Open transaction
Operation "DB operation"
Operation 2--"DB operation
Operation 3--"DB operation
Commit a transaction
}catch{
Rolling back a transaction
}

Use the SSM framework below to manage transactions

1 Configuring the transaction manager first

The key class is Datasourcetransactionmanager.

<bean id="Txmanager" class="Org.springframework.jdbc.datasource.DataSourceTransactionManager" >

<property name= "dataSource" ref="DataSource"/>(represents the transaction of which data source is managed)

</bean>

2 declaring the nature of the transaction, the rules (annotated mode without configuring transaction attributes)

<!--define transaction enhancements through <tx:advice> tags and develop transaction manager--

<tx:advice id="Txadvice" transaction-manager="Txmanager" >(The transaction attribute of which transaction manager is declared)

<!--defining properties, declaring transaction rules--

<tx:attributes>

<tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>

</tx:attributes>

</tx:advice>

Analysis of the propagation propagation mode of the transaction

53041564/

analysis of the isolation of the transaction

Isolation of
When reading and writing the same data, the transaction concurrency can result in error data processing, resulting in dirty reads, Phantom reads, non-repeatable reads and other errors, which can be resolved by setting the isolation level of the transaction
Dirty reads: One transaction reads data that is overwritten by another transaction but not yet committed, and if the data is rolled back, the data that is read is not valid
Non-repeatable READ: In the same transaction, multiple reads of the same data return different results, in other words, subsequent reads can be read to another transaction committed update data, so there will be two read data is not the same (such as transaction T1 read a data, transaction T2 read and modify the data, T1 read the change data in order to read the value of the wake-up check, get different results)
Repeatable reads: When data is read multiple times in the same transaction, the same data is guaranteed to be read, that is, subsequent reads cannot be read to another transaction that has committed the updated data
Phantom read: After a transaction has read a few rows of records, another transaction inserts some records, the phantom reads occur, and then in the subsequent query, the first transaction will find the original record

Isolation level (Isolation):
1, read UNCOMMITTED read_uncommitted has not yet submitted to be able to view
2, read submitted read_committed This level will be viewed, additions and deletions to separate, uncommitted additions and deletions can not be viewed
3, Repeatable Read Repeatable_read
4. Serialization operations serializable transactions to the same data mutually exclusive, must wait for the first thing to complete, the second transaction can operate
Default defaults (Oracle default is second, MySQL is third)
Level of isolation from low to high read_uncommitted-->read_committed-->repeatable_read-->serializable level the higher the security, the lower the concurrency processing power

Itachi
Links: https://www.jianshu.com/p/702ea7a02a10
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

Analysis of the rollback-for of the transaction

Rollback exception is a general exception when the transaction is rolled back

3 Configuring Transactions

3.1 Using annotations to configure

<!--turn on transaction annotations, the classes and methods that label @transactional will have transactional--

<tx:annotation-driven transaction-manager= "Txmanager"/> (which transaction manager to use to configure transactions)

annotated usage, The public method must be the only line, do not catch the exception, you want the exception to be thrown automatically, or the transaction cannot be rolled back. method to write in the service layer is not valid in the controller.

@Transactional (isolation=isolation. read_commited)
public void F3 () {
Handling 1
Handling 2
}

3.2 Using tags to configure (annotation mode without configuring transaction attributes)

<!--insert transactions in slices--

<aop:config Proxy-target-class= "true" >

Transaction agent (proxy attribution)
What are the issues that are received with sqlsesstion created?
Proxy-target-class=false
JDK's proxy (only with impl, not interface)
Proxy-target-class=true
Spring Proxy (interface is available)

< Span class= "Hljs-attribute" >< Span class= "Hljs-attribute" >)

<aop:pointcut id="Servicemethod" expression="Execution (* com.lyt.soa.service). *.*(..))" />(transactions in which pointcuts are added)

<!--combine transaction enhancement with pointcut (to weave a transaction plane)--

< Span class= "Hljs-tag" ><aop:advisor pointcut-ref=" ServiceMethod " advice-ref=" Txadvice "/> (weaving transactions and stipulating transaction attributes)

</aop:config>

 

Java Transaction processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.