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

济南网站系统优化网站推广目标计划

济南网站系统优化,网站推广目标计划,wordpress和hugu,做网站要到通信管理局备案了解事务的都知道#xff0c;在我们日常开发中单单靠事务管理就可以解决绝大多数问题了#xff0c;但是为啥还要提出JTA这个玩意呢#xff0c;到底JTA是什么呢#xff1f;他又是具体来解决啥问题的呢#xff1f; JTA JTA#xff08;Java Transaction API#xff09;是…了解事务的都知道在我们日常开发中单单靠事务管理就可以解决绝大多数问题了但是为啥还要提出JTA这个玩意呢到底JTA是什么呢他又是具体来解决啥问题的呢 JTA JTAJava Transaction API是Java平台上用于管理分布式事务的API。它提供了一组接口和类用于协调和控制跨多个资源如数据库、消息队列等的事务操作。 JTA的架构体系如下 JTA的主要目标是确保分布式环境中的事务的原子性、一致性、隔离性和持久性ACID属性。它通过以下几个关键概念和组件来实现 事务管理器Transaction Manager负责协调和管理事务的开始、提交和回滚等操作。它是JTA的核心组件负责跟踪和控制事务的状态。 用户事务User Transaction表示应用程序发起的事务通过事务管理器来管理和控制。 XA资源管理器XA Resource Manager表示分布式环境中的资源如数据库、消息队列等。它实现了XA接口可以参与到分布式事务中。 XA事务XA Transaction表示跨多个XA资源管理器的分布式事务。它遵循XA协议通过两阶段提交Two-Phase Commit来保证事务的一致性。 使用JTA开发人员可以在分布式环境中编写具有事务保证的应用程序。它提供了一种标准化的方式来处理分布式事务简化了开发人员的工作同时确保了数据的一致性和可靠性。 JTA事务比我们常用的JDBC事务更加强大一个JTA事务可以有多个参与者而一个JDBC事务则别限定在一个单一的数据库连接。 这么说吧我举个栗子 我们采用多数据源的时候假设我们对A数据源的更新与B数据源的更新具有事务性比如我们对订单中创建一条新的订单数据同时我也需要在商品库中进行相关商品的扣减库存假设我们对库存进行扣减失败了那么我们肯定希望我们的订单也返回到之前没下订单之前的状态毕竟我下了订单了库存没减少我这算哪门子的下了订单。 如果这两条数据位于一个数据库那么我们可以通过简单的事务管理就可以完成操作那么我们至此就可以结束了但是当我们的这两个操作要是在不同的数据库中那么我们该怎么办呢 那么我们就来测试一下 Spring Boot中引入相关依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependency!--重点围绕这个类--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jta-atomikos/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdscopeprovided/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency之后再Spring Boot application配置连接数据库的相关配置 spring.jta.enabledtruespring.jta.atomikos.datasource.primary.xa-properties.urljdbc:mysql://localhost:3306/test1?useUnicodetruecharacterEncodingutf-8useSSLtrueserverTimezoneUTC spring.jta.atomikos.datasource.primary.xa-properties.userroot spring.jta.atomikos.datasource.primary.xa-properties.password123456 spring.jta.atomikos.datasource.primary.xa-data-source-class-namecom.mysql.cj.jdbc.MysqlXADataSource spring.jta.atomikos.datasource.primary.unique-resource-nametest1 spring.jta.atomikos.datasource.primary.max-pool-size25 spring.jta.atomikos.datasource.primary.min-pool-size3 spring.jta.atomikos.datasource.primary.max-lifetime20000 spring.jta.atomikos.datasource.primary.borrow-connection-timeout10000spring.jta.atomikos.datasource.secondary.xa-properties.urljdbc:mysql://localhost:3306/test2?useUnicodetruecharacterEncodingutf-8useSSLtrueserverTimezoneUTC spring.jta.atomikos.datasource.secondary.xa-properties.userroot spring.jta.atomikos.datasource.secondary.xa-properties.password123456 spring.jta.atomikos.datasource.secondary.xa-data-source-class-namecom.mysql.cj.jdbc.MysqlXADataSource spring.jta.atomikos.datasource.secondary.unique-resource-nametest2 spring.jta.atomikos.datasource.secondary.max-pool-size25 spring.jta.atomikos.datasource.secondary.min-pool-size3 spring.jta.atomikos.datasource.secondary.max-lifetime20000 spring.jta.atomikos.datasource.secondary.borrow-connection-timeout10000Configuration public class DataSourceConfiguration {PrimaryBeanConfigurationProperties(prefix spring.jta.atomikos.datasource.primary)public DataSource primaryDataSource() {return new AtomikosDataSourceBean();}BeanConfigurationProperties(prefix spring.jta.atomikos.datasource.secondary)public DataSource secondaryDataSource() {return new AtomikosDataSourceBean();}Beanpublic JdbcTemplate primaryJdbcTemplate(Qualifier(primaryDataSource) DataSource primaryDataSource) {return new JdbcTemplate(primaryDataSource);}Beanpublic JdbcTemplate secondaryJdbcTemplate(Qualifier(secondaryDataSource) DataSource secondaryDataSource) {return new JdbcTemplate(secondaryDataSource);}}创建一个测试Service用来校验我们的JTA是否可以完成我们想要的工作。 Service public class TestService {private JdbcTemplate primaryJdbcTemplate;private JdbcTemplate secondaryJdbcTemplate;public TestService(JdbcTemplate primaryJdbcTemplate, JdbcTemplate secondaryJdbcTemplate) {this.primaryJdbcTemplate primaryJdbcTemplate;this.secondaryJdbcTemplate secondaryJdbcTemplate;}Transactionalpublic void tx() {// 修改test1库中的数据primaryJdbcTemplate.update(update user set age ? where name ?, 30, aaa);// 修改test2库中的数据secondaryJdbcTemplate.update(update user set age ? where name ?, 30, aaa);}Transactionalpublic void tx2() {// 修改test1库中的数据primaryJdbcTemplate.update(update user set age ? where name ?, 40, aaa);// 模拟修改test2库之前抛出异常throw new RuntimeException();} }在以上操作中我们定义tx方法中一般会成功但tx2方法中我们自己给他定义了一个异常这个是在test1数据库更新后才会产生的这样就可以测试一test1更新成功后是否还能再JTA的帮助下实现回滚。 创建一个单元测试类 SpringBootTest(classes Application.class) public class ApplicationTests {Autowiredprotected JdbcTemplate primaryJdbcTemplate;Autowiredprotected JdbcTemplate secondaryJdbcTemplate;Autowiredprivate TestService testService;Testpublic void test1() throws Exception {// 正确更新的情况testService.tx();Assertions.assertEquals(30, primaryJdbcTemplate.queryForObject(select age from user where name?, Integer.class, aaa));Assertions.assertEquals(30, secondaryJdbcTemplate.queryForObject(select age from user where name?, Integer.class, aaa));}Testpublic void test2() throws Exception {// 更新失败的情况try {testService.tx2();} catch (Exception e) {e.printStackTrace();} finally {// 部分更新失败test1中的更新应该回滚Assertions.assertEquals(30, primaryJdbcTemplate.queryForObject(select age from user where name?, Integer.class, aaa));Assertions.assertEquals(30, secondaryJdbcTemplate.queryForObject(select age from user where name?, Integer.class, aaa));}} }对以上测试用例 test1因为没有故意制造的异常一般情况下两个库的update都会成功然后我们根据nameaaa去把两个数据查出来看age是否都被更新到了30。 test2tx2函数会把test1中nameaaa的用户age更新为40然后抛出异常JTA事务生效的话会把age回滚回30所以这里的检查也是两个库的aaa用户的age应该都为30这样就意味着JTA事务生效保证了test1和test2两个库中的User表数据更新一致没有制造出脏数据。
http://wiki.neutronadmin.com/news/265413/

相关文章:

  • 南阳网站开发公司广州 网站优化
  • h5开发环境济南网站优化排名推广
  • 宣城市建设监督管理局网站首页完整的营销策划方案
  • 智慧团建团员登录网站网站 域名解析出错
  • 江苏建设行业证书编号查询网站电商公司组织架构图
  • 商城网站服务器租用视频制作流程
  • 建设通网站怎么注销微信小程序开发介绍
  • 会展相关网站建设柳州市建设中心网站首页
  • seo包括网站建设吗福建厦门网站建设公司
  • 建设一个旅游网站网络营销心得体会
  • 网站建设项目的工期计划英文网站建设合同
  • 大眼睛网站建设html个人博客完整代码
  • 大企业网站建设方案中国互联网前100名企业
  • 上海哪里有做网站的软件开发需要学什么知识
  • dede多个网站怎么做怎样在百度上做推广网站
  • 潍坊网站制作在线网站所有者是什么意思
  • 免费制作永久网站医院网站主页面设计
  • 大图网 网站网站上传百度多久收录
  • 广州企业网站建设公司哪家好施工企业项目负责人现场带班时间
  • 青岛手机网站建设包头市做网站公司
  • 三只松鼠电商网站建设利用wordpress实现分类筛选
  • 深圳国贸网站建设网站的推广方法
  • 网站费用多少空间印象商业空间设计
  • 天助网站个人工商查询官网入口查询
  • 西安城乡建设网站制作网站谁家做的好
  • 像优酷这样的网站需要怎么做棒的网页设计
  • 湖北移动网站建设网站开发网络课程
  • 网站首屏最好的医疗网站建设
  • 网站是怎么制作出来的大航母网站建设怎么样
  • 做的网站加载太慢怎么办医联体网站建设