spring事务

现在说下事务

spring提供俩种事务管理方式

1.annotation(注解)

2.xml配置(aop技术)

 

贴下配置文件

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  12.   
  13.     <bean id="dataSource"  
  14.         class="org.apache.commons.dbcp.BasicDataSource"  
  15.         destroy-method="close">  
  16.         <property name="driverClassName"  
  17.             value="org.gjt.mm.mysql.Driver" />  
  18.         <property name="url"  
  19.             value="jdbc:mysql://localhost:3306/test?userUnicode=true&amp;characterEncoding=UTF-8" />  
  20.         <property name="username" value="root" />  
  21.         <property name="password" value="5" />  
  22.         <!-- 连接池启动时的初始值 -->  
  23.         <property name="initialSize" value="1" />  
  24.         <!-- 连接池的最大值 -->  
  25.         <property name="maxActive" value="500" />  
  26.         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->  
  27.         <property name="maxIdle" value="2" />  
  28.         <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->  
  29.         <property name="minIdle" value="1" />  
  30.     </bean>  
  31.   
  32.   
  33.     <!-- 1.注解方式配置事务  -->  
  34.     <bean id="txManager"  
  35.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  36.         <property name="dataSource" ref="dataSource" />  
  37.     </bean>  
  38.   
  39.     <tx:annotation-driven transaction-manager="txManager" />  
  40.   
  41.   
  42.   
  43.     <!-- 2.xml方式配置事务.利用aop技术   
  44.         <aop:config>  
  45.         <aop:pointcut id="transactionPointcut" expression="execution(* com.test.service..*.*(..))"/>  
  46.         <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>  
  47.         </aop:config>   
  48.         <tx:advice id="txAdvice" transaction-manager="txManager">  
  49.         <tx:attributes>  
  50.         <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>  
  51.         <tx:method name="*"/>  
  52.         </tx:attributes>  
  53.         </tx:advice>  
  54.     -->  
  55.     <bean id="personService"  
  56.         class="com.test.service.impl.PersonServiceImpl">  
  57.         <property name="dataSource" ref="dataSource"></property>  
  58.     </bean>  
  59. </beans>  

  

我一般采用的是注解方式,因为比较简单一点.

 

配置文件里声明启用注解事务之后,看下面代码

Java代码   收藏代码
  1. package com.test.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.sql.DataSource;  
  6.   
  7. import org.springframework.jdbc.core.BeanPropertyRowMapper;  
  8. import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;  
  9. import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;  
  10. import org.springframework.transaction.annotation.Propagation;  
  11. import org.springframework.transaction.annotation.Transactional;  
  12.   
  13. import com.test.bean.Person;  
  14. import com.test.service.PersonService;  
  15.   
  16. @Transactional  
  17. public class PersonServiceImpl implements PersonService {  
  18.     private SimpleJdbcTemplate simpleJdbcTemplate;  
  19.   
  20.     public void setDataSource(DataSource dataSource) {  
  21.         this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);  
  22.     }  
  23.   
  24.     // 两种例外  
  25.     // 1.unchecked 默认回滚事务(RuntimeException)  
  26.     // 2.checked 默认不回滚事务(Exception)  
  27.     // 开启事务.并设置unchecked例外不回滚  
  28.   
  29.     @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = RuntimeException.class)  
  30.     public void delete(int id) {  
  31.         simpleJdbcTemplate.update("delete from person  where id=?",  
  32.                 new Object[] { id });  
  33.         throw new RuntimeException("运行期例外");  
  34.     }  
  35.   
  36.     /** 
  37.      * 说下事务的传播属性  
  38.      * REQUIRED (默认)    业务方法需要在事务中运行,如果方法运行的时候已经开启事务,就加入事务,否则创建新的事务 
  39.      * NOT_SUPPORTED    声明方法不需要开启事务.如果已经开启事务后调用该方法.则先挂起该事务,方法执行完毕后,事务恢复 
  40.      * REQUIRES_NEW     不管是否存在事务,都另行开启新的事务.如果先前已经开启事务,挂起原先事务.创建新的事务. 
  41.      *                  新的事务执行结束后,恢复原先事务 
  42.      * MANDATORY    只能在已经存在的事务中执行,不能自行创建事务,如果在没有开启事务的环境下调用,容器抛出例外 
  43.      * SUPPORTS     墙头草,有事务就在事务中执行.没事务就在没事务的环境下执行 
  44.      * NEVER    绝对不能在事务中执行.如果执行.抛出例外  
  45.      * NESTED   如果有事务存在,开启一个新事务嵌套在上层事务中.开启的新事务具有自己独立的回滚点.新事务的回滚不会对外部事务造成影响, 
  46.      *          但是外部事务的提交会直接提交嵌套事务 
  47.      *          如果没有事务存在,则按照REQUIRED属性执行,该属性只对DataSourceTransationManager事务管理器有效 
  48.      */   
  49.     @Transactional(propagation = Propagation.NOT_SUPPORTED)  
  50.     // 不开启事务  
  51.     public Person getPerson(int personId) {  
  52.         return simpleJdbcTemplate.queryForObject(  
  53.                 "select * from person where id=?",  
  54.                 ParameterizedBeanPropertyRowMapper.newInstance(Person.class),  
  55.                 personId);  
  56.     }  
  57.   
  58.     @Transactional(propagation = Propagation.NEVER)  
  59.     // 不能在事务环境下执行  
  60.     @SuppressWarnings("unchecked")  
  61.     public List<Person> getPersons() {  
  62.         String sql = "select * from  person";  
  63.         List persons = simpleJdbcTemplate.getJdbcOperations().query(sql,  
  64.                 new BeanPropertyRowMapper(Person.class));  
  65.         return persons;  
  66.     }  
  67.   
  68.     @Transactional(propagation = Propagation.NEVER)  
  69.     public void save(Person person) {  
  70.         simpleJdbcTemplate.getJdbcOperations().update(  
  71.                 "insert into person(name) values(?)",  
  72.                 new Object[] { person.getName() },  
  73.                 new int[] { java.sql.Types.VARCHAR });  
  74.     }  
  75.     public void update(Person person) {  
  76.         simpleJdbcTemplate.getJdbcOperations().update(  
  77.                 "update person set name=? where id=?",  
  78.                 new Object[] { person.getName(), person.getId() },  
  79.                 new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER });  
  80.     }  
  81. }  

 

大体就是这样.如果想仔细研究请自行学习..

 

 

附件还是下篇发吧..下篇介绍spring整合struts1.x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值