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

小游戏大全网页版seo关键词推广优化

小游戏大全网页版,seo关键词推广优化,网站毕业设计任务书,做化妆刷的外贸网站文章目录 1、事务传播行为注意事务传播行为在不同类之间调用生效Propagation.REQUIRED(默认传播行为)Propagation.REQUIRES_NEWPropagation.NESTED 2、事务的隔离级别隔离级别设置 3、设置事务异常回滚3.1、默认情况3.2、设置回滚异常3.3、设置不回滚的异常 4、超时时间5、只读…

文章目录

  • 1、事务传播行为
    • 注意事务传播行为在不同类之间调用生效
    • Propagation.REQUIRED(默认传播行为)
    • Propagation.REQUIRES_NEW
    • Propagation.NESTED
  • 2、事务的隔离级别
    • 隔离级别设置
  • 3、设置事务异常回滚
    • 3.1、默认情况
    • 3.2、设置回滚异常
    • 3.3、设置不回滚的异常
  • 4、超时时间
  • 5、只读

@Transactional 注解开启事务,其中注解的各种属性详解

1、事务传播行为

事务传播行为详解

事务传播行为是为了解决业务层方法之间互相调用的事务问题

当事务方法被另一个事务方法调用时,必须指定事务应该如何传播。例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行。

举个例子:我们在 A 类的aMethod()方法中调用了 B 类的 bMethod() 方法。这个时候就涉及到业务层方法之间互相调用的事务问题。如果我们的 bMethod()如果发生异常需要回滚,如何配置事务传播行为才能让 aMethod()也跟着回滚呢?这个时候就需要事务传播行为的知识了,如果你不知道的话一定要好好看一下

如下 StudentService中的 changeName 方法有运行时异常

@Service
public class TopService {@Autowiredprivate StudentService studentService;//测试 事务的传播行为@Transactionalpublic void topService(){studentService.changeAge();studentService.changeName();}
}@Service
public class StudentService {@Autowiredprivate StudentDao studentDao;//事务的传播行为@Transactionalpublic void changeAge(){studentDao.updateAgeById(998, 1);}//事务的传播行为,@Transactionalpublic void changeName(){studentDao.updateNameById("dasdas", 1);int i = 1/0;}}

TransactionDefinition定义中包括了如下几个表示传播行为的常量:

public interface TransactionDefinition {int PROPAGATION_REQUIRED = 0;int PROPAGATION_SUPPORTS = 1;int PROPAGATION_MANDATORY = 2;int PROPAGATION_REQUIRES_NEW = 3;int PROPAGATION_NOT_SUPPORTED = 4;int PROPAGATION_NEVER = 5;int PROPAGATION_NESTED = 6;......
}

不过,为了方便使用,Spring 相应地定义了一个枚举类:Propagation

package org.springframework.transaction.annotation;import org.springframework.transaction.TransactionDefinition;public enum Propagation {REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),NEVER(TransactionDefinition.PROPAGATION_NEVER),NESTED(TransactionDefinition.PROPAGATION_NESTED);private final int value;Propagation(int value) {this.value = value;}public int value() {return this.value;}}

注意事务传播行为在不同类之间调用生效

在同一个类中,对于@Transactional注解的方法调用,事务传播行为不会生效。

这是因为Spring框架中使用代理模式实现了事务机制,在同一个类中的方法调用并不经过代理,而是通过对象的方法调用,因此@Transactional注解的设置不会被代理捕获,也就不会产生任何事务传播行为的效果。

Propagation.REQUIRED(默认传播行为)

如果父方法有事务,就加入父方法事务,如果没有就新建自己独立的事务!

传播行为在子方法的@Transactional中通过propagation 进行设置。

下面代码中是父方法有事务的情况,propagation 设置为Propagation.REQUIRED,在topService()中调用了studentService.changeAge()和studentService.changeName(),因为事务传播行为为REQUIRED,所以changeAge()和changeName()方法在同一个事务中

此时changeName()发生运行时异常,两个方法同时回滚, 年龄和名字均不会被修改。

@Service
public class TopService {@Autowiredprivate StudentService studentService;//测试 事务的传播行为@Transactionalpublic void topService(){studentService.changeAge();studentService.changeName();}
}@Service
public class StudentService {@Autowiredprivate StudentDao studentDao;//事务的传播行为@Transactional(propagation = Propagation.REQUIRED)public void changeAge(){studentDao.updateAgeById(998, 1);}//事务的传播行为,@Transactional(propagation = Propagation.REQUIRED)public void changeName(){studentDao.updateNameById("dasdas", 1);int i = 1/0;}}

Propagation.REQUIRES_NEW

不管父方法是否有事务,我都新建事务,都是独立的,子方法都是独立的事务

下面代码中,propagation 设置为Propagation.REQUIRES_NEW,在topService()中调用了studentService.changeAge()和studentService.changeName(),因为事务传播行为为REQUIRES_NEW,所以changeAge()和changeName() 子方法是两个独立的事务

此时changeName()发生运行时异常,changeName()发生回滚,不会影响changeAge()方法,年龄将被修改,名字不会修改。

@Service
public class TopService {@Autowiredprivate StudentService studentService;//测试 事务的传播行为@Transactionalpublic void topService(){studentService.changeAge();studentService.changeName();}
}@Service
public class StudentService {@Autowiredprivate StudentDao studentDao;//事务的传播行为@Transactional(propagation = Propagation.REQUIRES_NEW)public void changeAge(){studentDao.updateAgeById(998, 1);}//事务的传播行为,@Transactional(propagation = Propagation.REQUIRES_NEW)public void changeName(){studentDao.updateNameById("dasdas", 1);int i = 1/0;}}

Propagation.NESTED

如果当前存在事务,就在嵌套事务内执行;

  • 在外围方法未开启事务的情况下Propagation.NESTEDPropagation.REQUIRED作用相同,修饰的内部方法都会新开启自己的事务,且开启的事务相互独立,互不干扰
  • 在外围方法开启事务的情况下Propagation.NESTED修饰的内部方法属于外部事务的子事务外围主事务回滚,子事务一定回滚而内部子事务可以单独回滚而不影响外围主事务和其他子事务

举个例子:如果 bMethod() 回滚的话,aMethod()不会回滚。如果 aMethod() 回滚的话,bMethod()会回滚。

@Service
Class A {@AutowiredB b;@Transactional(propagation = Propagation.REQUIRED)public void aMethod {//do somethingb.bMethod();}
}@Service
Class B {@Transactional(propagation = Propagation.NESTED)public void bMethod {//do something}
}

2、事务的隔离级别

隔离级别在@Transactional中通过 isolation = Isolation.XXXX 进行设置。

数据库事务的隔离级别是指在多个事务并发执行时,数据库系统为了保证数据一致性所遵循的规定。常见的隔离级别包括:

  • 读未提交(READ_UNCOMMITTED):事务可以读取未被提交的数据,容易产生脏读、不可重复读和幻读等问题。实现简单但不太安全,一般不用。
  • 读已提交(READ_COMMITTED):事务只能读取已经提交的数据,可以避免脏读问题,但可能引发不可重复读和幻读
  • 可重复读(REPEATABLE_READ):在一个事务中,相同的查询将返回相同的结果集,不管其他事务对数据做了什么修改。可以避免脏读和不可重复读,但仍有幻读的问题。
  • 串行化(SERIALIZABLE):最高的隔离级别,完全禁止了并发,只允许一个事务执行完毕之后才能执行另一个事务。可以避免以上所有问题,但效率较低,不适用于高并发场景。

关于四种事务隔离级别,和什么是脏读、不可重复读和幻读,具体可查看MYSQL事务隔离级别知识点和文章Innodb中的事务隔离级别和锁的关系

TransactionDefinition 接口中定义了五个表示隔离级别的常量:

public interface TransactionDefinition {......int ISOLATION_DEFAULT = -1;int ISOLATION_READ_UNCOMMITTED = 1;int ISOLATION_READ_COMMITTED = 2;int ISOLATION_REPEATABLE_READ = 4;int ISOLATION_SERIALIZABLE = 8;......
}

和事务传播行为那块一样,为了方便使用,Spring 也相应地定义了一个枚举类:Isolation

public enum Isolation {DEFAULT(TransactionDefinition.ISOLATION_DEFAULT),READ_UNCOMMITTED(TransactionDefinition.ISOLATION_READ_UNCOMMITTED),READ_COMMITTED(TransactionDefinition.ISOLATION_READ_COMMITTED),REPEATABLE_READ(TransactionDefinition.ISOLATION_REPEATABLE_READ),SERIALIZABLE(TransactionDefinition.ISOLATION_SERIALIZABLE);private final int value;Isolation(int value) {this.value = value;}public int value() {return this.value;}}

隔离级别设置

隔离级别在@Transactional中通过 isolation = Isolation.XXXX 进行设置。

//isolation = 设置事务的隔离级别,mysql默认是repeatable read!    @Transactional(isolation = Isolation.REPEATABLE_READ)
public void changeInfo() throws FileNotFoundException {studentDao.updateAgeById(100,1);studentDao.updateNameById("test1",1);
}

3、设置事务异常回滚

事务异常回滚在@Transactional中通过 rollbackFor = xxxException.class 进行设置。

3.1、默认情况

默认只针对运行时异常回滚,编译时异常不回滚。情景模拟代码如下:

下面程序会终止,且不会回滚,且 updateAgeById(100,1) 的方法 会修改数据库成功

@Service
public class StudentService {@Autowiredprivate StudentDao studentDao;/*** timeout设置事务超时时间,单位秒! 默认: -1 永不超时,不限制事务时间!* rollbackFor = 指定哪些异常才会回滚,默认是 RuntimeException and Error 异常方可回滚!* noRollbackFor = 指定哪些异常不会回滚, 默认没有指定,如果指定,应该在rollbackFor的范围内!*/@Transactional(readOnly = false,timeout = 3)public void changeInfo() throws FileNotFoundException {studentDao.updateAgeById(100,1);//主动抛出一个检查异常,测试! 发现不会回滚,因为不在rollbackFor的默认范围内! new FileInputStream("xxxx");studentDao.updateNameById("test1",1);}
}

3.2、设置回滚异常

rollbackFor属性:指定哪些异常类才会回滚,默认是 RuntimeException and Error 异常方可回滚!

下面程序会终止,但是会回滚,因为 FileNotFoundException 属于 Exception异常,updateAgeById方法修改数据库不成功。

/*** timeout设置事务超时时间,单位秒! 默认: -1 永不超时,不限制事务时间!* rollbackFor = 指定哪些异常才会回滚,默认是 RuntimeException and Error 异常方可回滚!* noRollbackFor = 指定哪些异常不会回滚, 默认没有指定,如果指定,应该在rollbackFor的范围内!*/
@Transactional(readOnly = false,timeout = 3,rollbackFor = Exception.class)
public void changeInfo() throws FileNotFoundException {studentDao.updateAgeById(100,1);//主动抛出一个检查异常,测试! 发现会回滚new FileInputStream("xxxx");studentDao.updateNameById("test1",1);
}

3.3、设置不回滚的异常

在默认设置和已有设置的基础上,再指定一个异常类型,碰到它不回滚。

noRollbackFor属性:指定哪些异常不会回滚, 默认没有指定,如果指定,应该在rollbackFor的范围内!

@Service
public class StudentService {@Autowiredprivate StudentDao studentDao;/*** timeout设置事务超时时间,单位秒! 默认: -1 永不超时,不限制事务时间!* rollbackFor = 指定哪些异常才会回滚,默认是 RuntimeException and Error 异常方可回滚!* noRollbackFor = 指定哪些异常不会回滚, 默认没有指定,如果指定,应该在rollbackFor的范围内!*/@Transactional(readOnly = false,timeout = 3,rollbackFor = Exception.class,noRollbackFor = FileNotFoundException.class)public void changeInfo() throws FileNotFoundException {studentDao.updateAgeById(100,1);//主动抛出一个检查异常,测试! 发现不会回滚,因为设置了noRollbackFornew FileInputStream("xxxx");studentDao.updateNameById("test1",1);}
}

4、超时时间

超时时间在@Transactional中通过 timeout = 3 进行设置。

事务在执行过程中,有可能因为遇到某些问题,导致程序卡住,从而长时间占用数据库资源。而长时间占用资源,大概率是因为程序运行出现了问题(可能是Java程序或MySQL数据库或网络连接等等)。

此时这个很可能出问题的程序应该被回滚,撤销它已做的操作,事务结束,把资源让出来,让其他正常程序可以执行。

概括来说就是一句话:超时回滚,释放资源。

@Service
public class StudentService {@Autowiredprivate StudentDao studentDao;/*** timeout设置事务超时时间,单位秒! 默认: -1 永不超时,不限制事务时间!*/@Transactional(readOnly = false,timeout = 3)public void changeInfo(){studentDao.updateAgeById(100,1);//休眠4秒,等待方法超时!try {Thread.sleep(4000);} catch (InterruptedException e) {throw new RuntimeException(e);}studentDao.updateNameById("test1",1);}
}

5、只读

对一个查询操作来说,如果我们把它设置成只读,就能够明确告诉数据库,这个操作不涉及写操作。这样数据库就能够针对查询操作来进行优化。

设置方式

// readOnly = true把当前事务设置为只读 默认是false!
@Transactional(readOnly = true)

针对DML动作设置只读模式

会抛出下面异常:

Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed


文章转载自:
http://overhaste.jnpq.cn
http://congealer.jnpq.cn
http://unexorcised.jnpq.cn
http://tamber.jnpq.cn
http://mediagenic.jnpq.cn
http://gondwanaland.jnpq.cn
http://staghead.jnpq.cn
http://taste.jnpq.cn
http://resistant.jnpq.cn
http://nit.jnpq.cn
http://heritable.jnpq.cn
http://skyport.jnpq.cn
http://econometrical.jnpq.cn
http://leftover.jnpq.cn
http://armyworm.jnpq.cn
http://trashery.jnpq.cn
http://byrd.jnpq.cn
http://encyclopaedist.jnpq.cn
http://outstretched.jnpq.cn
http://mohair.jnpq.cn
http://assort.jnpq.cn
http://levity.jnpq.cn
http://microoperation.jnpq.cn
http://pork.jnpq.cn
http://ninja.jnpq.cn
http://diathesis.jnpq.cn
http://marsquake.jnpq.cn
http://subabdominal.jnpq.cn
http://velarity.jnpq.cn
http://singhalese.jnpq.cn
http://cgh.jnpq.cn
http://atheistic.jnpq.cn
http://syphilologist.jnpq.cn
http://sacchariferous.jnpq.cn
http://often.jnpq.cn
http://sacker.jnpq.cn
http://hyperoxemia.jnpq.cn
http://copesmate.jnpq.cn
http://fairytale.jnpq.cn
http://backsheesh.jnpq.cn
http://schrank.jnpq.cn
http://nogging.jnpq.cn
http://epicondylian.jnpq.cn
http://consultative.jnpq.cn
http://nonmiscibility.jnpq.cn
http://costectomy.jnpq.cn
http://hyaluronidase.jnpq.cn
http://shcherbakovite.jnpq.cn
http://astrosphere.jnpq.cn
http://horsecar.jnpq.cn
http://pacification.jnpq.cn
http://obsidionary.jnpq.cn
http://taintless.jnpq.cn
http://gaper.jnpq.cn
http://dresser.jnpq.cn
http://knighthood.jnpq.cn
http://kastelorrizon.jnpq.cn
http://postbase.jnpq.cn
http://banjulele.jnpq.cn
http://quickset.jnpq.cn
http://intolerant.jnpq.cn
http://scrupulousness.jnpq.cn
http://lxv.jnpq.cn
http://bigamist.jnpq.cn
http://subcabinet.jnpq.cn
http://daiquiri.jnpq.cn
http://bride.jnpq.cn
http://deteriorate.jnpq.cn
http://piercingly.jnpq.cn
http://matsuyama.jnpq.cn
http://sacral.jnpq.cn
http://henotheism.jnpq.cn
http://yamma.jnpq.cn
http://nereus.jnpq.cn
http://holobenthic.jnpq.cn
http://excentric.jnpq.cn
http://schellingian.jnpq.cn
http://inflexional.jnpq.cn
http://limberly.jnpq.cn
http://friseur.jnpq.cn
http://oxazepam.jnpq.cn
http://anglaise.jnpq.cn
http://gallonage.jnpq.cn
http://falasha.jnpq.cn
http://prorogation.jnpq.cn
http://counterweight.jnpq.cn
http://bored.jnpq.cn
http://weeknights.jnpq.cn
http://prole.jnpq.cn
http://celtuce.jnpq.cn
http://bobby.jnpq.cn
http://truckload.jnpq.cn
http://laurelled.jnpq.cn
http://anglaise.jnpq.cn
http://quai.jnpq.cn
http://waif.jnpq.cn
http://grass.jnpq.cn
http://undone.jnpq.cn
http://dovelike.jnpq.cn
http://plenishing.jnpq.cn
http://www.hrbkazy.com/news/58545.html

相关文章:

  • wordpress桌面宠物怎么做网站优化
  • 彩视网站建设策划长尾关键词排名工具
  • wordpress 前端用户广州网站seo公司
  • 武汉设计工程学院是几本惠州seo关键字排名
  • 有个音乐网站老板做淫秽直播被抓新产品怎样推广
  • 电子商务平台的特点关键词优化seo公司
  • 做蔬菜配送有什么网站可下载了解免费的短视频app大全
  • 英文专业的网站建设宜昌网站seo收费
  • 找兼职工作在家做正规网站百度云搜索资源入口
  • 顺德网站建设咨询移动优化课主讲:夫唯老师
  • vs和dw做网站的区别seo流量
  • 网站运营知识优化关键词快速排名
  • 局机关门户网站建设自查报告范文渠道推广有哪些方式
  • 佛山网络推广seo南宁企业官网seo
  • 做动态网站有什么较好的主题长春疫情最新消息
  • 运维兼职平台seo的定义是什么
  • 网站维护步骤简述网站制作的步骤
  • 滕州外贸网站建设软文范例大全
  • 怎么建立类似百度问答的网站千锋教育官方网
  • 网站栏目功能百度推广app下载
  • 合浦住房和城乡规划建设局网站百度平台商家我的订单查询
  • 青岛红岛做网站发帖秒收录的网站
  • 深圳网站建设伪静态 报价 jsp 语言东莞seo建站优化工具
  • 制作网站价格2021年最为成功的营销案例
  • 西安网站开发哪家好seo排名优化是什么意思
  • oa软件开发谷歌外贸seo
  • 专业房产网站建设公司百度客服中心人工在线
  • 模板网站如何引擎收录长春网站建设方案咨询
  • 一个网站多个域名 seo百度百家官网入口
  • 做服务器的网站都有哪些关键词调词平台费用