spring 2.5 事务管理

spring2.5的事物管理,分为两种方式,一是基于注解方式的,而是基于配置文件方式的

一。基于注解方式

Java代码 复制代码
  1. import java.util.List;   
  2.   
  3. import javax.sql.DataSource;   
  4.   
  5. import org.springframework.jdbc.core.JdbcTemplate;   
  6. import org.springframework.transaction.annotation.Propagation;   
  7. import org.springframework.transaction.annotation.Transactional;   
  8.   
  9. import com.mingbai.bean.StudentBean;   
  10. import com.mingbai.service.StudentService;   
  11.   
  12. //将此业务类交由spring的事务管理,这时此方法的某个方法在执行前就开启事务,结束后提交事务   
  13. @Transactional                     
  14. public class StudentServiceImpl implements StudentService {   
  15.     private JdbcTemplate jdbcTemplate;   
  16.   
  17.     public void setDataSource(DataSource dataSource) {   
  18.         this.jdbcTemplate = new JdbcTemplate(dataSource);   
  19.     }   
  20.   
  21. /*  public void delete(Integer sid) {  
  22.         jdbcTemplate.update("delete from student where id=?",   
  23.                 new Object[]{sid}, new int[]{java.sql.Types.INTEGER});  
  24.         throw new RuntimeException("我是异常");  
  25.     }*/  
  26. /*  @Transactional(rollbackFor=Exception.class)  
  27.     public void delete(Integer sid) throws Exception {  
  28.         jdbcTemplate.update("delete from student where id=?",   
  29.                 new Object[]{sid}, new int[]{java.sql.Types.INTEGER});  
  30.         throw new Exception("我是异常");  
  31.     }*/  
  32.     @Transactional(noRollbackFor=RuntimeException.class)   
  33.     public void delete(Integer sid) throws Exception {   
  34.         jdbcTemplate.update("delete from student where id=?",    
  35.                 new Object[]{sid}, new int[]{java.sql.Types.INTEGER});   
  36.         throw new RuntimeException("我是运行期异常");//运行期例外(unchecked exception)事务默认回滚   
  37.     }   
  38.        
  39.     @Transactional(propagation=Propagation.NOT_SUPPORTED)   
  40.     public List<StudentBean> getAllStudent() {   
  41.         return jdbcTemplate.query("select * from student"new StudentRowMapper());   
  42.     }   
  43.        
  44.     public StudentBean getStudent(Integer sid) {   
  45.         StudentBean sb = (StudentBean)jdbcTemplate.queryForObject("select * from student where id=?"new Object[]{sid}, new int[]{java.sql.Types.INTEGER}, new StudentRowMapper());   
  46.         return sb;   
  47.     }   
  48.   
  49.     public void save(StudentBean student) {   
  50.         jdbcTemplate.update("insert into student(name,password) values(?,?)",    
  51.                 new Object[]{student.getName(),student.getPassword()}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});   
  52.     }   
  53.   
  54.     public void update(StudentBean student) {   
  55.         jdbcTemplate.update("update student set name=? where id=?",    
  56.                 new Object[]{student.getName(),5}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});   
  57.     }   
  58.   
  59. }   
  60.   
  61. 数据库链接信息放在类路径的文件jdbc.properties中   
  62.   
  63. <PRE class=java name="code">driverClassName=org.gjt.mm.mysql.Driver   
  64. url=jdbc\:mysql\://localhost\:3306/test   
  65. username=root   
  66. password=   
  67. initialSize=1  
  68. maxActive=500  
  69. maxIdle=2  
  70. minIdle=1</PRE>   
  71. <BR>   
  72. <BR>此时的配置文件需要注明采用注解方式   
  73. <BR>   
  74. <BR><PRE class=java name="code"><?xml version="1.0" encoding="UTF-8"?>   
  75. <beans xmlns="http://www.springframework.org/schema/beans"  
  76.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  77.        xmlns:context="http://www.springframework.org/schema/context"    
  78.        xmlns:aop="http://www.springframework.org/schema/aop"  
  79.        xmlns:tx="http://www.springframework.org/schema/tx"  
  80.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  81.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  82.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  83.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  84.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">             
  85.         
  86.         
  87.      <context:property-placeholder location="classpath:jdbc.properties"/><!-- 属性文件,占位符 -->   
  88.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  89.         <property name="driverClassName" value="${driverClassName}"/>   
  90.         <property name="url" value="${url}"/>   
  91.         <property name="username" value="${username}"/>   
  92.         <property name="password" value="${password}"/>   
  93.          <!-- 连接池启动时的初始值 -->   
  94.          <property name="initialSize" value="${initialSize}"/>   
  95.          <!-- 连接池的最大值 -->   
  96.          <property name="maxActive" value="${maxActive}"/>   
  97.          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->   
  98.          <property name="maxIdle" value="${maxIdle}"/>   
  99.          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->   
  100.          <property name="minIdle" value="${minIdle}"/>   
  101.      </bean>   
  102.         
  103.         
  104.      <!--    
  105.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  106.         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>   
  107.         <property name="url" value="jdbc:mysql://localhost:3306/test"/>   
  108.         <property name="username" value="root"/>   
  109.         <property name="password" value=""/>   
  110.          <property name="initialSize" value="1"/>   
  111.          <property name="maxActive" value="500"/>   
  112.          <property name="maxIdle" value="1"/>   
  113.          <property name="minIdle" value="1"/>   
  114.      </bean>   
  115.       -->   
  116.     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  117.        <property name="dataSource" ref="dataSource"/>   
  118.     </bean>   
  119.     <!-- 采用注解事务方式-->   
  120.     <tx:annotation-driven transaction-manager="txManager"/>   
  121.        <bean id="studentService" class="com.mingbai.service.impl.StudentServiceImpl">   
  122.        <property name="dataSource"  ref="dataSource"></property>   
  123.     </bean>   
  124. </beans></PRE>   
  125. <BR>  
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mingbai.bean.StudentBean;
import com.mingbai.service.StudentService;

//将此业务类交由spring的事务管理,这时此方法的某个方法在执行前就开启事务,结束后提交事务
@Transactional                  
public class StudentServiceImpl implements StudentService {
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

/*	public void delete(Integer sid) {
		jdbcTemplate.update("delete from student where id=?", 
				new Object[]{sid}, new int[]{java.sql.Types.INTEGER});
		throw new RuntimeException("我是异常");
	}*/
/*	@Transactional(rollbackFor=Exception.class)
	public void delete(Integer sid) throws Exception {
		jdbcTemplate.update("delete from student where id=?", 
				new Object[]{sid}, new int[]{java.sql.Types.INTEGER});
		throw new Exception("我是异常");
	}*/
	@Transactional(noRollbackFor=RuntimeException.class)
	public void delete(Integer sid) throws Exception {
		jdbcTemplate.update("delete from student where id=?", 
				new Object[]{sid}, new int[]{java.sql.Types.INTEGER});
		throw new RuntimeException("我是运行期异常");//运行期例外(unchecked exception)事务默认回滚
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<StudentBean> getAllStudent() {
		return jdbcTemplate.query("select * from student", new StudentRowMapper());
	}
	
	public StudentBean getStudent(Integer sid) {
		StudentBean sb = (StudentBean)jdbcTemplate.queryForObject("select * from student where id=?", new Object[]{sid}, new int[]{java.sql.Types.INTEGER}, new StudentRowMapper());
		return sb;
	}

	public void save(StudentBean student) {
		jdbcTemplate.update("insert into student(name,password) values(?,?)", 
				new Object[]{student.getName(),student.getPassword()}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});
	}

	public void update(StudentBean student) {
		jdbcTemplate.update("update student set name=? where id=?", 
				new Object[]{student.getName(),5}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
	}

}

数据库链接信息放在类路径的文件jdbc.properties中

Java代码 复制代码
  1. driverClassName=org.gjt.mm.mysql.Driver   
  2. url=jdbc\:mysql\://localhost\:3306/test   
  3. username=root   
  4. password=   
  5. initialSize=1  
  6. maxActive=500  
  7. maxIdle=2  
  8. minIdle=1  
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc\:mysql\://localhost\:3306/test
username=root
password=
initialSize=1
maxActive=500
maxIdle=2
minIdle=1


此时的配置文件需要注明采用注解方式

Java代码 复制代码
  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.         
  14.      <context:property-placeholder location="classpath:jdbc.properties"/><!-- 属性文件,占位符 -->   
  15.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  16.         <property name="driverClassName" value="${driverClassName}"/>   
  17.         <property name="url" value="${url}"/>   
  18.         <property name="username" value="${username}"/>   
  19.         <property name="password" value="${password}"/>   
  20.          <!-- 连接池启动时的初始值 -->   
  21.          <property name="initialSize" value="${initialSize}"/>   
  22.          <!-- 连接池的最大值 -->   
  23.          <property name="maxActive" value="${maxActive}"/>   
  24.          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->   
  25.          <property name="maxIdle" value="${maxIdle}"/>   
  26.          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->   
  27.          <property name="minIdle" value="${minIdle}"/>   
  28.      </bean>   
  29.         
  30.         
  31.      <!--    
  32.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  33.         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>   
  34.         <property name="url" value="jdbc:mysql://localhost:3306/test"/>   
  35.         <property name="username" value="root"/>   
  36.         <property name="password" value=""/>   
  37.          <property name="initialSize" value="1"/>   
  38.          <property name="maxActive" value="500"/>   
  39.          <property name="maxIdle" value="1"/>   
  40.          <property name="minIdle" value="1"/>   
  41.      </bean>   
  42.       -->   
  43.     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  44.        <property name="dataSource" ref="dataSource"/>   
  45.     </bean>   
  46.     <!-- 采用注解事务方式-->   
  47.     <tx:annotation-driven transaction-manager="txManager"/>   
  48.        <bean id="studentService" class="com.mingbai.service.impl.StudentServiceImpl">   
  49.        <property name="dataSource"  ref="dataSource"></property>   
  50.     </bean>   
  51. </beans>  
<?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:context="http://www.springframework.org/schema/context" 
       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/context http://www.springframework.org/schema/context/spring-context-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">          
     
     
     <context:property-placeholder location="classpath:jdbc.properties"/><!-- 属性文件,占位符 -->
	 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="${driverClassName}"/>
	    <property name="url" value="${url}"/>
	    <property name="username" value="${username}"/>
	    <property name="password" value="${password}"/>
	     <!-- 连接池启动时的初始值 -->
		 <property name="initialSize" value="${initialSize}"/>
		 <!-- 连接池的最大值 -->
		 <property name="maxActive" value="${maxActive}"/>
		 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
		 <property name="maxIdle" value="${maxIdle}"/>
		 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
		 <property name="minIdle" value="${minIdle}"/>
	 </bean>
     
     
     <!-- 
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
	    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
	    <property name="username" value="root"/>
	    <property name="password" value=""/>
		 <property name="initialSize" value="1"/>
		 <property name="maxActive" value="500"/>
		 <property name="maxIdle" value="1"/>
		 <property name="minIdle" value="1"/>
	 </bean>
	  -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  	   <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 采用注解事务方式-->
    <tx:annotation-driven transaction-manager="txManager"/>
       <bean id="studentService" class="com.mingbai.service.impl.StudentServiceImpl">
       <property name="dataSource"  ref="dataSource"></property>
    </bean>
</beans>






一。基于xml配置方式

Java代码 复制代码
  1. import java.util.List;   
  2.   
  3. import javax.sql.DataSource;   
  4.   
  5. import org.springframework.jdbc.core.JdbcTemplate;   
  6.   
  7. import com.mingbai.bean.StudentBean;   
  8. import com.mingbai.service.StudentService;   
  9.   
  10.                      
  11. public class StudentServiceImpl2 implements StudentService {   
  12.     private JdbcTemplate jdbcTemplate;   
  13.   
  14.     public void setDataSource(DataSource dataSource) {   
  15.         this.jdbcTemplate = new JdbcTemplate(dataSource);   
  16.     }   
  17.   
  18.   
  19.     public void delete(Integer sid) throws Exception {   
  20.         jdbcTemplate.update("delete from student where id=?",    
  21.                 new Object[]{sid}, new int[]{java.sql.Types.INTEGER});   
  22.         throw new RuntimeException("我是运行期异常");//运行期例外(unchecked exception)事务默认回滚   
  23.     }   
  24.        
  25.     public List<StudentBean> getAllStudent() {   
  26.         return jdbcTemplate.query("select * from student"new StudentRowMapper());   
  27.     }   
  28.        
  29.     public StudentBean getStudent(Integer sid) {   
  30.         StudentBean sb = (StudentBean)jdbcTemplate.queryForObject("select * from student where id=?"new Object[]{sid}, new int[]{java.sql.Types.INTEGER}, new StudentRowMapper());   
  31.         return sb;   
  32.     }   
  33.   
  34.     public void save(StudentBean student) {   
  35.         jdbcTemplate.update("insert into student(name,password) values(?,?)",    
  36.                 new Object[]{student.getName(),student.getPassword()}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});   
  37.     }   
  38.   
  39.     public void update(StudentBean student) {   
  40.         jdbcTemplate.update("update student set name=? where id=?",    
  41.                 new Object[]{student.getName(),5}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});   
  42.     }   
  43.   
  44. }  
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.mingbai.bean.StudentBean;
import com.mingbai.service.StudentService;

                  
public class StudentServiceImpl2 implements StudentService {
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}


	public void delete(Integer sid) throws Exception {
		jdbcTemplate.update("delete from student where id=?", 
				new Object[]{sid}, new int[]{java.sql.Types.INTEGER});
		throw new RuntimeException("我是运行期异常");//运行期例外(unchecked exception)事务默认回滚
	}
	
	public List<StudentBean> getAllStudent() {
		return jdbcTemplate.query("select * from student", new StudentRowMapper());
	}
	
	public StudentBean getStudent(Integer sid) {
		StudentBean sb = (StudentBean)jdbcTemplate.queryForObject("select * from student where id=?", new Object[]{sid}, new int[]{java.sql.Types.INTEGER}, new StudentRowMapper());
		return sb;
	}

	public void save(StudentBean student) {
		jdbcTemplate.update("insert into student(name,password) values(?,?)", 
				new Object[]{student.getName(),student.getPassword()}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR});
	}

	public void update(StudentBean student) {
		jdbcTemplate.update("update student set name=? where id=?", 
				new Object[]{student.getName(),5}, new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
	}

}




配置文件

Java代码 复制代码
  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.         
  14.      
  15.         
  16.      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  17.         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>   
  18.         <property name="url" value="jdbc:mysql://localhost:3306/test"/>   
  19.         <property name="username" value="root"/>   
  20.         <property name="password" value=""/>   
  21.          <property name="initialSize" value="1"/>   
  22.          <property name="maxActive" value="500"/>   
  23.          <property name="maxIdle" value="1"/>   
  24.          <property name="minIdle" value="1"/>   
  25.      </bean>   
  26.     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  27.        <property name="dataSource" ref="dataSource"/>   
  28.     </bean>   
  29.   
  30.     <!-- 采用基于xml方式 -->   
  31.     <tx:advice id="txadvice" transaction-manager="txManager">   
  32.         <tx:attributes>   
  33.             <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>   
  34.             <tx:method name="*" />   
  35.         </tx:attributes>   
  36.     </tx:advice>   
  37.     <aop:config>   
  38.         <aop:pointcut id="transactionPointcut" expression="execution(* com.mingbai.service..*.*(..))"/>   
  39.         <aop:advisor advice-ref="txadvice" pointcut-ref="transactionPointcut"/>   
  40.     </aop:config>   
  41.             
  42.         
  43.     <bean id="studentService" class="com.mingbai.service.impl.StudentServiceImpl">   
  44.        <property name="dataSource"  ref="dataSource"></property>   
  45.     </bean>   
  46. </beans>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值