Spring Transaction @Transactional Annotation parameter tx:annotation-driven_transactional

Source: Internet
Author: User

First modify the Applicationcontext.xml as follows:

... <!--define a data source--> <bean id= "DataSource" class= "Org.apache.tomcat.jdbc.pool.DataSource" > <property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://localhost:3 306/spring_test "/> <property name= username" value= "root"/> <property name= "password" value= "Root"/> </bean> <!--define JdbcTemplate Bean--> <bean id= "JdbcTemplate" Org.springframework.jdbc.core.JdbcTemplate "p:datasource-ref=" DataSource "> </bean> <!--configuration transaction manager
; <bean id= "Txmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" P:datasou rce-ref= "DataSource" > </bean> <!--enables scanning for @Transactional annotations--> &LT;TX: Annotation-driven transaction-manager= "Txmanager"/> <!--callout in the bean's code @transactional can be injected by the transaction manager--> <bean Id= "Userscore" class= "net.hingyi.springDemo.transaction. Service. Userscoreserviceimpl "p:userscorerepository-ref=" Userscorerepository_jdbc "/> <bean id=" userScoreRepository _jdbc "class=" Net.hingyi.springDemo.transaction.repository.UserScoreRepositoryImpl "p:jdbctemplate-ref=" JD Bctemplate "/> ...

Implementation Class Code:

@Transactional public
class Userscorerepositoryimpl implements Userscorerepository {

	private jdbctemplate JdbcTemplate;

	@Override public
	userscore getusersocore (String userno) {

	final userscore us = new Userscore ();
	...
	return us;
	}
	...

}
OK, okay. The above implementation of a simple transaction management. Now a little bit more about @transactional.
In the configuration file,,<tx:annotation-driven> automatically uses the transaction manager with the name TransactionManager by default.

Therefore, if the defined transaction manager name is TransactionManager, then you can use <tx:annotation-driven/> directly. As follows:

<!--configuration transaction manager-->
	<bean id= "TransactionManager"
		class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "
		p:datasource-ref=" DataSource ">
	</bean>

	<!--enables scanning for @Transactional annotations-->
	<tx:annotation-driven/>
<tx:annotation-driven> There are four attributes as follows,
Mode: Specifies how the Spring transaction management framework creates notification beans. The available values are proxy and ASPECTJ. The former is the default value, which means that the notification object is a JDK proxy, and the latter indicates that spring AOP uses ASPECTJ to create a proxy proxy-target-class: If true,spring will create subclasses to broker the business class; The interface-based proxy is used. (If you are using a subclass proxy, you need to add the Cglib.jar class library to the Classpath) Order: If the business class needs to weave other slices in addition to the transaction slice, this property allows you to control how the transaction slice is woven into the destination connection point. Transaction-manager: Specifies a reference to an existing Platformtransaction manager bean that notifies you @Transactional the location of the callout using the reference
@Transactional annotations can be annotated on classes and methods, and can be annotated on defined interfaces and interface methods.
If we annotate the @transactional annotation on the interface, it leaves a hidden danger: Because annotations cannot be inherited, the @transactional annotations that are annotated in the business interface are not inherited by the business implementation class. Therefore, there may be cases where the transaction does not start. So, spring suggests that we put @transaction annotations on the implementation class.
The @transactional annotation on the method overrides the @transactional on the class.


Using a different transaction manager
If we want to use multiple transaction managers (primarily for multiple data sources) in a program, we can do this in the following ways:
Service Code:

public class Multitxservice {
	@Transactional (' tran_1 ') public
	void addtest (int id) {
		
	}
	@Transactional ("Tran_2")
	public void deletetest (int id) {
		
	}

}
The Applicationcontext.xml configuration is as follows:

<bean id= "tran_1"
	class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"
	p: datasource-ref= "DataSource" >
	<qualifier value= "tran_1"/>
</bean>
<bean id= "Tran_2"
	class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"
	p:datasource-ref= "datasource ">
	<qualifier value=" tran_2 "/>
</bean>
Through the above code, each transaction is bound to its own independent data source for its own transaction management. We can optimize the code below to customize a note that is bound to a particular transaction manager and then identify it directly using this custom annotation:

@Target ({elementtype.method,elementtype.type})
@Retention (retentionpolicy.runtime)
@Transactional (" Tran_1 ") public
@interface customertransactional {

}
Used in service code:
...
Use the transaction manager named Tran_1 @CustomerTransactional public
void Addtest (String str) {
	
}
...


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.