spring事務隔離級別,Spring事物管理(二)

 2023-10-07 阅读 27 评论 0

摘要:Spring事物處理規則: 運行時異常,默認回滾。 編譯異常,默認提交。 ? 事物案例:購買股票 數據庫腳本 /* SQLyog v10.2 MySQL - 5.6.24 : Database - y2167 ********************************************************************* *//*!40101 SET

Spring事物處理規則:

運行時異常,默認回滾。

編譯異常,默認提交。

?

事物案例:購買股票

數據庫腳本

/*
SQLyog v10.2 
MySQL - 5.6.24 : Database - y2167
*********************************************************************
*//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`y2167` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `y2167`;/*Table structure for table `account` */DROP TABLE IF EXISTS `account`;CREATE TABLE `account` (`aid` INT(11) NOT NULL AUTO_INCREMENT,`aname` VARCHAR(20) DEFAULT NULL,`balance` INT(11) DEFAULT NULL,PRIMARY KEY (`aid`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;/*Data for the table `account` */insert  into `account`(`aid`,`aname`,`balance`) values (1,'xcq',60000);/*Table structure for table `stock` */DROP TABLE IF EXISTS `stock`;CREATE TABLE `stock` (`sid` int(11) NOT NULL AUTO_INCREMENT,`sname` varchar(30) DEFAULT NULL,`count` int(11) DEFAULT NULL,PRIMARY KEY (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*Data for the table `stock` */insert  into `stock`(`sid`,`sname`,`count`) values (1,'股票A',5),(2,'股票B',20),(3,'股票C',30);/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

spring事務隔離級別,?

?

創建DAO和Service層

?

//DAO層package cn.happy.day15tx.dao;/*** Created by Administrator on 2018/3/14.*/
//賬戶
public interface IAccountDAO {public void updateAccount(int aid,int balance,boolean isBuy);
}-------------------------------------------------------------------------public interface IStockDAO {//改變股票數量public void updateIStock(int sid,int count,boolean isBuy);
}-------------------------------------------------------------------------

//DAOImplpackage cn.happy.day15tx.dao.impl;import cn.happy.day15tx.dao.IStockDAO;
import org.springframework.jdbc.core.support.JdbcDaoSupport;/*** Created by Administrator on 2018/3/14.*///賣出
public class StockDaoImpl extends JdbcDaoSupport implements IStockDAO {public void updateIStock(int sid, int count, boolean isBuy) {String sql=null;if(isBuy){//增加股票sql="update Stock set count=count+? where sid=?";}else{//賣出股票sql="update Stock set count=count-? where sid=?";}this.getJdbcTemplate().update(sql,count,sid);}
}-------------------------------------------------------------------------package cn.happy.day15tx.dao.impl;import cn.happy.day15tx.dao.IAccountDAO;
import org.springframework.jdbc.core.support.JdbcDaoSupport;/*** Created by Administrator on 2018/3/14.*///買入
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDAO {public void updateAccount(int aid, int balance, boolean isBuy) {String sql=null;if(isBuy){//購買sql="update Account set balance=balance-? where aid=?";}else{//賣出sql="update Account set balance=balance+? where aid=?";}this.getJdbcTemplate().update(sql,balance,aid);}
}
------------------------------------------------------------------------
Servicepackage cn.happy.day15tx.service;/*** Created by Administrator on 2018/3/14.*/
public interface IStockService {//購買股票public void updateStock(int aid,int balance,int sid,int count) throws Exception, Exception;
}------------------------------------------------------------------------
ServiceImplpackage cn.happy.day15tx.service.impl;import cn.happy.day15tx.dao.IAccountDAO;
import cn.happy.day15tx.dao.IStockDAO;
import cn.happy.day15tx.service.IStockService;
import com.sun.javafx.beans.annotations.Default;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;/*** Created by Administrator on 2018/3/14.*/public class StockServiceImpl implements IStockService {//植入DAO層對象private IAccountDAO accountDAO;private IStockDAO iStockDAO;//使用注解@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)public void updateStock(int aid, int balance, int sid, int count) throws Exception {//創建boolean對象判斷是買入true還是賣出falseboolean isBuy=true;accountDAO.updateAccount(aid,balance,isBuy);//定義一個異常if(true)throw new Exception("出錯了");iStockDAO.updateIStock(sid,count,isBuy);}public IAccountDAO getAccountDAO() {return accountDAO;}public void setAccountDAO(IAccountDAO accountDAO) {this.accountDAO = accountDAO;}public IStockDAO getiStockDAO() {return iStockDAO;}public void setiStockDAO(IStockDAO iStockDAO) {this.iStockDAO = iStockDAO;}
}



-------------------------------------------------------------
entity


package cn.happy.day15tx.entity;

/**
* Created by Administrator on 2018/3/14.
*/
//購票類
public class Account {
private Integer aid;
private String aname;
private Integer balance;

public Integer getAid() {
return aid;
}

public void setAid(Integer aid) {
this.aid = aid;
}

public String getAname() {
return aname;
}

public void setAname(String aname) {
this.aname = aname;
}

public Integer getBalance() {
return balance;
}

public void setBalance(Integer balance) {
this.balance = balance;
}
}



-------------------------------------------------------

package cn.happy.day15tx.entity;

/**
* Created by Administrator on 2018/3/14.
*/
//股票類
public class Stock {
private Integer sid;
private String sname;
private Integer count;

public Integer getSid() {
return sid;
}

public void setSid(Integer sid) {
this.sid = sid;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}
}


?

一、配置xml文件

<!--數據源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--識別jdbc.properties文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--DAO--><bean id="StockDAO" class="cn.happy.day15tx.dao.impl.StockDaoImpl"><property name="dataSource" ref="dataSource"/></bean><bean id="AccountDAO" class="cn.happy.day15tx.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"/></bean><!--service--><bean id="StockService" class="cn.happy.day15tx.service.impl.StockServiceImpl"><property name="accountDAO" ref="AccountDAO"/><property name="iStockDAO" ref="StockDAO"/></bean><!--事物管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--事物控制--><!--方案一--><bean id="stockServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager" ref="transactionManager"/><!--需要控制的類--><property name="target" ref="StockService"/><property name="transactionAttributes"><props><!--控制的方法--><prop key="updateStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-Exception</prop>
          <!--
            ISOLATION_DEFAULT 底層數據庫默認隔離級別
            PROPAGATION_REQUIRED 如果當前沒有事務,就創建一個新事務,如果當前存在事務,就加入該事務
            -Exception,設置回滾(設置異常,只要出現運行時異常就會回滾)
          -->
       </props>
     </property>
  </bean>

  

測試方法

package day15tx;import cn.happy.JDBCTemplate.service.BookService;
import cn.happy.day15tx.service.IStockService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/*** Created by Administrator on 2018/3/3.*/
public class Test20180314 {//事物@Testpublic void Spring(){ApplicationContext ctx=new ClassPathXmlApplicationContext("day15tx.xml");IStockService service=(IStockService)ctx.getBean("stockServiceProxy");try{service.updateStock(1,2000,1,5);}catch (Exception e){e.printStackTrace();}}}

  

?

二、使用注解管理事物

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://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"><!--數據源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--識別jdbc.properties文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--DAO--><bean id="StockDAO" class="cn.happy.day15tx.dao.impl.StockDaoImpl"><property name="dataSource" ref="dataSource"/></bean><bean id="AccountDAO" class="cn.happy.day15tx.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"/></bean><!--service--><bean id="StockService" class="cn.happy.day15tx.service.impl.StockServiceImpl"><property name="accountDAO" ref="AccountDAO"/><property name="iStockDAO" ref="StockDAO"/></bean><!--事物管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--方案二:使用注解管理事物--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>

  

在需要管理的方法上添加注解

 @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)

  

三、使用AspectJ實現事物管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://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"><!--數據源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--識別jdbc.properties文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--DAO--><bean id="StockDAO" class="cn.happy.day15tx.dao.impl.StockDaoImpl"><property name="dataSource" ref="dataSource"/></bean><bean id="AccountDAO" class="cn.happy.day15tx.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"/></bean><!--service--><bean id="StockService" class="cn.happy.day15tx.service.impl.StockServiceImpl"><property name="accountDAO" ref="AccountDAO"/><property name="iStockDAO" ref="StockDAO"/></bean><!--事物管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--方案三:使用aspectJ AOP實現事物管理--><!-- <tx:advice id="advice" transaction-manager="transactionManager"><tx:attributes><tx:method name="updateStock" isolation="DEFAULT" rollback-for="Exception"/></tx:attributes></tx:advice><aop:config><!–切點–><aop:pointcut id="mypoint" expression="execution(* *..day15tx.service.*.*(..))"></aop:pointcut><!–顧問–><aop:advisor advice-ref="advice" pointcut-ref="mypoint"></aop:advisor></aop:config>--></beans>

  

?

轉載于:https://www.cnblogs.com/xuchangqi1/p/8581634.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/125600.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息