Spring3 and Hibernate4 annotation-type declarative transaction management

Source: Internet
Author: User

Annotated declarative transaction management refers to the method of specifying transaction management in the Java source code using annotations.

Applicationcontext-common.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.1.xsd "> <bean id=" propertyconfigurer "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Location "value="/ Web-inf/server.properties "></property> &Lt;/bean> <!--data Source--<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" des troy-method= "Close" > <property name= "Driverclass" ><value>${jdbc.driverClassName}</value> </property> <property name= "Jdbcurl" ><value>${jdbc.url}</value></property> < Property name= "User" ><value>${jdbc.username}</value></property> <property name= "Password" ><value>${jdbc.password}</value></property> <property name= "Minpoolsize" ><value >10</value></property> <property name= "Maxpoolsize" ><value>20</value></ property> <property name= "MaxIdleTime" ><value>600</value></property> <property name= "Acquireincrement" ><value>2</value></property> <property name= "Maxstatements" >< value>0</value></property> <property name= "Maxstatementsperconnection"><value>20</value></property> <property name= "Initialpoolsize" ><value>20</ value></property> <property name= "Idleconnectiontestperiod" ><value>600</value></ property> <property name= "acquireretryattempts" ><value>30</value></property> < Property Name= "Breakafteracquirefailure" ><value>false</value></property> <property name= " Testconnectiononcheckout "><value>false</value></property> </bean> <!--session
		Factory--<bean id= "sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name= "DataSource" ref= "DataSource"/> <property name= "Packagestoscan" value= "Com.ipt.model"/> & Lt;property name= "Hibernateproperties" > <props> <prop key= "Hibernate.dialect" >org.hibernate.dialect . mysqldialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.format_sql" >false</prop> <prop key= "Hibernate.use_sql_comments" > false</prop> <prop key= "Hibernate.cache.use_second_level_cache" >false</prop> </props> &lt ;/property> </bean> <bean id= "TransactionManager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <!--start Annotations to manage transactions with annotations--<tx:annotation-driven transaction-manager=" TransactionManager "/> </beans>

Add annotations in service code

Package Com.ipt.service.impl;

Import java.util.List;

Import org.springframework.transaction.annotation.Transactional;

Import Com.ipt.dao.IPersonDao;
Import Com.ipt.model.Person;
Import Com.ipt.service.PersonService;

@Transactional public
class Personserviceimpl implements Personservice {
	private Ipersondao Persondao;

	@Override
	@Transactional (Rollbackfor={exception.class}) public
	void Addperson () {person
		u1 = new Person ();
		U1.setname ("Ying Ma");
		U1.setage ();
		Persondao.save (U1);

		Person U2 = new Person ();
		U2.setname ("Wei June");
		U2.setage (a);
		Persondao.save (U2);
	}

	@Override
	@Transactional (readonly=true) public
	list<person> getpersons () {
		List<person > persons = Persondao.query ("From person",
				new object[] {});
		return persons;
	}

	Public Ipersondao Getpersondao () {
		return persondao;
	}

	public void Setpersondao (Ipersondao persondao) {
		This.persondao = Persondao;
	}
}

A little explanation

@Transactional default propagation=propagation.required, so you can omit it.

@Transactional (Readonly=true)

Readonly=true indicates that the annotated method or class simply reads the data.

Readonly=false indicates that the annotated method or class is adding and deleting data.

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.