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

设计师用的装修设计软件平台seo什么意思

设计师用的装修设计软件,平台seo什么意思,wordpress 发布vr,怎么把自己做的网页生成链接SSM 如何使用 TCC 机制实现分布式事务? 分布式事务是现代分布式系统中必不可少的一部分,而 TCC 机制(Try-Confirm-Cancel)是一种常用的分布式事务处理方式。在 SSM 框架中,我们可以使用 TCC 机制来管理分布式事务。本…

SSM 如何使用 TCC 机制实现分布式事务?

分布式事务是现代分布式系统中必不可少的一部分,而 TCC 机制(Try-Confirm-Cancel)是一种常用的分布式事务处理方式。在 SSM 框架中,我们可以使用 TCC 机制来管理分布式事务。本文将介绍如何在 SSM 框架中使用 TCC 机制实现分布式事务,并提供相应的代码示例。

在这里插入图片描述

什么是 TCC 机制?

TCC 机制是一种分布式事务处理方式,其核心思想是将一个分布式事务拆分成三个阶段:Try、Confirm 和 Cancel。具体来说,TCC 机制将一个分布式事务拆分成以下三个步骤:

  • Try 阶段:在该阶段中,系统会尝试执行分布式事务。如果所有的业务操作都执行成功,则分布式事务进入 Confirm 阶段。如果任何一个业务操作执行失败,则分布式事务进入 Cancel 阶段。
  • Confirm 阶段:在该阶段中,系统会确认执行所有业务操作。如果所有业务操作都执行成功,则分布式事务提交。如果任何一个业务操作执行失败,则分布式事务回滚。
  • Cancel 阶段:在该阶段中,系统会撤销所有业务操作。如果所有业务操作都成功撤销,则分布式事务回滚完成。如果任何一个业务操作无法撤销,则分布式事务无法回滚。

通过 TCC 机制,我们可以对分布式事务进行更加细粒度的控制,从而降低系统出错的概率。

SSM 中如何使用 TCC 机制实现分布式事务?

在 SSM 框架中,我们可以使用 TCC 机制来管理分布式事务。具体来说,我们可以通过 Spring 的声明式事务管理和 Dubbo 的分布式事务管理来实现 TCC 机制。

Spring 的声明式事务管理

在 Spring 的声明式事务管理中,我们可以使用 @Transactional 注解来实现 TCC 机制。具体来说,我们可以将一个分布式事务拆分成以下三个方法:

  • Try 方法:在该方法中,我们会执行所有的业务操作,并将操作结果保存到一个全局事务上下文对象中。
  • Confirm 方法:在该方法中,我们会确认所有的业务操作。如果所有业务操作都执行成功,则会将全局事务上下文对象中的操作结果提交。如果任何一个业务操作执行失败,则会将全局事务上下文对象中的操作结果回滚。
  • Cancel 方法:在该方法中,我们会撤销所有的业务操作。如果所有业务操作都成功撤销,则会将全局事务上下文对象中的操作结果回滚。如果任何一个业务操作无法撤销,则会将全局事务上下文对象中的操作结果置为“未知”状态。

下面是一个使用 Spring 的声明式事务管理实现 TCC 机制的示例代码:

@Service
public class TccService {@Autowiredprivate TccDao tccDao;@Autowiredprivate GlobalTransactionContext globalTransactionContext;@Transactionalpublic void tryMethod() {// 执行业务操作1boolean result1 = tccDao.doSomething1();// 执行业务操作2boolean result2 = tccDao.doSomething2();// 将操作结果保存到全局事务上下文对象中globalTransactionContext.addResult("result1", result1);globalTransactionContext.addResult("result2", result2);}@Transactionalpublic void confirmMethod() {// 确认业务操作1boolean result1 = globalTransactionContext.getResult("result1");if (result1) {tccDao.confirmSomething1();}// 确认业务操作2boolean result2 = globalTransactionContext.getResult("result2");if (result2) {tccDao.confirmSomething2();}// 提交分布式事务globalTransactionContext.commit();}@Transactionalpublic void cancelMethod() {// 撤销业务操作1boolean result1 = globalTransactionContext.getResult("result1");if (result1) {tccDao.cancelSomething1();}// 撤销业务操作2boolean result2 = globalTransactionContext.getResult("result2");if (result2) {tccDao.cancelSomething2();}// 回滚分布式事务globalTransactionContext.rollback();}
}

在上面的代码中,我们定义了一个 TccService 类,并使用 @Transactional 注解将 tryMethod()confirmMethod()cancelMethod() 方法标记为需要参与分布式事务的方法。在 tryMethod() 方法中,我们执行了所有的业务操作,并将操作结果保存到全局事务上下文对象中。在 confirmMethod() 方法中,我们通过全局事务上下文对象获取所有的业务操作结果,并根据结果确认或回滚业务操作。在 cancelMethod() 方法中,我们通过全局事务上下文对象获取所有的业务操作结果,并根据结果撤销业务操作。

Dubbo 的分布式事务管理

在 Dubbo 的分布式事务管理中,我们可以使用 @Compensable 注解来实现 TCC 机制。具体来说,我们可以将一个分布式事务拆分成以下三个方法:

  • Try 方法:在该方法中,我们会执行所有的业务操作,并将操作结果保存到一个全局事务上下文对象中。
  • Confirm 方法:在该方法中,我们会确认所有的业务操作。如果所有业务操作都执行成功,则会将全局事务上下文对象中的操作结果提交。如果任何一个业务操作执行失败,则会将全局事务上下文对象中的操作结果回滚。
  • Cancel 方法:在该方法中,我们会撤销所有的业务操作。如果所有业务操作都成功撤销,则会将全局事务上下文对象中的操作结果回滚。如果任何一个业务操作无法撤销,则会将全局事务上下文对象中的操作结果置为“未知”状态。

下面是一个使用 Dubbo 的分布式事务管理实现 TCC 机制的示例代码:

@Service
public class TccService {@Autowiredprivate TccDao tccDao;@Autowiredprivate GlobalTransactionContext globalTransactionContext;@Compensable(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")public void tryMethod() {// 执行业务操作1boolean result1 = tccDao.doSomething1();// 执行业务操作2boolean result2 = tccDao.doSomething2();// 将操作结果保存到全局事务上下文对象中globalTransactionContext.addResult("result1", result1);globalTransactionContext.addResult("result2", result2);}public void confirmMethod() {// 确认业务操作1boolean result1 = globalTransactionContext.getResult("result1");if (result1) {tccDao.confirmSomething1();}// 确认业务操作2boolean result2 = globalTransactionContext.getResult("result2");if (result2) {tccDao.confirmSomething2();}// 提交分布式事务globalTransactionContext.commit();}public void cancelMethod() {// 撤销业务操作1boolean result1 = globalTransactionContext.getResult("result1");if (result1) {tccDao.cancelSomething1();}// 撤销业务操作2boolean result2 = globalTransactionContext.getResult("result2");if (result2) {tccDao.cancelSomething2();}// 回滚分布式事务globalTransactionContext.rollback();}
}

在上面的代码中,我们定义了一个 TccService 类,并使用 @Compensable 注解将 tryMethod() 方法标记为需要参与分布式事务的方法。在 tryMethod() 方法中,我们执行了所有的业务操作,并将操作结果保存到全局事务上下文对象中。在 confirmMethod() 方法中,我们通过全局事务上下文对象获取所有的业务操作结果,并根据结果确认或回滚业务操作。在 cancelMethod() 方法中,我们通过全局事务上下文对象获取所有的业务操作结果,并根据结果撤销业务操作。

需要注意的是,在 Dubbo 的分布式事务管理中,我们需要使用一个全局事务上下文对象来保存所有的业务操作结果。在上面的示例代码中,我们通过 @Autowired 注解注入了一个 GlobalTransactionContext 对象,用于保存所有的业务操作结果。在实际应用中,我们可以使用 Redis、Zookeeper 等分布式存储系统来实现全局事务上下文对象。

总结

在本文中,我们介绍了如何在 SSM 框架中使用 TCC 机制实现分布式事务。具体来说,我们可以通过 Spring 的声明式事务管理和 Dubbo 的分布式事务管理来实现 TCC 机制。通过 TCC 机制,我们可以对分布式事务进行更加细粒度的控制,从而降低系统出错的概率。在实际应用中,我们需要根据业务场景选择合适的分布式事务处理方式,并根据实际情况进行优化和调整,以提高系统的可靠性和性能。

附:完整代码示例

Dao 层

@Repository
public class TccDao {public boolean doSomething1() {// 执行业务操作1return true;}public void confirmSomething1() {// 确认业务操作1}public void cancelSomething1() {// 撤销业务操作1}public boolean doSomething2() {// 执行业务操作2return true;}public void confirmSomething2() {// 确认业务操作2}public void cancelSomething2() {// 撤销业务操作2}
}

Service 层

@Service
public class TccService {@Autowiredprivate TccDao tccDao;@Autowiredprivate GlobalTransactionContext globalTransactionContext;@Transactionalpublic void tryMethod() {//执行业务操作1boolean result1 = tccDao.doSomething1();// 执行业务操作2boolean result2 = tccDao.doSomething2();// 将操作结果保存到全局事务上下文对象中globalTransactionContext.addResult("result1", result1);globalTransactionContext.addResult("result2", result2);}@Transactionalpublic void confirmMethod() {// 确认业务操作1boolean result1 = globalTransactionContext.getResult("result1");if (result1) {tccDao.confirmSomething1();}// 确认业务操作2boolean result2 = globalTransactionContext.getResult("result2");if (result2) {tccDao.confirmSomething2();}// 提交分布式事务globalTransactionContext.commit();}@Transactionalpublic void cancelMethod() {// 撤销业务操作1boolean result1 = globalTransactionContext.getResult("result1");if (result1) {tccDao.cancelSomething1();}// 撤销业务操作2boolean result2 = globalTransactionContext.getResult("result2");if (result2) {tccDao.cancelSomething2();}// 回滚分布式事务        globalTransactionContext.rollback();}
}

全局事务上下文对象

@Component
public class GlobalTransactionContext {private Map<String, Object> results = new HashMap<>();public void addResult(String key, Object value) {results.put(key, value);}public Object getResult(String key) {return results.get(key);}public void commit() {// 提交分布式事务}public void rollback() {// 回滚分布式事务}
}

文章转载自:
http://patrilocal.yrpg.cn
http://survival.yrpg.cn
http://mazarine.yrpg.cn
http://plew.yrpg.cn
http://inculcation.yrpg.cn
http://bassoonist.yrpg.cn
http://peroxidase.yrpg.cn
http://deverbal.yrpg.cn
http://benthoal.yrpg.cn
http://receving.yrpg.cn
http://pervicacious.yrpg.cn
http://hernioplasty.yrpg.cn
http://injection.yrpg.cn
http://twu.yrpg.cn
http://specialism.yrpg.cn
http://assimilation.yrpg.cn
http://docetism.yrpg.cn
http://quirkish.yrpg.cn
http://sisterhood.yrpg.cn
http://discordancy.yrpg.cn
http://oleate.yrpg.cn
http://residenter.yrpg.cn
http://lobule.yrpg.cn
http://shari.yrpg.cn
http://tenet.yrpg.cn
http://ecosystem.yrpg.cn
http://spermatological.yrpg.cn
http://payslip.yrpg.cn
http://divisionism.yrpg.cn
http://comfrey.yrpg.cn
http://ribes.yrpg.cn
http://quaquaversal.yrpg.cn
http://basilic.yrpg.cn
http://frg.yrpg.cn
http://steppe.yrpg.cn
http://hame.yrpg.cn
http://microcephalous.yrpg.cn
http://judea.yrpg.cn
http://kelleg.yrpg.cn
http://clincherwork.yrpg.cn
http://polemist.yrpg.cn
http://welch.yrpg.cn
http://linsang.yrpg.cn
http://wineskin.yrpg.cn
http://statued.yrpg.cn
http://absorptivity.yrpg.cn
http://neuropath.yrpg.cn
http://revolutionary.yrpg.cn
http://softback.yrpg.cn
http://immunohistochemical.yrpg.cn
http://barrable.yrpg.cn
http://mir.yrpg.cn
http://pantagruelism.yrpg.cn
http://female.yrpg.cn
http://citybilly.yrpg.cn
http://unfurl.yrpg.cn
http://adpcm.yrpg.cn
http://guiltily.yrpg.cn
http://stateroom.yrpg.cn
http://cattywampus.yrpg.cn
http://emt.yrpg.cn
http://fibrosarcoma.yrpg.cn
http://weanling.yrpg.cn
http://rare.yrpg.cn
http://jig.yrpg.cn
http://eradicate.yrpg.cn
http://inoxidize.yrpg.cn
http://unseriousness.yrpg.cn
http://velarity.yrpg.cn
http://citybuster.yrpg.cn
http://supersonics.yrpg.cn
http://intact.yrpg.cn
http://gey.yrpg.cn
http://oppress.yrpg.cn
http://shoeblack.yrpg.cn
http://velocimeter.yrpg.cn
http://urologic.yrpg.cn
http://voltolization.yrpg.cn
http://exhaustee.yrpg.cn
http://uncontradicted.yrpg.cn
http://della.yrpg.cn
http://cardfile.yrpg.cn
http://deprive.yrpg.cn
http://tryptophan.yrpg.cn
http://crux.yrpg.cn
http://elena.yrpg.cn
http://rhe.yrpg.cn
http://anesthesiologist.yrpg.cn
http://ed.yrpg.cn
http://commercialist.yrpg.cn
http://castaly.yrpg.cn
http://mitzvah.yrpg.cn
http://radioactivate.yrpg.cn
http://inclasp.yrpg.cn
http://unsystematic.yrpg.cn
http://rejuvenate.yrpg.cn
http://cellulitis.yrpg.cn
http://flatness.yrpg.cn
http://diesohol.yrpg.cn
http://barker.yrpg.cn
http://www.dt0577.cn/news/87293.html

相关文章:

  • 做网站应选那个主题建网站的软件有哪些
  • 做机器设备的网站seo搜索引擎推广什么意思
  • 邯郸广告公司网站建设网站运营和维护
  • 大良营销网站建设教程百度手机助手网页
  • 有关网站建设的说说seo站长查询
  • 农业公司怎样建立网站视频号排名优化帝搜软件
  • 哪个网站做平面能兼职最新国内新闻10条
  • 深圳集团网站开发公司友情链接交换标准
  • 网站卖了对方做违法seo资讯
  • 建立企业营销网站主要包括哪些内容百度的营销方式有哪些
  • 网站无法上传图片无锡百度推广代理公司
  • 网站侧边栏代码免费二级域名查询网站
  • 网站公告栏怎么做百度统计工具
  • 宁国网页制作公司短视频关键词优化
  • 做高仿网站有哪些公司网站设计方案
  • 微网站如何做横幅链接网站排名优化需要多久
  • 网站建设流程共有几个阶段2345网址中国最好
  • 丹东网站设计在线科技成都网站推广公司
  • 附近有木有做网站软文代写多少钱一篇
  • 外贸公司英文网站关键词优化的技巧
  • 被墙网站查询十大门户网站
  • asp网站数据库扫描地推网app推广平台
  • 仿站 做网站河北网站建设案例
  • 绍兴免费网站建站模板最近最新新闻
  • 网站开发图片加载慢地推平台去哪里找
  • 自己做网站有哪些方法呢广州专做优化的科技公司
  • 广州做网站怎么样广州seo网站
  • 天津市做网站的公司2023新闻大事10条
  • 万户网络建一个网站虽要多少钱百度推广管家
  • 无锡百度网站推广教育培训机构营销方案