Spring的事務(wù)控制實現(xiàn)方法
Spring的事務(wù)控制實現(xiàn),供大家參考,具體內(nèi)容如下
提示:這里使用的是xml的方式配置事務(wù)的
前言
例:當銀行轉(zhuǎn)賬的時候,如果轉(zhuǎn)賬和收款的一方出現(xiàn)問題,那么這次的轉(zhuǎn)賬則不成功,此處如果沒有事務(wù)管理,那么可能出現(xiàn)一方已經(jīng)轉(zhuǎn)賬成功,另一方卻沒有收款的問題。為了避免此問題,應(yīng)當使用到事務(wù)管理。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、Spring聲明式事務(wù)控制
示例:
二、使用步驟
1.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:aop="http://www.springframework.org/schema/aop"
? ? ? ?xmlns:tx="http://www.springframework.org/schema/tx"
? ? ? ?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/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
? ? ? ?http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
? ? <!-- ?1.加載jdbc.properties ?-->
? ? <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
? ? <!-- ?2.配置數(shù)據(jù)源對象 ?-->
? ? <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
? ? ? ? <property name="driverClassName" value="${driver}"></property>
? ? ? ? <property name="url" value="${url}"></property>
? ? ? ? <property name="username" value="${user}"></property>
? ? ? ? <property name="password" value="${password}"></property>
? ? </bean>
? ? <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>
? ? <bean id="accountDao" class="com.codejams.dao.impl.AccountDaoImpl">
? ? ? ? <property name="jdbcTemplate" ref="jdbcTemplate"/>
? ? </bean>
<!-- ? ?<bean class="com.codejams.controller.DemoTest">-->
<!-- ? ? ? ?<property name="accountService" ref="accountService"/>-->
<!-- ? ?</bean>-->
? ? <!--目標對象 ?內(nèi)部的方法就是切點-->
? ? <bean id="accountService" class="com.codejams.service.impl.AccountServiceImpl">
? ? ? ? <property name="accountDao" ref="accountDao"/>
? ? </bean>
? ? <!--配置平臺事務(wù)管理器-->
? ? <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>
? ? <!--通知 ?事務(wù)的增強-->
? ? <tx:advice id="txAdvice" transaction-manager="transactionManager">
? ? ? ? <!--設(shè)置事務(wù)的屬性信息的-->
? ? ? ? <tx:attributes>
? ? ? ? ? ? <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
? ? ? ? ? ? <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
? ? ? ? ? ? <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
? ? ? ? ? ? <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
? ? ? ? ? ? <tx:method name="*"/>
? ? ? ? </tx:attributes>
? ? </tx:advice>
? ? <!--配置事務(wù)的aop織入-->
? ? <aop:config>
? ? ? ? <aop:pointcut id="txPointcut" expression="execution(* com.codejams.service.impl.*.*(..))"/>
? ? ? ? <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
? ? </aop:config>
</beans>
2.AccountDaoImpl代碼
代碼如下(示例):
package com.codejams.dao.impl;
import com.codejams.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDaoImpl implements AccountDao {
? ? private JdbcTemplate jdbcTemplate;
? ? public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
? ? ? ? this.jdbcTemplate = jdbcTemplate;
? ? }
? ? public void out(String outMan, double money) {
? ? ? ? jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
? ? }
? ? public void in(String inMan, double money) {
? ? ? ? jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
? ? }
}3.AccountServiceImpl代碼
代碼如下(示例):
package com.codejams.service.impl;
import com.codejams.dao.AccountDao;
import com.codejams.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
public class AccountServiceImpl implements AccountService {
? ? //@Autowired
? ? private AccountDao accountDao;
? ? public void setAccountDao(AccountDao accountDao) {
? ? ? ? this.accountDao = accountDao;
? ? }
? ? public void transfer(String outMan, String inMan, double money) {
? ? ? ? accountDao.out(outMan,money);
? ? ? ? int i = 1/0;
? ? ? ? accountDao.in(inMan,money);
? ? }
}結(jié)果展示

手動在這里制造一個異常

此時數(shù)據(jù)庫的狀態(tài)為兩人均為5000

執(zhí)行后,可以看見這里報了異常,并且數(shù)據(jù)庫的兩人的money都沒有更改


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在啟動后臺 jar包時,使用指定的 application.yml操作
這篇文章主要介紹了在啟動后臺 jar包時,使用指定的 application.yml操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java中過濾器 (Filter) 和 攔截器 (Interceptor)的使用
這篇文章主要介紹了Java中過濾器 (Filter) 和 攔截器 (Interceptor)的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
@Transactional注解異常報錯之多數(shù)據(jù)源詳解
這篇文章主要介紹了@Transactional注解異常報錯之多數(shù)據(jù)源詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法
這篇文章主要介紹了使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

