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

用哪个软件做网站广州百度推广优化

用哪个软件做网站,广州百度推广优化,dreawever如何做本地网站,做网站需要展示工厂么大家都知道Spring的声明式事务在多线程当中会失效,来看一下如下案例。 按照如下方式,b()方法抛出异常,由于父子线程导致事务失效,a()会成功插入,但是b()不会。 因此成功插入一条数据,事务失效。 Component public class UserServ…

大家都知道Spring的声明式事务在多线程当中会失效,来看一下如下案例。
按照如下方式,b()方法抛出异常,由于父子线程导致事务失效,a()会成功插入,但是b()不会。
因此成功插入一条数据,事务失效。

@Component
public class UserServiceImpl implements UserService{@Transactionalpublic void a(){jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(18,'张三','北京')");UserService userService = (UserService)AopContext.currentProxy();Thread thread = new Thread(()->{userService.b();})}@Transactionalpublic void b(){jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(19,'张三','北京')");throw new RuntimeException();}}

这里就需要了解一下嵌套方法事务的传播行为是怎么实现的?
在这里插入图片描述
如图所示,如果b()方法是在子线程当中的,因为ThreadLocal不是同一个因此子线程又创建了一个事务。由于是各用各的事务所以事务b就会回滚,而事务a的数据就会插入成功。
要保证这种父子线程中的事务传播,则在创建子线程后把父线程中的事务取出来再设置进去。
进行如下改造后保证两个线程使用同一个事务。
在这里插入图片描述
那么接下来的问题就是如何获取外层的connection以及如何设置到子线程的ThreadLocal中。
这里就需要看一下源码了
需要看这个类org.springframework.jdbc.datasource.DataSourceTransactionManager找到doBegin()方法

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;Connection con = null;try {if (!txObject.hasConnectionHolder() ||txObject.getConnectionHolder().isSynchronizedWithTransaction()) {Connection newCon = obtainDataSource().getConnection();if (logger.isDebugEnabled()) {logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction");}txObject.setConnectionHolder(new ConnectionHolder(newCon), true);}txObject.getConnectionHolder().setSynchronizedWithTransaction(true);con = txObject.getConnectionHolder().getConnection();Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);txObject.setPreviousIsolationLevel(previousIsolationLevel);txObject.setReadOnly(definition.isReadOnly());if (con.getAutoCommit()) {txObject.setMustRestoreAutoCommit(true);if (logger.isDebugEnabled()) {logger.debug("Switching JDBC Connection [" + con + "] to manual commit");}// 这里开启事务con.setAutoCommit(false);}prepareTransactionalConnection(con, definition);txObject.getConnectionHolder().setTransactionActive(true);int timeout = determineTimeout(definition);if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {txObject.getConnectionHolder().setTimeoutInSeconds(timeout);}// 这里将连接存入到ThreadLocalif (txObject.isNewConnectionHolder()) {TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder());}}catch (Throwable ex) {if (txObject.isNewConnectionHolder()) {DataSourceUtils.releaseConnection(con, obtainDataSource());txObject.setConnectionHolder(null, false);}throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);}
}

最终改造

@Component
public class UserServiceImpl implements UserService{@Transactionalpublic void a(){ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource);jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(18,'张三','北京')");UserService userService = (UserService)AopContext.currentProxy();Thread thread = new Thread(()->{// 绑定主线程的connection到子线程TransactionSynchronizationManager.bindResource(dataSource,connectionHolder);userService.b();})}@Transactionalpublic void b(){jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(19,'张三','北京')");throw new RuntimeException();}}
http://www.dtcms.com/wzjs/242179.html

相关文章:

  • 北京自己怎么做网站如何免费推广网站
  • 如何让自己做的博客网站上线企业推广方法
  • 现在个人做网站或者app还有收益seo排名是什么意思
  • 河南专业网站建设公司排名广州市运营推广公司
  • 烟台建网站公司免费建站哪个最好
  • 做盗版小说网站百度指数搜索热度
  • 网站空白页黑链企业培训计划
  • 网站设计怎么做才好看小红书推广
  • 成都网站建设 erp免费打广告网站
  • 网站建设-部署与发布石家庄疫情
  • 一级做爰片a视频网站4seo蜘蛛池
  • 网站开发大概需要多少钱如何推广公司
  • 郑州官网seo推广seoul是哪个国家
  • 少儿美术专业网站做课件拓客软件哪个好用
  • 深圳专业网站建设网站超级外链
  • 自建营销型企业网站黄页网络的推广网站有哪些软件
  • 商城类的网站一般怎么做温岭网络推广
  • 番禺做网站平台游戏推广渠道
  • 怎样做网站的反链广告联盟哪个比较好
  • 网站的策划做推广市场营销活动策划方案
  • 免费简历制作软件app英文网站seo
  • 简单大气食品农业网站源码google搜索引擎入口
  • 临沂网站维护墨猴seo排名公司
  • 网站的优化和推广方案病毒营销案例
  • 公司在百度做网站找谁免费软文推广平台
  • 嘉兴做网络推广的公司怎么样优化关键词排名
  • 外贸出口流程图详细怎么快速优化关键词排名
  • 文交所网站开发免费个人网站申请
  • 网站专题怎么做呢徐州百度seo排名优化
  • 张家口网站建设哪里好品牌营销策划培训课程