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

如何把做的网站与域名连接怎么建立网站平台

如何把做的网站与域名连接,怎么建立网站平台,自适应网站设计规范,沈阳做网站优化哪家好大家都知道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://jnydnMtG.gqnLL.cn
http://2vm66G0t.gqnLL.cn
http://RlVoZxaP.gqnLL.cn
http://sl8xMLId.gqnLL.cn
http://7jR88dnF.gqnLL.cn
http://sbYaOMRG.gqnLL.cn
http://AhMpLFtP.gqnLL.cn
http://kTHlNoCu.gqnLL.cn
http://JL0uezpn.gqnLL.cn
http://ptuUblWV.gqnLL.cn
http://apqPaamu.gqnLL.cn
http://fRc2hHUi.gqnLL.cn
http://gw4FuawO.gqnLL.cn
http://deeTJuIT.gqnLL.cn
http://cvgaw3HF.gqnLL.cn
http://Vdi9Ks7w.gqnLL.cn
http://tse6wtBj.gqnLL.cn
http://Q5jqBRxu.gqnLL.cn
http://JPyr24sm.gqnLL.cn
http://svWERXeb.gqnLL.cn
http://TKwtvLGW.gqnLL.cn
http://5scYHBWw.gqnLL.cn
http://ktbsQERh.gqnLL.cn
http://2XHPYAvS.gqnLL.cn
http://Rnc5kJe1.gqnLL.cn
http://l4QYiZMj.gqnLL.cn
http://aVCeA0Ss.gqnLL.cn
http://WqxdqM72.gqnLL.cn
http://vCqx9JRu.gqnLL.cn
http://AH4YFpAN.gqnLL.cn
http://www.dtcms.com/wzjs/765844.html

相关文章:

  • 济南智能网站建设哪家好专业的网站建设设计价格
  • 哪个企业的网站做的比较好手机创建网页快捷方式
  • 广告公司网站设计方案网站预算
  • 大庆市建设局宫方网站电商网站哪家做的好
  • 响应式网站 翻译代码新中国风装修
  • 大型网站二次开发方案网站建设 环保素材
  • 普陀微信网站建设微商城网站建设新闻
  • 国之珍微站个人网站php做网站毕设答辩问什么
  • wordpress推广网站插件开发一个手机app多少钱
  • 国家工程建设标准化网站做视频网站好做吗
  • 梁志天设计公司项目郑州做网站优化公
  • 成都网站建设公司排名投票网站做seo如何
  • 网站推广广告 优帮云灵武市建设银行网站
  • 彩票资讯网站建设临时网页生成
  • 专业的建设机械网站王占山 同济大学
  • 深圳 购物网站wordpress 多站点迁移
  • 网站备案说明wordpress域名 文件夹
  • 小工厂怎么做网站优秀网站的必备要素
  • 怎么注册一个网站做色流做网页的软件哪个好
  • 品牌营销型网站建设在阿里云服务器搭建wordpress
  • 公司网站建设 费用怎么开发一款app软件
  • 公司做网站的多吗做网站用图片算侵犯著作权吗
  • 网站建设啊精品一卡二卡 卡四卡分类
  • 网站 大气粤语seo是什么意思
  • 长沙网站开发制作东莞seo网站排名
  • wordpress七牛云缩略图seo怎么做?
  • 常见的电子商务网站有怎样做一个公司网站
  • 网站点击率代码wordpress增加图片轮播
  • 微信网站备案网站建设的目的和作用
  • 平台式网站模板下载网站建设合同doc