Declarative transactions for database transactions

Source: Internet
Author: User
Tags aop rollback

I. Overview of transactions

1, in the application field of Java EE Enterprise Development, in order to ensure the integrality and consistency of data, it is necessary to introduce the concept of database transaction, so transaction management is an essential technology in enterprise-level application development.

2. A transaction is a group of multiple database operations that are merged into a single whole ( Unit of work ) due to a logically tight association, either executed or not executed .

3. Four key properties of a transaction (ACID)

3.1, atomicity (atomicity): The Meaning of "atom" is " no More ", the atomicity of a transaction is logically integral to the multiple operations involved in a transaction. The atomicity of a transaction requires that all operations in the transaction are either executed or not executed.

3.2, consistency (consistency): "Consistent" refers to the consistency of data, in particular: all data are in compliance with the business rules of the state . The consistency principle requires that, regardless of the number of operations involved in a transaction, the data is correct before the transaction executes and the data is still correct after the transaction executes. If one or several of the operations fail during the execution of a transaction, all other operations must be undone and the data restored to the state before the transaction was executed, which is the rollback.

3.3, isolation (Isolation): In theactual operation of the application, transactions are often executed concurrently, so it is very likely that there are many transactions processing the same data at the same time, so each transaction should be isolated from other transactions, to prevent data corruption. The principle of isolation requires that multiple transactions do not interfere with each other during concurrent execution .

3.4. persistence (Durability): The persistence principle requires that after a transaction is completed, modifications to the data are persisted and will not be affected by various system errors or other unforeseen circumstances. Typically, a transaction's modification of the data should be written to the persisted memory.

Ii. Management of Spring affairs

1. Programming Transaction Management

①, using native JDBC APIs for transaction management

[1] Get database connection Connection Object

[2] canceling automatic commit of transactions

[3] Perform Actions

[4] manually commit transactions when the operation is completed normally

[5] Rollback transaction when execution fails

[6] Close Related Resources

②, evaluation

Using native JDBC APIs to implement transaction management is the cornerstone of all transaction management methods and is also the most typical programmatic transaction management. Programmatic transaction management requires that transaction management code be embedded in a business method to control the commit and rollback of a transaction. When you manage transactions programmatically, you must include additional transaction management code in each transaction operation. The code for transaction management is obviously a non-core business relative to the core business , and if multiple modules use the same pattern of code for transaction management, it will obviously result in a greater degree of code redundancy .

2. Declarative transaction Management

In most cases, declarative transactions are better than programmatic transaction management: It separates transaction management code from business methods, and implements transaction management in a declarative manner.

of the transaction management code Fixed mode as a crosscutting concerns , which can be modularized through the AOP approach, Spring AOP Framework Implement declarative transaction management .

Spring defines an abstraction layer on top of different transaction management APIs that is configured to take effect so that application developers You can use Spring's transaction management mechanism without having to understand the underlying implementation details of the transaction management API .

Spring supports both programmatic transaction management and declarative transaction management.

3. the transaction manager provided by Spring

Spring abstracts a whole set of transaction management mechanisms from different transaction management APIs , allowing transaction management code to be isolated from specific transactional technologies. Developers are configured to manage transactions without having to understand how the underlying is implemented.

Spring 's core transaction management abstraction is Platformtransactionmanager. It encapsulates a set of technology-independent methods for transaction management. The transaction manager is required, regardless of which transaction management policy ( programmatic or declarative )is used in Spring.

The transaction manager can be declared in the Spring IOC container in the form of a normal bean.

4, the main implementation of the transaction manager

①, Datasourcetransactionmanager: Only one data source needs to be processed in the application and is accessed through JDBC.

②, Jtatransactionmanager: transaction management with JTA (Java Transaction API) on the Java EE Application Server .

③, Hibernatetransactionmanager: access the database with the Hibernate framework.

III. Preparation of test data

Build the database and tables you need to use for your tests.

Iv. Importing Jar Packages

[1]IOC container required for JAR package
[2] The jar packages required by AOP
[3] Jar packages required for jdbctemplate operation
[4] MySQL Driver and c3p0

V. Preliminary implementation

1. Configure the transaction manager. It is necessary to configure the relevant things in the spring configuration file that you build. Here my config file is applicationcontext.xml. As shown in the following code:

<!--configuration transaction manager--><bean id= "Datasourcetransactionmanager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "><property name=" datasource "ref=" Combopooleddatasource "></property></bean><!--turn on annotation-based declarative transaction--><!-- Transaction-manager used to configure the transaction manager--><tx:annotation-driven transaction-manager= "Datasourcetransactionmanager"/ >

vi. Add annotations to the methods that require transaction control :@Transactional

Such a simple transaction control is done.

Declarative transactions for database transactions

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.