标签:
概念
流程
spring声明式事务处理的步骤
package cn.itcast.spring.jdbc.transaction.bean; public class Person { private Long pid; private String pname; private String psex; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getPsex() { return psex; } public void setPsex(String psex) { this.psex = psex; } }异常类
package cn.itcast.spring.jdbc.transaction.exception; public class MyException { public void myException(Throwable ex){ System.out.println(ex.getMessage()); } }
package cn.itcast.spring.jdbc.transaction.sh.dao; import java.util.List; import cn.itcast.spring.jdbc.transaction.bean.Person; public interface PersonDao { public void savePerson(); public List<Person> getPerson(); }
package cn.itcast.spring.jdbc.transaction.sh.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.AbstractPlatformTransactionManager; import cn.itcast.spring.jdbc.transaction.bean.Person; public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao{ @Override public List<Person> getPerson() { // TODO Auto-generated method stub return this.getJdbcTemplate().query("select * from person", new RowMapper() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub Person person = new Person(); person.setPid(rs.getLong("pid")); person.setPname(rs.getString("pname")); person.setPsex(rs.getString("psex")); return person; } }); } @Override public void savePerson() { // TODO Auto-generated method stub this.getJdbcTemplate().update("insert into person(pname,psex) values(‘a‘,‘a‘)"); } }
package cn.itcast.spring.jdbc.transaction.sh.service; import java.util.List; import cn.itcast.spring.jdbc.transaction.bean.Person; public interface PersonService { public void savePerson(); public List<Person> getPerson(); }
package cn.itcast.spring.jdbc.transaction.sh.service; import java.util.List; import org.springframework.transaction.PlatformTransactionManager; import cn.itcast.spring.jdbc.transaction.bean.Person; import cn.itcast.spring.jdbc.transaction.sh.dao.PersonDao; public class PersonServiceImpl implements PersonService{ private PersonDao personDao; public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public List<Person> getPerson() { // TODO Auto-generated method stub return this.personDao.getPerson(); } @Override public void savePerson() { // TODO Auto-generated method stub this.personDao.savePerson(); int a = 1/0; } }
package cn.itcast.spring.jdbc.transaction.test; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.spring.jdbc.transaction.sh.service.PersonService; public class TransactionTest { public static ApplicationContext context; static{ context = new ClassPathXmlApplicationContext("cn/itcast/spring/jdbc/transaction/config/applicationContext.xml"); } @Test public void testDataSource(){ DataSource dataSource = (DataSource)context.getBean("dataSource"); System.out.println(dataSource); } @Test public void testPesonDao(){ context.getBean("personDao"); } @Test public void testPersonService(){ PersonService personService = (PersonService)context.getBean("personService"); personService.savePerson(); } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="personDao" class="cn.itcast.spring.jdbc.transaction.sh.dao.PersonDaoImpl"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="personService" class="cn.itcast.spring.jdbc.transaction.sh.service.PersonServiceImpl"> <property name="personDao"> <ref bean="personDao" /> </property> </bean> <bean id="myException" class="cn.itcast.spring.jdbc.transaction. exception.MyException"></bean> <!-- (2)使用事务管理器,spring提供的类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- 通知 1、告诉spring容器,采用什么样的方法处理事务 <bean id="transactionManager"> 2、告诉spring容器,对那些方法是用事务以及目标方法应该采用什么样的事务处理策略 这一行就体现了声明式事务 <tx:method name="save*" read-only="false" /> 事务肯定不能自己来写,这样的话就相当于程序猿还是要知道事务,还是要知道类和方法,还是得具体操作 而在这里,程序猿只是负责声明一些而已 --> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <!-- name规定方法 对save开头的方法进行事务管理 isolation 默认值为DEFAULT 隔离机制,默认不改,所以可填可不填 propagation 传播机制 REQUIRED 99%用required read-only false是读写事务,true是只读事务 应该spring容器使用的session肯定是和程序猿使用的是同一个,那么spring容器就只能获取当前线程的session来使用 而使用当前线程session进行操作的前提是所有CRUD必需在是事务下运行,所以就没有说有没有事务的区分 只有只读或者可读写的区分 --> <tx:method name="save*" read-only="false" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.itcast.spring.jdbc.transaction.sh.service.*.*(..))" id="perform" /> <!-- 把事务加在切片中 --> <aop:advisor advice-ref="tx" pointcut-ref="perform" /> <aop:aspect ref="myException"> <aop:after-throwing method="myException" pointcut-ref="perform" throwing="ex" /> </aop:aspect> </aop:config> </beans>
标签:
原文地址:http://my.oschina.net/u/2356176/blog/469146