Declarative Transaction Management and declarative transaction details

Source: Internet
Author: User

Declarative Transaction Management and declarative transaction details

 

Spring supports two types of transaction processing:

1. Programmatic transactions: control the start, execution, and submission of transactions in the program. (This is not recommended, so I will not describe too much here)

2. Declarative transactions: Configure transactions in the Spring configuration file without writing code in the program. (recommended) I understand declarative transactions as follows: the Spring configuration file defines such a rule, which can specify which methods of which classes are added with transaction control during execution, and configure the relevant execution attributes of the transaction, when these methods of these classes are executed, they implicitly Add the code for starting, executing, committing, or rolling the transaction (which we cannot see). Declarative transactions are written in two ways: 2.1. xml configuration file 2.2 and annotations: Next we will first explain how to configure declarative Transaction Management in the xml configuration file.
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns =" http://www.springframework.org/schema/beans "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Xmlns: p =" http://www.springframework.org/schema/p "Xmlns: aop =" http://www.springframework.org/schema/aop "Xmlns: context =" http://www.springframework.org/schema/context "Xmlns: jee =" http://www.springframework.org/schema/jee "Xmlns: tx =" http://www.springframework.org/schema/tx "Xsi: schemaLocation =" http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop Spring-aop-4.2.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans Spring-beans-4.2.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context Spring-context-4.2.xsd http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee Spring-jee-4.2.xsd http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx /Spring-tx-4.2.xsd "> <! -- Similar to the financial department, class is money, and all instances requiring class are managed by srping --> <! -- <Context: component-scan>: There is a use-default-filters attribute, which defaults to true. This means that all annotation-marked classes under the specified package will be scanned, and register it as bean. we can find that the scanning granularity is too large. What if you only want to scan the Controller under the specified package? In this case, the sub-tag <context: CE-filter> becomes a brave place. <Context: component-scan base-package = "news" use-default-filters = "false"> <context: exclude-filter type = "annotation" expression = "org. springframework. stereotype. controller "/> </context: component-scan> If use-dafault-filters is not specified, the default value is true, which means that you add <context: exclude-filter/> is the same as that you didn't add. If you want to use the sub-tag of <context: component-scan>, you must change the use-dafault-filters value to false. Of course, the other one is the opposite. Here, we will not explain it as follows: Us. E-dafault-filters = "false": <context: exclude-filter> the specified scan does not exist. <context: include-filter> the specified scan <context: base-package attribute of component-scan>: sets the package to be scanned --> <! -- (This case is not used, but a full scan is used. The above content is only used to let everyone know about it) --> <context: component-scan base-package = "news .. "/> <context: property-placeholder location =" classpath: jdbc. properties "/> <! -- <Tx: annotation-driven transaction-manager = "transactionManager"/> --> <bean id = "myDataSource" class = "com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" driverClass "value =" $ {jdbc. driver} "/> <property name =" jdbcUrl "value =" $ {jdbc. url} "/> <property name =" user "value =" $ {jdbc. user} "/> <property name =" password "value =" $ {jdbc. password} "/> <! -- Check all idle connections in the connection pool every 300 seconds. --> <property name = "idleConnectionTestPeriod" value = "300"/> <! -- Maximum idle time. connections are discarded if they are not used within 900 seconds. If it is 0, it will never be discarded --> <property name = "maxIdleTime" value = "900"/> <! -- Maximum number of connections --> <property name = "maxPoolSize" value = "2"/> </bean> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate5.LocalSessionFactoryBean "> <property name =" dataSource "ref =" myDataSource "/> <property name =" hibernateProperties "> <props> <prop key =" hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <prop key =" hibernate. dialect "> org. hibernate. dialect. mySQL5Dial Ect </prop> <prop key = "hibernate. connection. autocommit "> false </prop> <prop key =" hibernate. hbm2ddl. auto "> update </prop> </props> </property> <property name =" mappingResources "> <list> <value> news/entity/News. hbm. xml </value> </list> </property> </bean> <bean id = "transactionManager" class = "org. springframework. orm. hibernate5.HibernateTransactionManager "> <! -- Create a Transaction Manager and manage sessionFactory (because all sessions are obtained from sessionFactory) --> <property name = "sessionFactory" ref = "sessionFactory"/> </bean> <! -- Configure the notification and what type of transactions are required for these methods --> <tx: advice id = "advice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "add *" propagation = "REQUIRED"/> <tx: method name = "del *" propagation = "REQUIRED"/> <tx: method name = "update *" propagation = "REQUIRED"/> <tx: method name = "*" propagation = "SUPPORTS" read-only = "true"/> </tx: attributes> </tx: advice> <! -- Configure the cut plane expression and combine the tx and cut plane expressions into one --> <aop: config> <! -- Expression, which defines which classes of the package need to be written into the transaction, but there is no way in the class, and what kind of transaction needs to be written --> <aop: pointcut expression = "execution (* news. service. *. *(..)) "id =" pointcut "/> <aop: advisor advice-ref =" advice "pointcut-ref =" pointcut "/> </aop: config> </beans>

I believe this code is not so easy for beginners to understand. Let me analyze it for you:

1. Create a Transaction Manager

 

2. Configuration notification

  

<Tx: attributes/>
Attribute Required Default Value Description
Name Yes   Method name to be switched in. Wildcards can be used.
Propagation No REQUIRED Transaction Propagation Behavior
Isolation No DEFAULT Transaction isolation level
Timeout No -1 Transaction timeout time (in seconds)
Read-only No False Is the transaction read-only?
Rollback- No   Will be triggered for rollbackException(s); Separated by commas. For example:'news.serviceImpl'
No-rollback- No   NoTriggered for rollbackException(s); Separated by commas. For example:'news.serviceImpl'
3. Configure the cut surface expression and merge the tx and cut surface expressions into one.

    

  

I will explain execution (* news. service. *. *(..)) the meanings of the following wildcards: The first * -- wildcard, any return value type, and the second * -- wildcard package news. the third class in the service * -- wildcard package news. the fourth method of any class under service .. -- The wildcard method can have 0 or more parameters: Packet news. any class in the service has any return value type, any number of parameters, and any name.

 

 

Here, the xml-based transaction management is basically done, and the last step is to import its related missing packages.

    

If you do not have these package friends can go to the Web site download: http://pan.baidu.com/s/1eSlyY1G

Note:
In the following cases, spring's transaction management will fail: ● The private method cannot be used to add transaction management. ● The final method cannot be used to add transaction management. ● The static method cannot be used to add transaction management. ● when you bypass the proxy object and directly call the method for adding transaction management, the transaction management will not take effect.
 

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.