Spring transaction management can be implemented in several ways

Source: Internet
Author: User
The essence of spring + hibernate: The datasource used by hibernate, The sessionfactory instance of hibernate, And the hibernatetransactionmanager of the Transaction Manager are all handed over to spring for management. So how does hibernate implement transaction management before integration? Using servletfilter to manage database transactions avoids the need to process database transactions each time during database operations. I. Four features of a transaction: atomicity: all operations on the database in a transaction are an inseparable sequence of operations, either full or full. Consistency: data will not be damaged by the execution of transactions. Isolation: the execution of a transaction is not affected by other transactions (processes. Transactions that are concurrently executed do not interfere with each other. Persistence: Once a transaction is committed, its changes to the database will be permanent. 2. Transaction implementation methods: two methods are available: encoding and declarative transaction management. Declarative Transaction Management implemented based on AOP technology is essentially: interception before and after method execution, and then creation and addition of transactions before the target method starts, commit or roll back the transaction based on the execution status after the target method is executed. Declarative transaction management can be implemented in two ways: XML-based configuration files, and @ transactional Annotation on business methods to apply transaction rules to business logic. Iii. Time to create a transaction: whether to create a transaction is controlled by the transaction Propagation Behavior. You do not need to specify a read-only transaction for the read data, but the data insertion and modification require transaction management to delete the data. A common transaction management configuration: A combination of transaction interceptor transactioninterceptor and automatic transaction proxy beannameautoproxycreator <! -- Define the hibernate Transaction Manager hibernatetransactionmanager --> <bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager"> <! -- Dependency injection sessionfactory defined above --> <property name = "sessionfactory" ref = "sessionfactory"/> </bean> <! -- Define the Spring transaction interceptor transactioninterceptor --> <bean id = "transactioninterceptor" class = "org. springframework. transaction. Interceptor. transactioninterceptor"> <! -- Dependency injection the Transaction Manager transactionmanager defined above --> <property name = "transactionmanager" ref = "transactionmanager"/> <! -- Define the method for transaction interception and the transaction control type used --> <property name = "transactionattributes"> <props> <! -- All Methods Starting with browse, list, load, get, and is use read-only transaction control types --> <prop key = "Browse *"> propagation_required, readonly </prop> <prop key = "list *"> propagation_required, readonly </prop> <prop key = "load *"> propagation_required, readonly </prop> <prop key = "get *"> propagation_required, readonly </prop> <prop key = "is *"> propagation_required, readonly </prop> <! -- All methods are subject to transaction control. If no transaction exists, create a transaction --> <prop key = "*"> propagation_required </prop> </props> </property> </bean> <! -- Define beannameautoproxycreatdorf to process spring transactions --> <Bean class = "org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator"> <! -- Automatically generate a Service proxy for the specified Bean --> <property name = "beannames"> <list> <value> adminservice </value> <value> columnsservice </value> <Value> newsservice </value> <value> crawlservice </value> <value> memberlevelservice </value> <value> memberservice </value> <value> categoryservice </value> <Value> merservice </value> <value> cartservice </value> <value> ordersservice </value> <value> trafficservice </value> </List> </property> <! -- When this attribute is true, indicates that the proxy is the interface of the target class rather than the target class --> <property name = "proxytargetclass"> <value> true </value> </property> <! -- Dependency injection the transaction interceptor transactioninterceptor defined above --> <property name = "interceptornames"> <list> <value> transactioninterceptor </value> </List> </property> </ bean> note: *************************************** **************************************** * ************************** [principles of the above transaction interceptor and automatic transaction proxy Methods: like struts2, they all use powerful interceptor functions to intercept calls to business logic methods, and beannameautoproxycreator automatically generates a transaction proxy and finally sends it to the Transaction Manager, unified management ]************************************* **************************************** ******************************

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.