1. 程式人生 > >spring框架(容器框架之一)事務管理

spring框架(容器框架之一)事務管理

Spring: 容器框架,一站式框架,用於配置bean,並維護bean之間的關係框架,貫穿框架三層。

(本文章屬於個人學習經驗總結,如果有錯誤或描述不當的地方歡迎指出討論)

Spring之事務管理:

一、相關概念

1、什麼是事務:事務就是對一系列的資料庫操作(比如插入多條資料)進行統一的提交或回滾操作,如果插入成功,那麼一起成功,如果中間有一條出現異常,那麼回滾之前的所有操作。

2、事務管理:我們在實際業務場景中,經常會遇到資料頻繁修改讀取的問題。在同一時刻,不同的業務邏輯對同一個表資料進行修改,這種衝突很可能造成資料不可挽回的錯亂,所以我們需要用事務來對資料進行管理。
例子:兩個人同時向一個賬戶裡轉錢、一個人向另外一個人轉錢。

3、併發事務導致的問題
第一類丟失更新:撤銷一個事務時,把其他事務已提交的更新資料覆蓋。
髒讀:一個事務讀取到另一個事務未提交的更新資料。
幻讀也叫虛讀:一個事務執行兩次查詢,第二次結果集包含第一次中沒有或某些行已經被刪除的資料,造成兩次結果不一致,只是另一個事務在這兩次查詢中間插入或刪除了資料造成的。
不可重複讀:一個事務兩次讀取同一行的資料,結果得到不同狀態的結果,中間正好另一個事務更新了該資料,兩次結果相異,不可被信任。

4、JDBC 中是通過Connection物件進行事務管理的,預設是自動提交事務,可以手工將自動提交關閉,通過commit方法進行提交,rollback方法進行回滾,如果不提交,則資料不會真正的插入到資料庫中。
Hibernate中是通過Transaction進行事務管理處理方法與JDBC中類似。
Spring 中也有自己的事務管理機制,一般是使用TransactionMananger進行管理,可以通過Spring的注入來完成此功能。

二、Spring的事務管理方式:

兩種方式:1)程式設計事務管理

                    2)宣告事務管理

                         1、基於xml配置檔案

                         2、基於註解

舉例:搭建轉賬環境(說明事務管理重要和相關操作)(採用非註釋)

第一步:Mysql下建立資料庫表:


第二步:

建立dao類:OrdersDao

package com.sp.dao;

import org.springframework.jdbc.core.JdbcTemplate;
 
public class OrdersDao {
    private JdbcTemplate jdbctemplate ;
	public JdbcTemplate getJdbctemplate() {
		return jdbctemplate;
	}
	public void setJdbctemplate(JdbcTemplate jdbctemplate) {
		this.jdbctemplate = jdbctemplate;
	}
	//向一個賬戶裡轉錢進去
	public 	void lessMoney() {
		String sql = "update acount set salary =salary-? where username =?";
		jdbctemplate.update(sql,1000,"小王");
		
	}
	//向一個賬戶裡轉錢出去
	public void moreMoney() {
		String sql = "update acount set salary =salary+? where username =?";
		jdbctemplate.update(sql,1000,"小馬");
		
	} 
}

建立Service類:

package com.sp.service;

import com.sp.dao.OrdersDao;

public class OrdersService {
      private OrdersDao ordersDao;

	public OrdersDao getOrdersDao() {
		return ordersDao;
	}

	public void setOrdersDao(OrdersDao ordersDao) {
		this.ordersDao = ordersDao;
	}
    public void accountMoney() {
    	ordersDao.lessMoney();
    	int i = 10/0;
    	ordersDao.moreMoney();
    }
}

測試類:

package com.sp.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("bean2.xml");
		OrdersService ordersService = (OrdersService) context.getBean("ordersService");
		//System.out.println(book);
		ordersService.accountMoney();
	}
}

配置xml檔案:
 <!-- 配置連線池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        <!-- 注入裡面的屬性 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="lky123456"></property>
        </bean>
        
        <bean id="ordersService" class="com.sp.service.OrdersService">
        <property name="ordersDao" ref="ordersDao"></property>
        </bean>
        
        <bean id="ordersDao" class="com.sp.dao.OrdersDao">
        <property name="jdbctemplate" ref="jdbctemplate"></property>
        </bean>
        
        <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 配置事務管理器 -->
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource"></property>
        
        
        </bean>
        
        <!-- 配置事務增強 -->
        <tx:advice id="txadvice" transaction-manager="transactionManager">
        <!-- 做事務的操作 -->
           <tx:attributes>
                 <!-- 設定事務方法匹配規則 -->
                <tx:method name="account*"/>
        
           </tx:attributes>
        
        </tx:advice>
         
         <!-- 配置切面 -->
         
         <aop:config>
            <aop:pointcut expression="execution(* com.sp.service.OrdersService.*(..))" id="pointcut1"/>
            
            <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>   
         
         </aop:config>
輸出測試:執行測試類時,如果沒有在xml檔案中配置事務管理器相關資訊,則執行到service類的account方法時,則會出現小王的salary減少1000,小馬的沒有增加1000的結果出現。

如果加上了事務管理器,則會出現資料沒有改變的結果。

說明了Spring事務管理器防止了這樣的事務的發生。

附上:

Spring 配置xml檔案的全約束
( <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.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">
</beans> )


相關推薦

spring框架(容器框架之一)事務管理

Spring: 容器框架,一站式框架,用於配置bean,並維護bean之間的關係框架,貫穿框架三層。 (本文章屬於個人學習經驗總結,如果有錯誤或描述不當的地方歡迎指出討論) Spring之事務管理: 一、相關概念 1、什麼是事務:事務就是對一系列的資料庫操作(比如插入多條資

SSH:Spring框架(宣告式事務管理詳解)

1) 概念:事務 (Transaction) 是訪問並可能更新資料庫中各種資料項的一個程式執行單元 (unit) 。事務通常由高階資料 庫操縱語言或程式語言 (如 SQL , C++ 或 Java ) 書寫的使用者程式的執行所引起, 並用形如 begin transacti

Spring Boot框架-使用 @Transactional 進行事務管理

往資料庫裡存兩個資料,要求一個存失敗另一個也不能成功怎麼做?通過對類進行事務註解,可以做到 @Transactional public void insertTwo(){ Gril girlA = new Gril(); gir

深入學習Spring框架(四)- 事務管理

1.什麼是事務?   事務(Transaction)是一個操作序列。這些操作要麼都做,要麼都不做,是一個不可分割的工作單位,是資料庫環境中的邏輯工作單位。事務是為了保證資料庫的完整性。例如:A給B轉賬,需要先減掉A的賬戶餘額再加到B的賬戶上,這兩個操作是一個整體,不可能扣掉A的錢不給B加上,或者只給B加沒有

Spring 框架基礎(05):事務管理機制,和實現方式

本文原始碼:GitHub·點這裡 || GitEE·點這裡 一、Spring事務管理 1、基礎描述 Spring事務管理的本質就是封裝了資料庫對事務支援的操作,使用JDBC的事務管理機制,就是利用java.sql.Connection物件完成對事務的提交和回滾。 Connection conn = Drive

SSM框架+MySQL資料庫配置事務管理

資料庫事務(Database Transaction) ,是指作為單個邏輯工作單元執行的一系列操作,要麼完全地執行,要麼完全地不執行。 例如銀行轉賬,A賬戶轉100元給B賬戶,正常的流程是A賬戶減掉100元,B賬戶增加100元。如果轉賬失敗的話,不能出現A賬戶已經減掉100元而B賬戶沒有增加10

spring boot配置mybatis和事務管理

生成 很多 -s 順序 south 位置 ron 技術 username spring boot配置mybatis和事務管理 一、spring boot與mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依賴如下: <!-- Spri

Spring整合mybatis時使用事務管理

lang pan lan -s error org 版本 batis java 異常報告:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTi

Spring 使用註解方式進行事務管理

正在 als 也不會 實現 指定 ati nbsp 負責 spring 事務 事務的傳播行為和隔離級別 大家在使用spring的註解式事務管理時,對事務的傳播行為和隔離級別可能有點不知所措,下邊就詳細的介紹下以備方便查閱。 事物註解方式: @Transactional 當標

Spring+JTA+Atomikos+mybatis分散式事務管理

  背景描述:我們平時的工作中用到的Spring事務管理是管理一個數據源的。但是如果對多個數據源進行事務管理該怎麼辦呢?我們可以用JTA和Atomikos結合Spring來實現一個分散式事務管理的功能。 事務(官方解釋):是由一組sql語句組成的“邏輯處理單元”。 事務具有

Spring文件閱讀之事務管理

1.Transaction Management Spring提供了持久層抽象用於事務管理,可以提供如下便利: 穿插多個事務API的持久層模型:Java Transaction API(JTA),JDBC,Hibernate以及Java Persistence API(JPA)。 支援宣告式的事務管理。

Spring 多資料來源 @Transactional 註解事務管理

在 Spring,MyBatis 下兩個資料來源,通過 @Transactional 註解 配置簡單的事務管理 spring-mybatis.xml <!--******************************** data one start *********

Spring之—ApplicationListener 介面及事務管理

想在spring啟動時自動呼叫一些db操作作快取,於是在service實現ApplicationListener 介面。 並重寫onApplicationEvent操作。這樣就實現了要求,程式碼如下: @Service @Scope("singleton") publi

四、spring的JDBC模板和事務管理

Spring的JDBC模板 Spring是JavaEE開發的一站式框架,對各種持久化技術都提供了簡單的模板 ORM持久化技術 模板類 JDBC org.springframework.jdbc.core.JdbcTemplate

Spring 使用註解方式進行事務管理 /==/ Spring分散式事務實現

使用步驟: 步驟一、在spring配置檔案中引入<tx:>名稱空間 <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

Spring的資料庫操作和事務管理

一.概述 spring的資料庫操作:spring是一個優秀的一站式框架,其中涵蓋了很多持久化框架模板物件,如JDBC,hibernate,mybatis物件模板,極大地簡化了資料庫操作。 事務:表示邏輯上的一組操作,這組操作要麼一起成功要麼一起失敗。最經典的就是銀行轉賬業務

Spring JDBC-混合框架事務管理

組合 manager 延遲 發生 required 應用 conf 一個 研究 ? Spring 抽象的 DAO 體系兼容多種數據訪問技術,它們各有特色,各有千秋。 Hibernate 是非常優秀的 ORM 實現方案,但對底層 SQL 的控制不太方便 M

[Spring框架]Spring 事務管理基礎入門總結.

復制 tor junit4 dao img bubuko 說過 應該 pat 前言:在之前的博客中已經說過了數據庫的事務, 不過那裏面更多的是說明事務的一些鎖機制, 今天來說一下Spring管理事務的一些基礎知識. 之前的文章: [數據庫事務與鎖]詳解一: 徹底理解數據庫

Spring 框架系列之事務管理

操作 提交 目標 form 重復 表達 required supports 嵌套事務 1、事務回顧 (1)、什麽是事務: 事務是邏輯上的一組操作,組成這組操作的各個邏輯單元,要麽一起成功,要麽一起失敗。 (2)、事務特性(ACID) 原子性 :強調事務的不可分割 一致性

12 Spring框架 SpringDAO的事務管理

http 定義 public strong 比較 attr on() platform attribute 上一節我們說過Spring對DAO的兩個支持分為兩個知識點,一個是jdbc模板,另一個是事務管理。 事務是數據庫中的概念,但是在一般情況下我們需要將事務提到業務層次