Spring的事務控制
一、事務的概念
事務是一組操作的執行單元,相對於數據庫操作來講,事務管理的是一組SQL指令,比如增加,修改,刪除等,事務的一致性,要求,這個事務內的操作必須全部執行成功,如果在此過程種出現了差錯,比如有一條SQL語句沒有執行成功,那麽這一組操作都將全部回滾
事務特性(ACID)
(1)Atomic(原子性):要麽都成功,要麽都失敗
(2)Consistent(一致性):數據應該不被破壞
(3)Isolate(隔離性):用戶間操作不相混淆
(4)Durable(持久性):永久保存
二、Spring提供的事務控制方式
編程式事務管理
編寫程序式的事務管理可以清楚的定義事務的邊界,可以實現細粒度的事務控制,比如你可以通過程序代碼來控制你的事務何時開始,何時結束等,與後面介紹的聲明式事務管理相比,它可以實現細粒度的事務控制,例如jdbc,hibernate,spring中不提倡使用。
聲明式事務管理
如果你並不需要細粒度的事務控制,你可以使用聲明式事務,在Spring中,你只需要在Spring配置文件中做一些配置,即可將操作納入到事務管理中,解除了和代碼的耦合, 這是對應用代碼影響最小的選擇,從這一點再次驗證了Spring關於AOP的概念。當你不需要事務管理的時候,可以直接從Spring配置文件中移除該設置
三、Spring對事務控制的實現
Spring的聲明式事務管理,核心實現就是基於AOP,但該管理只能進行粗粒度的事務控制,只能給整個方法應用事務而不能對某幾行代碼應用事務(因為AOP攔截的是方法)
下面介紹Spring聲明式事務管理的實現:
步驟1.包括數據源的配置,事務管理器的配置,事務的切面和只讀屬性配置
<?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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 加載jdbc驅動 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <!-- jdbc連接地址 --> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/kk"></property> <!-- 連接數據庫的用戶名 --> <property name="user" value="root"></property> <!-- 連接數據庫的密碼 --> <property name="password" value="123456"></property> <!-- 數據庫的初始化連接數 --> <property name="initialPoolSize" value="3"></property> <!-- 數據庫的最大連接數 --> <property name="maxPoolSize" value="10"></property> <!-- 數據庫最多執行的事務 --> <property name="maxStatements" value="100"></property> <!-- 連接數量不夠時每次的增量 --> <property name="acquireIncrement" value="2"></property> </bean> <!-- 創建jdbcTemplate對象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"> </property> </bean> <bean id="personDao" class="com.jyk.spring.transaction.PersonDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="personService" class="com.jyk.spring.transaction.PersonService"> <property name="personDao" ref="personDao"> </property> </bean> <!-- 配置事務管理器類 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 只讀指定為false表示對數據的操作是讀取,報異常後不用回滾 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 5.3 Aop配置: 攔截哪些方法(切入點表表達式) + 應用上面的事務增強配置 --> <aop:config> <aop:pointcut expression="execution(* com.jyk.spring.transaction.PersonService.operateMethod(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
步驟2.編寫業務接口和實現
package com.jyk.spring.transaction; public class PersonService { private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void operateMethod(Person person) { personDao.addPerson(person); //人為制造異常方便測試回滾 int a = 1/0; //personDao.addPerson(person); } }
package com.jyk.spring.transaction; import org.springframework.jdbc.core.JdbcTemplate; public class PersonDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void addPerson(Person person) { String sql = "insert into aa(id,name) values (?,?)"; jdbcTemplate.update(sql,person.getId(),person.getName()); } }
四、Spring的事務註解式使用
使用註解實現Spring的聲明式事務管理,更加簡單快捷,準備工作如下
(1)必須引入Aop相關的jar文件
(2)bean.xml中指定註解方式實現聲明式事務管理以及應用的事務管理器類
(3)在需要添加事務控制的地方,寫上: @Transactional
@Transactional註解:
(1)應用事務的註解
(2)定義到方法上:當前方法應用spring的聲明式事務
(3)定義到類上:當前類的所有的方法都應用Spring聲明式事務管理;
(4)定義到父類上:當執行父類的方法時候應用事務。
步驟1.配置數據源,事務管理器和開啟事務註解自動掃描
<?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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 加載jdbc驅動 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <!-- jdbc連接地址 --> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/kk"></property> <!-- 連接數據庫的用戶名 --> <property name="user" value="root"></property> <!-- 連接數據庫的密碼 --> <property name="password" value="123456"></property> <!-- 數據庫的初始化連接數 --> <property name="initialPoolSize" value="3"></property> <!-- 數據庫的最大連接數 --> <property name="maxPoolSize" value="10"></property> <!-- 數據庫最多執行的事務 --> <property name="maxStatements" value="100"></property> <!-- 連接數量不夠時每次的增量 --> <property name="acquireIncrement" value="2"></property> </bean> <!-- 創建jdbcTemplate對象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"> </property> </bean> <!-- 配置事務管理器類 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟註解掃描 --> <context:component-scan base-package="com.jyk.spring.annotation.transaction.property"> </context:component-scan> <!-- 註解方式實現事務,指定註解方式實現事務 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
步驟2.編寫業務實現Service層和Dao層
package com.jyk.spring.annotation.transaction.property; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service public class PersonService { @Resource private PersonDao personDao; /* * @Transactional的使用位置 * 1、定義到方法上:當前方法應用的spring的聲明式事務 * 2、定義到類上:當前類的所有方法都應用spring的聲明式事務 * 3、定義到父類上:當執行父類的方法的時候應用Spring的聲明式事務 */ @Transactional( readOnly=false, //讀寫事務,只讀事務不修改任何數據 timeout = -1,//事務的超時時間沒有限制 // noRollbackFor = ArithmeticException.class, //遇到數學異常不回滾 isolation = Isolation.DEFAULT, //事務的隔離級別 propagation = Propagation.REQUIRED //傳播行為 ) public void operateMethod(Person person) { personDao.addPerson(person);//添加用戶 int myexception = 1/0; } }
package com.jyk.spring.annotation.transaction.property; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class PersonDao { @Resource private JdbcTemplate jdbcTemplate; public void addPerson(Person person) { String sql = "insert into aa(id,name) values (?,?)"; jdbcTemplate.update(sql,person.getId(),person.getName()); } }
Spring的事務控制