1. 程式人生 > >Spring框架入門-----事務

Spring框架入門-----事務

  • 事務的回滾
    • 在這裡我用一個數據庫的使用者扣錢,加錢的例子:
      我隨便建了一個數據庫,裡面有兩個使用者:
      在這裡插入圖片描述

然後在pom.xml裡面寫上一個資料庫資原始檔的操作:`

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
        <!-- 這裡是資料庫資原始檔的位置 -->
	<context:property-placeholder location="classpath:properties/jdbc.properties"/>
	
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName"
			value="${jdbcDriver}" />
		<property name="url" value="${jdbcUrl}" />
		<property name="username" value="${jdbcUsername}" />
		<property name="password" value="${jdbcPassword}" />
	</bean> 
</beans>

然後一個main方法:

ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("classpath:lesson04/prop.xml");
		DataSource bean = (DataSource) cac.getBean("dataSource");
		Connection c = bean.getConnection();
		try {
			String sql1 = "UPDATE money SET lmoney=lmoney-50 WHERE id=1";
			String sql2 = "UPDATE money SET lmoney=lmoney+50 WHERE id=2";
			c.setAutoCommit(false);// 設定手動提交
			c.createStatement().execute(sql1);// 自動提交
			int i = 5/0;
			c.createStatement().execute(sql2);
			c.commit();
		} catch (Exception e) {
			// TODO: handle exception
			c.rollback();
		}

還是上面那個例子,註解版在來一發:
xml檔案程式碼
有點多

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
        <!-- 掃描註解 -->
	<context:component-scan
		base-package="com.ps.lesson04"></context:component-scan>
	<context:property-placeholder
		location="lesson04/jdbc.properties"></context:property-placeholder>
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbcDriver}" />
		<property name="url" value="${jdbcUrl}" />
		<property name="username" value="${jdbcUsername}" />
		<property name="password" value="${jdbcPassword}" />
	</bean>
	<!-- jdbc的模板類 通常用於操作 增刪查改 注入資料來源 -->
	<bean id="jdbc"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<!-- 事物管理器 用於提交和回滾 -->
	<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	 <aop:config >
		<!-- 切點一般都是攔截所有的service包 所有介面 所有方法 -->
		<aop:pointcut expression="execution(* com.ps.lesson04.*.transfer*(..))" id="aop"/>
		<aop:advisor pointcut-ref="aop" advice-ref="txAdvice"/>
	</aop:config>
	
	<!-- 通知       一般資料庫的sql操作異常都是操作型異常
		所有定義的事務 執行時異常回滾 檢查異常不會回滾
		rollback-for="你要回滾的檢查異常"
		rollback-for="java.lang.Exception" >>>>>代表所有的檢查異常會回滾
		
		no-rollback-for="什麼樣的執行時異常不回滾"
		no-rollback-for="java.lang.RuntimeException"  >>>>>代表所有的執行異常不會回滾
	 -->
	
	<tx:advice id="txAdvice" transaction-manager="tm">
		<tx:attributes>
		<!-- propagation:事務的傳播特性
			read-only:禁止事務預設false
		 -->
			<tx:method name="transfer*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED"/>
			<!-- 除了上面所有定義的方法 以外都不用事物 -->
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice> 
	

</beans>

這裡的程式碼可以複製,但是裡面的路徑可能就要自己改了