Spring事务管理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.transaction; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import com.dao.IPeopleDao; import com.dao.People; public class TransactionRun { private IPeopleDao peopleDao; public void setPeopleDao(IPeopleDao peopleDao) { this.peopleDao = peopleDao; } public IPeopleDao getPeopleDao() { return peopleDao; } public void run(){ People people=new People(); people.setName("事务管理"); people.setSex("男"); people.setAge(12); Date d=null; try { d = (new SimpleDateFormat("yyyy-mm-dd")).parse("2016-03-07"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } people.setBirthday(d); peopleDao.addPeople(people); System.out.println(peopleDao.getPeopleCount()); //System.out.println(peopleDao.getPeopleName(Integer.MAX_VALUE));//这个会出现异常,因为id为最大的int值的没有 List<People> peoples=peopleDao.findAllPeople(); Iterator<People> it=peoples.iterator(); while(it.hasNext()){ People p=it.next(); System.out.println(p.getName()+"\t"+p.getAge()); } } } package com.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.transaction.TransactionRun; public class TestTransactionRun { public static void main(String[] args){ ApplicationContext contre=new ClassPathXmlApplicationContext("applicationContext.xml"); TransactionRun dr=contre.getBean("transactionRun",TransactionRun.class); dr.run(); } }
前面是测试类
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="jdbc.properties"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="configproperties"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> </bean>
<!-- 前三个bean是取读xml内容注入dataSource -->
<bean id="peopleDao" class="com.dao.IPeopleDaoImpl" depends-on="dataSource" > <property name="dataSource" ref="dataSource" /> </bean> <bean id="jdbcTransactionMessage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 配置事务管理器,注入数据源--> <property name="dataSource"> <ref bean="dataSource"></ref> </property> </bean> <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> <!-- 配置事务传播特性 --> <property name="properties"> <props> <prop key="*">PROPAGATION_REQUIRED</prop><!-- PROPAGATION_REQUIRED如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。 -->
<!--<prop key="run">PROPAGATION_REQUIRED</prop> --> </props> </property> </bean>
<!--配置参与事务的类以及处理事务的过程--> <bean id="transactionRun" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="jdbcTransactionMessage" /> <property name="target"> <bean class="com.transaction.TransactionRun"> <property name="peopleDao" ref="peopleDao"></property> </bean> </property> <property name="transactionAttributeSource" ref="transactionAttributeSource"></property> </bean> </beans>