网站出现的问题吗宁波高端网站设计价格
news/
2025/10/4 3:12:52/
文章来源:
网站出现的问题吗,宁波高端网站设计价格,建筑方案设计,盐城建设网站1.什么是atomikos Atomikos是一个轻量级的分布式事务管理器#xff0c;实现了Java Transaction API (JTA)规范#xff0c;可以很方便的和Spring Boot集成#xff0c;支持微服务场景下跨节点的全局事务。Atomikos公司官方网址为#xff1a;https://www.atomikos.com/。其旗下… 1.什么是atomikos Atomikos是一个轻量级的分布式事务管理器实现了Java Transaction API (JTA)规范可以很方便的和Spring Boot集成支持微服务场景下跨节点的全局事务。Atomikos公司官方网址为https://www.atomikos.com/。其旗下最著名的产品就是事务管理器。产品分两个版本 TransactionEssentials开源的免费产品ExtremeTransactions上商业版需要收费。 2.环境搭建 第一个mysql数据库 docker run --name docker-mysql -e MYSQL_ROOT_PASSWORD123456 -p 3333:3306 -d mysql 第二个mysql数据库 docker run --name docker-mysql-2 -e MYSQL_ROOT_PASSWORD123456 -p 3334:3306 -d mysql 初始化数据 create database demo;
create table user_info
(
user_id varchar(64) not null primary key,
username varchar(100) null ,
age int(3) null ,
gender tinyint(1) null ,
remark varchar(255) null ,
create_time datetime null ,
create_id varchar(64) null ,
update_time datetime null ,
update_id varchar(64) null ,
enabled tinyint(1) default 1 null
); 说明 msyql账号root
mysql密码123456 3.项目代码 实验目的实现2个mysql数据的分布式事务管理要么全部成功只要有一个失败就会滚。 pom..xml ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringboot-demo/artifactIdgroupIdcom.et/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdatomikos/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.3.0/version!-- 1.3.0以上的版本没有MapperScan以及Select注解 --/dependency!-- automaticjta的分布式事务管理 --!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jta-atomikos --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jta-atomikos/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scope/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId!--boot 2.1默认 mysql8的版本; boot 2.0默认mysql5版本--version8.0.13/version!--version5.1.46/version--!--scoperuntime/scope--/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optionalversion1.18.2/version/dependency/dependencies
/project mapper 创建2个mapper连接不同的数据库 package com.et.atomikos.mapper1;import org.apache.catalina.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;public interface UserInfoMapper1 {// querySelect(SELECT * FROM user_info WHERE username #{username})User findByName(Param(username) String username);// addInsert(INSERT INTO user_info(user_id,username, age) VALUES(#{userId},#{username}, #{age}))int insert(Param(userId) String userId,Param(username) String username, Param(age) Integer age);
} package com.et.atomikos.mapper2;import org.apache.catalina.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;public interface UserInfoMapper2 {// querySelect(SELECT * FROM user_info WHERE username #{username})User findByName(Param(username) String username);// addInsert(INSERT INTO user_info(user_id,username, age) VALUES(#{userId},#{username}, #{age}))int insert(Param(userId) String userId,Param(username) String username, Param(age) Integer age);
} service 创建2个service分别用不同mapper package com.et.atomikos.mapper1;import com.et.atomikos.mapper1.UserInfoMapper1;
import com.et.atomikos.mapper2.UserInfoMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;Service
public class ManyService1 {Autowiredprivate UserInfoMapper1 userInfoMapper1;Autowiredprivate UserInfoMapper2 userInfoMapper2;Transactionalpublic int insert(String userId,String username, Integer age) {int insert userInfoMapper1.insert(userId,username, age);int i 1 / age;// if age is zero ,then a error will be happened.return insert;}Transactionalpublic int insertDb1AndDb2(String userId,String username, Integer age) {int insert userInfoMapper1.insert(userId,username, age);int insert2 userInfoMapper2.insert(userId,username, age);int i 1 / age;// if age is zero ,then a error will be happened.return insert insert2;}} package com.et.atomikos.mapper2;import com.et.atomikos.mapper2.UserInfoMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;Service
public class ManyService2 {Autowiredprivate UserInfoMapper2 userInfoMapper2;Transactionalpublic int insert(String userId,String username, Integer age) {int i userInfoMapper2.insert(userId,username, age);System.out.println(userInfoMapper2.insert end : null);int a 1 / 0;//touch a errorreturn i;}} config 初始化数据源1和数据源2 package com.et.atomikos.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;Data
ConfigurationProperties(prefix spring.datasource.test1)
public class DBConfig1 {// Value(${mysql.datasource.test1.jdbcurl})//Value(${jdbcurl})private String jdbcurl;//private String url;private String username;private String password;private int minPoolSize;private int maxPoolSize;private int maxLifetime;private int borrowConnectionTimeout;private int loginTimeout;private int maintenanceInterval;private int maxIdleTime;private String testQuery;
} package com.et.atomikos.config;import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.sql.SQLException;/*** author liuhaihua* version 1.0* ClassName MyBatisConfig1* Description todo* date 2024年04月18日 13:37*/Configuration
MapperScan(basePackages com.et.atomikos.mapper1, sqlSessionTemplateRef test1SqlSessionTemplate)
public class MyBatisConfig1 {Bean(name test1DataSource) //test1DataSourcepublic DataSource testDataSource(DBConfig1 testConfig) throws SQLException {MysqlXADataSource mysqlXaDataSource new MysqlXADataSource();//mysqlXaDataSource.setUrl(testConfig.getUrl());mysqlXaDataSource.setUrl(testConfig.getJdbcurl());mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);mysqlXaDataSource.setPassword(testConfig.getPassword());mysqlXaDataSource.setUser(testConfig.getUsername());mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);// 将本地事务注册到创 Atomikos全局事务AtomikosDataSourceBean xaDataSource new AtomikosDataSourceBean();xaDataSource.setXaDataSource(mysqlXaDataSource);xaDataSource.setUniqueResourceName(test1DataSource);xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());xaDataSource.setTestQuery(testConfig.getTestQuery());return xaDataSource;}Bean(name test1SqlSessionFactory)public SqlSessionFactory testSqlSessionFactory(Qualifier(test1DataSource) DataSource dataSource)throws Exception {SqlSessionFactoryBean bean new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}Bean(name test1SqlSessionTemplate)public SqlSessionTemplate testSqlSessionTemplate(Qualifier(test1SqlSessionFactory) SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
} 数据源2也是类似配置 controller package com.et.atomikos.controller;import com.et.atomikos.mapper1.ManyService1;
import com.et.atomikos.mapper2.ManyService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;RestController
public class HelloWorldController {Autowiredprivate ManyService1 manyService1;Resourceprivate ManyService2 manyService2;//http://localhost:8088/datasource1?userId9usernamedatasource1age2RequestMapping(value datasource1)public int datasource1(String userId,String username, Integer age) {return manyService1.insert(userId,username, age);}//http://localhost:8088/datasource2?userId9usernamedatasource2age2RequestMapping(value datasource2)public int datasource2(String userId,String username, Integer age) {return manyService2.insert(userId,username, age);}//http://localhost:8088/insertDb1AndDb2?userId1usernametom5age2//http://localhost:8088/insertDb1AndDb2?userId2usernametom5age0 //touch a errorRequestMapping(value insertDb1AndDb2)public int insertDb1AndDb2(String userId,String username, Integer age) {return manyService1.insertDb1AndDb2(userId,username, age);}} application.yaml server:port: 8088spring:application:name: manyDatasourcedatasource:# spring.datasource.test1# druid:test1:jdbcurl: jdbc:mysql://localhost:3333/demo?serverTimezoneGMT%2B8useUnicodetruecharacterEncodingutf-8username: rootpassword: 123456initial-size: 1min-idle: 1max-active: 20test-on-borrow: truedriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceminPoolSize: 3maxPoolSize: 25maxLifetime: 20000borrowConnectionTimeout: 30loginTimeout: 30maintenanceInterval: 60maxIdleTime: 60test2:jdbcurl: jdbc:mysql://localhost:3334/demo?serverTimezoneGMT%2B8useUnicodetruecharacterEncodingutf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceminPoolSize: 3maxPoolSize: 25maxLifetime: 20000borrowConnectionTimeout: 30loginTimeout: 30maintenanceInterval: 60maxIdleTime: 60mybatis:mapper-locations: classpath:mapper/*.xmlspring.resources.static-locations: classpath:static/,file:static/logging:level:czs: debugorg.springframework: WARNorg.spring.springboot.dao: debug 以上只是一些关键代码所有代码请参见下面代码仓库 代码仓库 https://github.com/Harries/springboot-demo 4.测试 启动Spring Boot 应用 插入第一个数据测试 http://localhost:8088/datasource1?userId9usernamedatasource1age2 插入第二个数据库 http://localhost:8088/datasource2?userId9usernamedatasource2age2 同时插入2个数据库 http://localhost:8088/insertDb1AndDb2?userId1usernametom5age2 异常回滚测试 http://localhost:8088/insertDb1AndDb2?userId2usernametom5age0 //touch a error 5.参考 https://github.com/ColoZhu/springbootmanyDatasourcehttp://www.liuhaihua.cn/archives/710435.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/926549.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!