You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "longkaimao (via GitHub)" <gi...@apache.org> on 2023/03/14 13:25:49 UTC
[GitHub] [shardingsphere] longkaimao commented on issue #24586: How to configure 'seata.enable-auto-data-source-proxy' when using shardingsphere?
longkaimao commented on issue #24586:
URL: https://github.com/apache/shardingsphere/issues/24586#issuecomment-1468100339
The following is my main code
- GlobalTransaction code
` @Transactional(rollbackFor = Exception.class)
@ShardingTransactionType(value = TransactionType.BASE)
public boolean add(OrderAddReq req) {
log.info("Seata全局事务id=================>{}", RootContext.getXID());
OrderEntity order = new OrderEntity();
order.setProductId(req.getProductId());
order.setMoney(req.getMoney());
order.setUserId(req.getUserId());
order.setCount(req.getCount());
order.setStatus(0);
this.baseOrderService.save(order);
AccountOpReq accountOpReq = new AccountOpReq();
accountOpReq.setUserId(req.getUserId());
accountOpReq.setMoney(req.getMoney());
Result<?> result = accountApi.accountOp(accountOpReq);
if(!result.success()){
throw new BaseFlowException(result.getMsg());
}
if(req.getCount() == 10000){
int i = 1/0;
}
return true;
}
`
- branch transaction code
1. seata config in bootstrap.yml
`
sfs:
nacos:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
namespace: seata-server-160
group: SEATA_GROUP
seata:
enabled: true
application-id : ${spring.application.name}
tx-service-group: default_tx_group
use-jdk-proxy: true
enable-auto-data-source-proxy: true
registry:
type: nacos
nacos:
application: seata-server
server-addr: ${sfs.nacos.server-addr}
namespace: ${sfs.nacos.namespace}
group: ${sfs.nacos.group}
config:
type: nacos
nacos:
server-addr: ${sfs.nacos.server-addr}
namespace: ${sfs.nacos.namespace}
group: ${sfs.nacos.group}
data-id: seataServer.properties
service:
vgroupMapping:
default_tx_group: default`
2. sharding config
`#账号微服务服务信息-分库又分表
server.port=9220
spring.application.name=account
#订单微服务注册中心
spring.cloud.nacos.discovery.server-addr=82.157.70.56:8848
spring.cloud.nacos.discovery.group=DEFAULT_GROUP
spring.cloud.nacos.discovery.namespace=public
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=lkmNacos
#sharding-jdbc数据源配置
spring.shardingsphere.datasource.names=ds-0,ds-1
spring.shardingsphere.mode.type=Standalone
#spring.shardingsphere.mode.repository.type=File
spring.shardingsphere.mode.overwrite=true
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://82.157.70.56:3306/seata-account-0?serverTimezone=UTC&characterEncoding=utf8
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=l
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://82.157.70.56:3306/seata-account-1?serverTimezone=UTC&characterEncoding=utf8
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=l
#sharding-jdbc分片配置
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=id
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=database-inline
spring.shardingsphere.rules.sharding.binding-tables[0]=t_account
#spring.shardingsphere.rules.sharding.broadcast-tables=t_address
spring.shardingsphere.rules.sharding.tables.t_account.actual-data-nodes=ds-$->{0..1}.t_account_$->{0..2}
spring.shardingsphere.rules.sharding.tables.t_account.table-strategy.standard.sharding-column=id
spring.shardingsphere.rules.sharding.tables.t_account.table-strategy.standard.sharding-algorithm-name=t-account-inline
spring.shardingsphere.rules.sharding.tables.t_account.key-generate-strategy.column=id
spring.shardingsphere.rules.sharding.tables.t_account.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds-$->{id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t-account-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.t-account-inline.props.algorithm-expression=t_account_$->{id % 3}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
#sharding-jdbc显示最终SQL
spring.shardingsphere.props.sql.show=true
#mybatis-plus配置
mybatis-plus.configuration.map-underscore-to-camel-case=true
logging.level.io.seata=info
`
3. branch transaction code
`@Service
@Slf4j
public class AccountService {
@Resource
IBaseAccountService baseAccountService;
public List<AccountEntity> list(){
return baseAccountService.list();
}
/**
* 以下在enable-auto-data-source-proxy开启和关闭的情况下,是否使用注解Transactional和ShardingSphereTransactionType,对事务回滚的结果
* enable-auto-data-source-proxy @Transactional @ShardingSphereTransactionType 结果
* true 有 无 回滚成功
* false 无 有 回滚成功
* false 有 无 分支事务没有回滚
* false 有 有 空指针异常
* true 有 有 空指针异常
* true 无 有 空指针异常
*
* @param req
*/
@Transactional(rollbackFor = Exception.class)
// @ShardingSphereTransactionType(value = TransactionType.BASE)
public void op(AccountOpReq req) {
log.info("Seata全局事务id=================>{}", RootContext.getXID());
AccountEntity byUserId = new AccountEntity();
byUserId.setUserId(req.getUserId());
byUserId.setUsed(new BigDecimal(1000L));
byUserId.setResidue(req.getMoney());
this.baseAccountService.save(byUserId);
}
}`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org