The configuration of the spring transaction uses

Source: Internet
Author: User

The first: one of the programmatic transaction management
Configuration information in the configuration file:

id="txManagerForStatus"       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <propertyname="dataSource"ref="dataSource" />    </bean>

Code used by the transaction:

    @Autowired    @Qualifier("Sqlsession")PrivateSqlsessiontemplate sqlsession;//    @Autowired    @Qualifier("Txmanagerforstatus")PrivateDatasourcetransactionmanager Txmanager;@Override     Public int Updatebyprimarykey(Jobmessage vo) {Log.info ("Updatebyprimarykey start,vo="+VO);intExestatus =1; Transactiondefinition td =NewDefaulttransactiondefinition (transactiondefinition.propagation_required); Transactionstatus ts = This. Txmanager.gettransaction (TD);Try{exestatus = Sqlsession.update ("Jobmessage.updatebyprimarykey", VO); }Catch(Throwable t)            {txmanager.rollback (TS);        Log.error (T.getmessage ());        } txmanager.commit (TS); String Msurl = Propertiesutil.getpropertyvalue ("Ms_url"); Log.info ("Ms_url="+ Msurl); Messageformat myformat=NewMessageformat (Msurl); String URL =myformat.format (NewString[]{string.valueof (Vo.getjobseq ()), Vo.gettaskid ()});        Httpclientutils.doget (URL); Log.info ("Updatebyprimarykey End");returnExestatus; }

Second type: declarative transactions
Configuration information:

id="sitdbTxManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <propertyname="dataSource"ref="sitdb" />    </bean>    transaction-manager="sitdbTxManager"/>

Also add the TX namespace to the Spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"    xmlns:jms="http://www.springframework.org/schema/jms"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"

MyBatis automatically participates in spring transaction management without the need for additional configuration, Transaction management will not work as long as the data source referenced by Org.mybatis.spring.SqlSessionFactoryBean is consistent with the data source referenced by Datasourcetransactionmanager.
In addition, you need to download the dependency package Aopalliance.jar to the Web-inf/lib directory. Otherwise, spring will report an exception when initialized
Java.lang.noclassdeffounderror:org/aopalliance/intercept/methodinterceptor

By default, only method calls from outside are captured by the AOP proxy, which means that other methods inside the class that call this class do not cause transactional behavior, even if the called method uses @transactional annotations to decorate.

@Transactional(readOnly =true) Public  class defaultfooservice implements fooservice {   PublicFooGetfoo(String fooname) {//Do something}//These settings has precedence for this method  //The annotation property on the method overrides the same property on the class annotation  @Transactional(readOnly =false, propagation = propagation.requires_new) Public void Updatefoo(foo foo) {//Do something}}

@Transactional Properties

 
Properties type Description
Value String Optional qualifier descriptor, specifying the transaction manager to use
Propagation Enum:propagation Optional transaction propagation behavior settings
Isolation Enum:isolation Optional Transaction ISOLATION Level setting
ReadOnly Boolean Read-write or read-only transactions, default read and write
Timeout Int (in seconds granularity) Transaction time-out time setting
Rollbackfor Class object array, must inherit from Throwable An array of exception classes that cause transaction rollback
Rollbackforclassname Class An array group, must inherit from Throwable An array of exception class names that caused the transaction rollback
Norollbackfor Class object array, must inherit from Throwable An array of exception classes that will not cause the transaction to be rolled back
Norollbackforclassname Class An array group, must inherit from Throwable An array of exception class names that will not cause the transaction to be rolled back

Usage

@Transactional can be used on interfaces, interface methods, classes, and class methods. When used on a class, all public methods of the class will have transactional properties of that type, and at the method level we can also use the callout to override the class-level definition.

Although @Transactional annotations can be used on interfaces, interface methods, classes, and class methods, Spring does not recommend using this annotation on an interface or interface method, because it takes effect only when using an interface-based proxy. In addition, @Transactional annotations should only be applied to the public method, which is determined by the nature of Spring AOP. If you use @Transactional annotations on protected, private, or default visibility methods, this is ignored and no exceptions are thrown.

Reference: http://www.cnblogs.com/xusir/p/3650522.html
Differences between the two types:
The minimum granularity of a declarative transaction can only be scoped to the method level, and cannot be scoped to the code block as a programmatic transaction. Workaround: You can separate code blocks that require transaction management into methods, and so on

The configuration of the spring transaction uses

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.