欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring的事務(wù)控制實現(xiàn)方法

 更新時間:2022年07月29日 09:10:51   作者:興奮の大公猴  
這篇文章主要為大家詳細介紹了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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在啟動后臺 jar包時,使用指定的 application.yml操作

    在啟動后臺 jar包時,使用指定的 application.yml操作

    這篇文章主要介紹了在啟動后臺 jar包時,使用指定的 application.yml操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java中過濾器 (Filter) 和 攔截器 (Interceptor)的使用

    Java中過濾器 (Filter) 和 攔截器 (Interceptor)的使用

    這篇文章主要介紹了Java中過濾器 (Filter) 和 攔截器 (Interceptor)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • Java算法之位圖的概念和實現(xiàn)詳解

    Java算法之位圖的概念和實現(xiàn)詳解

    這篇文章主要介紹了Java算法之位圖的概念和實現(xiàn)詳解,位圖可以利用每一位來對應(yīng)一個值,比如可以利用int類型的數(shù)去存儲0~31這個集合的數(shù)字,如果該集合內(nèi)的數(shù)字存在,則把對應(yīng)的位設(shè)置位1默認為0,需要的朋友可以參考下
    2023-10-10
  • 詳解SpringBoot之集成Spring AOP

    詳解SpringBoot之集成Spring AOP

    本篇文章主要介紹了詳解SpringBoot之集成Spring AOP,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • @Transactional注解異常報錯之多數(shù)據(jù)源詳解

    @Transactional注解異常報錯之多數(shù)據(jù)源詳解

    這篇文章主要介紹了@Transactional注解異常報錯之多數(shù)據(jù)源詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Sping中如何處理@Bean注解bean同名的問題

    Sping中如何處理@Bean注解bean同名的問題

    這篇文章主要介紹了Sping中如何處理@Bean注解bean同名的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法

    使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法

    這篇文章主要介紹了使用Idea或Datagrip導(dǎo)入excel數(shù)據(jù)的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Java中Comparator升序降序的具體使用

    Java中Comparator升序降序的具體使用

    本文主要介紹了Java中Comparator升序降序的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • Java實現(xiàn)二維碼功能的實例代碼

    Java實現(xiàn)二維碼功能的實例代碼

    今天這篇文章,主要是利用Java實現(xiàn)二維碼功能,本文思路清晰,需要的朋友參考下
    2017-02-02
  • Spring注解之@Value詳解

    Spring注解之@Value詳解

    這篇文章主要介紹了Spring注解之@Value詳解,@Value可以修飾屬性、方法、參數(shù)、注釋類型,編譯器會將?@Value注解的信息保留在?.class?文件中,并且能被虛擬機讀取,需要的朋友可以參考下
    2024-01-01

最新評論