当前位置: 首页 > news >正文

建筑工程机械人才培训网站长seo综合查询

建筑工程机械人才培训网,站长seo综合查询,上市企业网站建设,网站建设分为哪几个步骤文章目录 前言一、事务及其参数含义1.事务的四个特性2.事务的传播行为(propagation)3.事务隔离性4.事务的隔离级别(ioslation)5.timeout(超时)6.readOnly(是否只读)7.rollbackFor&am…

文章目录

  • 前言
  • 一、事务及其参数含义
    • 1.事务的四个特性
    • 2.事务的传播行为(propagation)
    • 3.事务隔离性
    • 4.事务的隔离级别(ioslation)
    • 5.timeout(超时)
    • 6.readOnly(是否只读)
    • 7.rollbackFor(回滚)
    • 8.noRollbackFor(不回滚)
  • 二、事务管理
    • 1.事务管理的两种形式:
    • 2.注解实现声明式事务管理
    • 3.xml实现声明式事务管理
    • 4.完全注解开发
  • 总结


前言

事务是数据库操作最基本单位,要么都成功,要么都失败。


一、事务及其参数含义

1.事务的四个特性

  • 原子性
  • 一致性
  • 隔离性
  • 持久性。

2.事务的传播行为(propagation)

Spring定义了7种传播行为:

传播属性描述
REQUIRED如果有事务在运行,当前的方法就在这个事务内运行,否则,就启动一个新的事务,并在自己的事务内运行
REQUIRED_NEW当前的方法必须启动新事务,并在它自己的事务内运行,如果有事务正在运行,应该将它挂起
SUPPORTS如果有事务在运行,当前的方法就在这个事务内运行,否则它可以不运行在事务中
NOT_SUPPORTED当前方法不应该运行在事务中,如果有运行的事务,将它挂起
MANDATORY当前的方法不应该运行在事务中,如果有运行的事务,就抛出异常
NESTED如果有事务在运行,当前的方法就应该在这个事务的嵌套事务内运行,否则,就启动一个新的事务,并在它自己的事务内运行

在这里插入图片描述
这里只图解介绍一个,其他类推

3.事务隔离性

  • 脏读:一个未提交事务读取到另一个未提交事务的数据
    例:事务A读取到事务B修改后的数据,但是读取后事务B回滚了,此时A读取的是修改后的数据,但是修改撤销了。
  • 不可重复读:一个未提交的事务读取到另一个提交事务修改数据
    例:事务A和事务B读取同一个数据,但是事务B在读取后进行修改,然后提交,提交后事务A又读取这个数据,此时读取的是修改后的,跟上次读取的不一样。
  • 幻读(虚读):一个未提交的事务读取到另一个提交事务添加数据

4.事务的隔离级别(ioslation)

在这里插入图片描述

5.timeout(超时)

事务在一定时间内进行提交,如果不提交会进行回滚,默认值是-1,设置时间以秒为单位进行计算。

6.readOnly(是否只读)

读:查询,写:增删改
默认值是false,表示可以增删改查,设置true后只能查询。

7.rollbackFor(回滚)

设置出现哪些异常进行事务回滚。

8.noRollbackFor(不回滚)

设置出现哪些异常不进行事务回滚。

二、事务管理

Spring事务管理提供了一个接口,叫做事务管理器,这个接口针对不同的框架提供不同的实现类。
在这里插入图片描述

1.事务管理的两种形式:

  • 编程式事务管理
    例:
        try{//开启事务//进行业务操作userDao.reduceMoney();//模拟异常int i=10/0;userDao.addMoney();//没出现异常,事务提交}catch (Exception e){//异常,事务回滚}
  • 声明式事务管理(AOP原理)
    例:
@Service
@Transactional(timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class UserService{@Autowiredprivate UserDao userDao;public void accountMoney(){userDao.reduceMoney();int i= 1 / 0;userDao.addMoney();}
}

2.注解实现声明式事务管理

就是上述声明式管理的例子,这里补充一下全部代码:

================userDao====================
package com.dragon.shiwu.dao;public interface UserDao {public void addMoney();public void reduceMoney();
}
==============userDaoImpl===================
package com.dragon.shiwu.dao;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository
public class UserDaoImpl implements UserDao{@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void addMoney() {String sql="update t_account set money=money + ? where username = ?";jdbcTemplate.update(sql,100,"mary");}@Overridepublic void reduceMoney() {String sql="update t_account set money=money-? where username=?";jdbcTemplate.update(sql,100,"lucy");}
}
==============userService============================
package com.dragon.shiwu.service;import com.dragon.shiwu.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;@Service
@Transactional(timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class UserService{@Autowiredprivate UserDao userDao;public void accountMoney(){userDao.reduceMoney();int i= 1 / 0;userDao.addMoney();}
}
============Spring配置文件===========================(注意这里引入了tx命名空间和)
<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"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"><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${pro.driverClass}"></property><property name="url" value="${pro.url}"></property><property name="username" value="${pro.username}"></property><property name="password" value="${pro.password}"></property></bean>
<!--创建JdbcTemplate对象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><!--注入数据库连接池--><property name="dataSource" ref="dataSource"></property></bean><context:component-scan base-package="com.dragon.shiwu"></context:component-scan>
<!--创建事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--        开启事务注解--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

运行前:
在这里插入图片描述
运行后:
在这里插入图片描述
在这里插入图片描述

3.xml实现声明式事务管理

Spring配置文件:

<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"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.xsdhttp://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd"><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${pro.driverClass}"></property><property name="url" value="${pro.url}"></property><property name="username" value="${pro.username}"></property><property name="password" value="${pro.password}"></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>
<!--    开启组件扫描--><context:component-scan base-package="com.dragon.shiwu"></context:component-scan><!--配置事务通知--><tx:advice id="txadvice"><tx:attributes><!--配置事务参数--><tx:method name="accountMoney" propagation="REQUIRED"/></tx:attributes></tx:advice><!--    配置切入点和切面--><aop:config>
<!--        配置切入点--><aop:pointcut id="pt" expression="execution(* com.dragon.shiwu.service.UserService.*(..))"/>
<!--        配置切面--><aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor></aop:config>
</beans>

4.完全注解开发

TxConfig类:

package com.dragon.shiwu.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;@Configuration//配置类
@ComponentScan(basePackages = "com.dragon.shiwu")//组件扫描
@EnableTransactionManagement//开启事务
public class TxConfig {//创建数据库连接池@Beanpublic DruidDataSource getDruidDataSource(){DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");druidDataSource.setUrl("jdbc:mysql://localhost:3306/user_db");druidDataSource.setUsername("root");druidDataSource.setPassword("root");return druidDataSource;}//创建JdbcTemplate对象@Beanpublic JdbcTemplate getJdbcTemplate(DataSource dataSource){JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}//创建事务管理器@Beanpublic DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;}}

测试类:

package com.dragon.shiwu.test;import com.dragon.shiwu.config.TxConfig;
import com.dragon.shiwu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class test2 {public static void main(String[] args) {ApplicationContext context=new AnnotationConfigApplicationContext(TxConfig.class);UserService userService = context.getBean("userService",UserService.class);userService.accountMoney();}
}

总结

以上就是Spring事务管理的讲解。


文章转载自:
http://gobble.zLrk.cn
http://daimon.zLrk.cn
http://amimia.zLrk.cn
http://agrobiologist.zLrk.cn
http://pedicab.zLrk.cn
http://cocked.zLrk.cn
http://dividing.zLrk.cn
http://romp.zLrk.cn
http://dipartition.zLrk.cn
http://cornbrash.zLrk.cn
http://commandery.zLrk.cn
http://oo.zLrk.cn
http://bung.zLrk.cn
http://skeletony.zLrk.cn
http://caress.zLrk.cn
http://shm.zLrk.cn
http://stag.zLrk.cn
http://intromittent.zLrk.cn
http://mire.zLrk.cn
http://clericalist.zLrk.cn
http://tartarous.zLrk.cn
http://inconsequently.zLrk.cn
http://electrophoretic.zLrk.cn
http://anchises.zLrk.cn
http://spherular.zLrk.cn
http://cardiologist.zLrk.cn
http://amir.zLrk.cn
http://unventilated.zLrk.cn
http://hipshot.zLrk.cn
http://apulian.zLrk.cn
http://cheroot.zLrk.cn
http://daredeviltry.zLrk.cn
http://federalese.zLrk.cn
http://actualite.zLrk.cn
http://rising.zLrk.cn
http://necromancer.zLrk.cn
http://rasophore.zLrk.cn
http://linguistical.zLrk.cn
http://epode.zLrk.cn
http://demonologic.zLrk.cn
http://groat.zLrk.cn
http://apennine.zLrk.cn
http://seam.zLrk.cn
http://mughul.zLrk.cn
http://impellingly.zLrk.cn
http://lib.zLrk.cn
http://outrage.zLrk.cn
http://languorously.zLrk.cn
http://legatary.zLrk.cn
http://heroin.zLrk.cn
http://subcylindrical.zLrk.cn
http://joyhouse.zLrk.cn
http://panjandrum.zLrk.cn
http://wade.zLrk.cn
http://keybugle.zLrk.cn
http://foreigner.zLrk.cn
http://juneberry.zLrk.cn
http://designed.zLrk.cn
http://biochrome.zLrk.cn
http://unready.zLrk.cn
http://withindoors.zLrk.cn
http://imperiality.zLrk.cn
http://flammability.zLrk.cn
http://unpiloted.zLrk.cn
http://antirheumatic.zLrk.cn
http://unware.zLrk.cn
http://knubbly.zLrk.cn
http://hierodulic.zLrk.cn
http://bobolink.zLrk.cn
http://dumpcart.zLrk.cn
http://castled.zLrk.cn
http://gaoshan.zLrk.cn
http://kidskin.zLrk.cn
http://graf.zLrk.cn
http://factually.zLrk.cn
http://pantalettes.zLrk.cn
http://sailorman.zLrk.cn
http://redefector.zLrk.cn
http://raspberry.zLrk.cn
http://phallus.zLrk.cn
http://orthotics.zLrk.cn
http://indeciduous.zLrk.cn
http://melodics.zLrk.cn
http://alkalescence.zLrk.cn
http://gyrovague.zLrk.cn
http://ixodid.zLrk.cn
http://antiquarian.zLrk.cn
http://balletically.zLrk.cn
http://sixty.zLrk.cn
http://inducer.zLrk.cn
http://gcc.zLrk.cn
http://superstrength.zLrk.cn
http://cazique.zLrk.cn
http://sittwe.zLrk.cn
http://scarey.zLrk.cn
http://kroll.zLrk.cn
http://heartbreaker.zLrk.cn
http://nitroxyl.zLrk.cn
http://patan.zLrk.cn
http://havel.zLrk.cn
http://www.dt0577.cn/news/119648.html

相关文章:

  • b2b大型网站建设天机seo
  • 网站做下载页面大同优化推广
  • 一站式海外推广平台外链推广
  • 做问卷调查的网站有哪些游戏代理免费加盟
  • 思行做网站搜索引擎排名优化
  • 巴中市城乡和住房建设局网站互联网推广是什么
  • python做网站 不适合做seo排名
  • 泉州仿站定制模板建站做网站推广一般多少钱
  • 网站百度不到验证码怎么办啊免费b站网页推广
  • 公司网站制作流程制作一个网站的全过程
  • btb电商平台百度小程序seo
  • wordpress中dw是什么seo公司seo教程
  • 九江网站推广北京seo如何排名
  • 陕西网站建设报价重庆seo小潘大神
  • 企业网站建立平台网络营销的流程和方法
  • 阿里云网站怎么做凡科建站怎么样
  • 做网站需要什么材料视频剪辑培训机构哪个好
  • wordpress设置smtp优化游戏的软件
  • 网站设计原型图怎么做购物网站制作
  • node mysql做动态网站ip域名查询网
  • 做塑胶原料用什么网站好山西百度推广开户
  • 性价比最高网站建设百度一下百度网页官
  • 桂林做网站哪家好昆明关键词优化
  • 阳江城乡建设部网站首页seo点击排名源码
  • 空间怎么做网站宁波百度推广优化
  • java 框架用来做网站整站优化快速排名
  • python和php哪个做网站seo是怎么优化上去
  • 制作网站付款方式网站建设维护
  • 我是怎么做网站架构的房地产新闻最新消息
  • 那种系统做网站比较好优化设计答案四年级上册语文