Spring (c) Spring transaction operations

Source: Internet
Author: User
Tags savepoint

The previous blog post explains what AOP is. Learned to write the implementation of AOP, but did not actually use it, this blog post is the application of AOP technology, the focus is the processing of transactions.

--wh

First, JdbcTemplate 

What is JdbcTemplate?

Spring provides for manipulating database templates, similar to Dbutils, in layman's terms, we operate the database, and spring also helps us to provide an operational database tool for us to use instead of manually writing a connection database, getting a result set, etc. This tool is JdbcTemplate. Like Dbutils, if you want to use JdbcTemplate, you must configure the data source for him.

First Step: Guide Package

              

Step Two: Configure the data source, configure the template, and configure the template for Userdao

Using DBCP to act as a data source

              

Using C3P0 to act as a data source

              

Step three: Use JdbcTemplate in Userdao

              

But spring in order to make us more convenient, do not have to write the set method and JdbcTemplate property every time, give us a class jdbcdaosupport, we only need to inherit him, we can directly get JdbcTemplate, Because Jdbcdaosupport helped us write the set method.

              

Fourth step: Using the JdbcTemplate API to manipulate the database

Update for the Add and revise operation

queryForObject Query A

Query queries All

queryForInt Query a shaping (paging)

              

The fifth step, the test successfully inserted data, the other on the contingency.

                    

Promotion: Using properties

Match the specific parameters of the connection data to the properties file, loaded by spring, and used in the spring configuration file

              

Ii. Review of matters

If it's not clear, read this blog post detailing the transaction http://www.cnblogs.com/whgk/p/6182742.html

Transactions: A set of business operations, either all successful or all failed

Characteristics: ACID

A: atomicity means that the business is a whole, either all succeeds or all fails.

C: Consistency, data integrity, you turn 100 to me, you minus 100, I'm going to add 100

I: Isolation, concurrency (multiple transactions)

D: Persistence, committed transactions have been saved in the database, can not change the

Isolate the problem:

Dirty read: Read to uncommitted data

Non-repeatable READ: Read the data that has been submitted (update)

Wasted (Phantom Read): Read the data that has been committed (insert)

Resolving Isolation issues

READ UNCOMMITTED: There are three issues.

Read Committed: There are two issues to resolve the dirty read problem (Oracle default level)

REPEATABLE READ: There is a problem resolving dirty reads, non-repeatable read issues (MySQL default level)

Serialization: Solve all problems.

and our normal program to use the transaction, by connecting the connection to open the transaction and close the transaction or rollback,

              

The theoretical analysis of how spring carries out the management of affairs

     Platformtransactionmanager: Transaction platform Manager.

When spring manages transactions, it must use the Platform transaction manager, which is an interface that defines the specification of a spring usage transaction, that is, if you want to use spring to help you manage transactions, you must follow this specification. Spring also helped us implement some of the managers needed for common technologies, such as JDBC, which has a JDBC manager and Hibernate manager, all of which implement the Platformtransactionmanager interface in spring.

              

JDBC Transaction manager: Datasourcetransactionmanager

Hibernate transaction manager: Hibernatetransactionmanager

Platformtransactionmanager is an interface, so let's see what methods it defines for us to use.

                  

Transactionstatus gettransaction (transactiondefinition);

Get transaction, parameter Transactiondefinition (transaction detail), this parameter is required to be configured by us to know how the transaction is handled by the contents of our configuration. This is going to be detailed below

Commit (Transactionstatus);

Actions to commit a transaction based on state

Rollback (Transactionstatus);

To roll back a transaction based on state

      Transactionstatus

Spring uses the manager to manage transactions through the State (operations), and we don't have to care about this because it's a matter of spring internal operations, but we can see what the methods are

              

      Transactiondefinition

The spring manager must obtain the appropriate transaction through the transaction details setting for transaction management. Here This is important, we need to configure this

               

Set 4 Isolation levels It's needless to say, it's the same as we said above. Resolves four levels of isolation issues.

Propagation behavior: A business A, a business b,ab how to share transactions, different propagation behavior sharing scheme is different.

What does that mean? For example, business A is a bank transfer business. Business B for the transfer of text messaging business, usually we turn the money, then we need to receive a text message that our account was transferred to how much money, and the recipient of the money need to receive a text message that the account was transferred to how much money, then the two businesses are using the same business? or separate use of different transactions, that is, if the use of the same transaction, we have successfully transferred money on behalf of business a success, but the business B sent a message when the problem, then the transaction failed, then the money just turned to not succeed, need to roll back, but in real life, is not the case, the transfer of money success, SMS is not sent successfully, then the text message is sent again. You don't need to get business a back in action again. This is a workaround for business A and business B shared transactions, so that both of them use their own transactions. The propagation behavior is to provide the properties of such a shared scheme.

Communication Behavior Programme

                1.propagation_required , REQUIRED, transaction must be used (default value)

A If a transaction is used, B uses the same transaction. (Supports current transaction)

A if there is no transaction, B creates a new transaction.

2.propagation_supports,supports, support transactions

A If a transaction is used, B uses the same transaction. (Supports current transaction)

A If there is no transaction, B executes as a non-transaction.

3.propagation_mandatory,mandatory Mandatory

A If a transaction is used, B uses the same transaction. (Supports current transaction)

A If there is no transaction, B throws the exception

4.propagation_requires_new , requires_new, must be a new business

A If you use a transaction, B suspends the transaction of a, and then creates a new.

A if there is no transaction, B creates a new transaction

5.propagation_not_supported, not_supported does not support transactions

A If a transaction is used, b suspends the transaction of a and executes as a non-transactional

A if there is no transaction, B executes in a non-transactional

6.propagation_never,never never used

A If you use transactions, B throws an exception

A if there is no transaction, B executes in a non-transactional

7.propagation_nested NESTED nesting

A If a transaction is used, B takes a nested transaction.

Nesting the underlying transaction uses SavePoint to set the SavePoint, which is the equivalent of splitting multiple transactions. For example, business A is ab two Cao Zu, Business B is a CD two operation, business AB uses the same transaction, and on the AB (point) CD, when business B fails, it rolls back to point, so that business A is still a success, that is, the operation of keeping points.

The underlying uses nested try Mode

   

Master:propagation_required,propagation_requires_new,propagation_nested

         Summary :

Steps for spring transaction programming

1. Determine the manager

2. Transaction details (whether read-only, isolation level, propagation behavior, etc.) must be configured

Having configured the transaction details and determined which manager to use, spring knows how to handle the transaction.

Iv. Spring uses AOP technology for transactional operations (XML-based)

We know what spring uses for transactions, but simply using the above-written transactions, we need to write code for every method that needs to use transactions, but it's a lot easier to combine the AOP ideas we learned earlier.

Focus on Configuration

              

        

Focus on 47 to 59 lines of code, configure the transaction manager and transaction details, and then use AOP to apply our transactions to the specified pointcut, using an expression. Specifies a range. The combination of transaction details and transaction manager is equivalent to the notification (enhanced method), so the dish can be written on exadvice on the notification reference. This has to be made clear. That is, we do not need to manually write what to open the transaction code, spring all help us to write, we just need to configure the transaction details.

It is important to note that if you do not use the interface, then you need to write 56 lines of this line of code, if you use the interface, then do not need to write. Before we do not need to be because of our own handwritten notice, not the same, probably for the transaction, it must be written, here I do not know, anyway, remember, if the transaction agent, there is no interface and the interface is not handled the same way.

Configuration of transaction Details

              

In this way, it is convenient for different methods to handle different transactions. For example, add*, which means the method at the beginning of the add, uses the propagation behavior for required transactions, and Find*,find begins with a method that is read-only and the propagation behavior is required.

Five, Spring uses AOP technology for transactional operations (annotation-based)

Super simple. Three steps

1. Declaring the transaction manager

2. Submit the transaction manager to spring

The first two steps are written in XML.

            

3. Use annotations on the target class or method @Transactional

Use @transactional (write transaction details)

            

Vi. Summary

The AOP described above uses spring's default AOP and does not use the ASPECTJ framework to differentiate clearly. And today this article is about using AOP to manage transactions. In fact, it is through AOP to strengthen the specified method, and the content of the enhancement is the content of the transaction, and the content of the transaction is done by spring to help us, that is, the notice is spring to help us write, we only need to configure the transaction manager and transaction details. In the previous article, all the notifications were written manually by ourselves, in order to experience the idea of AOP, which is not the same. So don't confuse yourself.

First post: IOC (inversion control), configure the Bean in the configuration file, and spring helps us create the object. An object is created by default. Singgton

The second blog post: AOP (aspect-oriented programming), an understanding of the AOP idea, and how spring uses AOP techniques, the underlying use of JDK and cglib for proxies, interfaces using the JDK, and no interfaces to use Cglib. Mastering the spring's built-in AOP technology, there is also a ASPECTJ framework. To know how these two are configured, use ASPECTJ annotations and XML

Third post (this article): Application of AOP (Spring transaction Processing), spring helps us to write the transaction, we only need to configure the transaction manager, and the transaction details (the equivalent of the notification), and then use the spring built-in AOP technology, Use the transaction on the specified method (equivalent to the enhanced specified method), XML configuration, and annotations in two ways.

Fourth blog: Integrated SSH framework

Spring (c) Spring transaction operations

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.