Transaction management
A database transaction is a sequence of operations that is considered a single unit of work. These operations should either be performed completely or not at all. Transaction management is an important part of an RDBMS for enterprise applications to ensure data integrity and consistency. The concept of a transaction can be described as having the following four key properties that are said to be ACID:
atomicity: A transaction should act as a single unit, which means that the entire sequence operation is either successful or failed.
Consistency: This represents the consistency of referential integrity of the database, the only primary key in the table, and so on.
Isolation: Many transactions with the same data set may be processed at the same time, and each transaction should be isolated from other transactions to prevent data corruption.
- Persistence: Once a transaction has completed its entire operation, the result of the transaction must be permanent and not be removed from the database due to a system failure.
A true RDBMS database system will guarantee all four properties for each transaction. A simple view of transactions published to a database using SQL is as follows:
Start the transaction using the BEGIN TRANSACTION command.
Use SQL query statements to perform various delete, update, or insert operations.
- If all operations succeed, the commit operation is performed, or all operations are rolled back.
The Spring framework provides an abstraction layer at the top of different underlying transaction management APIs. Spring's transactional support is designed to provide an alternative to EJB transactions by adding transaction capabilities to POJOs. Spring supports both programmatic and declarative transaction management. EJBs requires an application server, but Spring transaction management can be implemented without the need for an application server.
Local things vs. global transactions
A local transaction is a transaction that is specific to a single transactional resource, such as a JDBC connection, while a global transaction can span multiple transactional resource transactions, such as in a distributed system.
Local transaction management is useful in a centralized computing environment where application components and resources are located in a single unit point, and transaction management involves only one local data manager running in one machine. Local transactions are easier to implement.
Global transaction management requires that in a distributed computing environment, all resources are distributed across multiple systems. In this case, transaction management requires both local and global scope. A distributed or global transaction executes across multiple systems, and its execution requires coordination between the global transaction management system and the local data managers of all relevant systems.
Programmatic vs. declarative
Spring supports two types of transaction management:
Declarative transaction management is preferable to programmatic transaction management, although it is less flexible than programmatic transaction management, but it allows you to control transactions through code. But as a crosscutting concern, declarative transaction management can be modularized using an AOP approach. Spring supports declarative transaction management using the Spring AOP framework.
Spring Transaction Abstraction
The key to the Spring transaction abstraction is defined by the Org.springframework.transaction.PlatformTransactionManager interface, as follows:
Public Interface Platformtransactionmanager { transactionstatus gettransaction (transactiondefinition definition); throws transactionexception; void throws transactionexception; void throws transactionexception;}
Transactiondefinition is the core interface for transaction support in Spring, which is defined as follows:
Public Interface transactiondefinition { int getpropagationbehavior (); int getisolationlevel (); String getName (); int gettimeout (); Boolean isreadonly ();}
Isolation level Possible values:
Possible values for the propagation type:
The Transactionstatus interface provides a simple way for transaction code to control the execution of transactions and query the state of transactions.
Public Interface extends Savepointmanager { boolean isnewtransaction (); Boolean hassavepoint (); void setrollbackonly (); Boolean isrollbackonly (); Boolean iscompleted ();}
Why are transactions so important? Like the four attributes of a transaction, atomicity, consistency, isolation, persistence, ensure data stability, security, and authenticity. The bank's example of taking money is sufficient to illustrate.
No one wants, because the system is not good, especially financial software, if the transaction is not done, the loss will be very large. For the enterprise, because of business reasons, the user inexplicably account of a few 0 more, for users of course is very happy, but for the enterprise is a devastating blow.
Spring (14) 's Business