SPRING+SPRINGMVC, declarative transaction invalidation, causes, and solutions

Source: Internet
Author: User
Tags aop

A. Declarative transaction configuration:

	<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
		<property name= "DataSource" ref= "DataSource"/>
	</bean>
    <tx:advice id= "Txadvice" Transaction-manager= "TransactionManager" >
	   <tx:attributes>
	   		 <tx:method name= "add*" propagation = "REQUIRED" read-only= "false"/>
		     <tx:method name= "del*" propagation= "REQUIRED" read-only= "false"/>
		     <tx:method name= "get*" propagation= "REQUIRED" read-only= "true"/> <tx:method "name="
		     propagation= "REQUIRED" read-only= "false"/>          
	   </tx:attributes>
    </tx:advice>
    <AOP: config>   
	    <aop:pointcut id= "servicemethods" expression= "Execution" (Public * com.lexing.platform.service.*. *(..))" />
	    <aop:advisor  advice-ref= "Txadvice" pointcut-ref= servicemethods "/>"
    </aop:config >

Two. Declarative transaction invalidation, reason

Root cause: An instance of the @service annotation was assembled by a child container scan.

The context of spring is a parent-child container in which the spring configuration file is loaded by Servletcontextlistener and the parent container is generated by the SPRINGMVC load configuration file. The Controller is assembled with an instance of the @service annotation (@Controller instance relies on the @service instance) when the child container scans the assembly, and the instance is supposed to be initialized by the parent container to guarantee the enhanced processing of the transaction. So at this time will be the original service (without a transaction to strengthen processing, so there is no transaction processing capacity.)

Three. Solutions

1.spring configuration file ApplicationContext:

<!--do not scan classes with @controller annotations, let SPRINGMVC child containers load.
<context:component-scan base-package= "Com.lexing.platform" >   
	<context:exclude-filter type= " Annotation "expression=" Org.springframework.stereotype.Controller "/>   
</context:component-scan>  


2.springMVC Config file servlet-context.xml

<!--class with @Service annotations is instantiated by the spring parent container, [@Service instance relies on @repository instances, so the spring parent container will also assemble @repository instances]
<context:component-scan base-package= "Com.lexing.platform" >   
	<context:exclude-filter type= " Annotation "expression=" Org.springframework.stereotype.Service "/>   
</context:component-scan>   



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.