From 37dfce9cc01b218266a34f6522429b0559f8d030 Mon Sep 17 00:00:00 2001 From: yu199195 <549477611@qq.com> Date: Wed, 2 Sep 2020 20:06:58 +0800 Subject: [PATCH] hmily tac dubbo demo --- ...HmilyTransactionSelfRecoveryScheduled.java | 78 +++++++++---------- .../config/HmilyTacDatasourceConfig.java | 67 ++++++++++++++++ .../dubbo/account/mapper/AccountMapper.java | 10 +++ .../account/service/AccountServiceImpl.java | 3 +- .../DubboHmilyInventoryApplication.java | 1 - .../config/HmilyTacDatasourceConfig.java | 67 ++++++++++++++++ .../inventory/mapper/InventoryMapper.java | 10 +++ .../service/InventoryServiceImpl.java | 13 ++-- .../src/main/resources/application.yml | 6 +- .../HmilyTacDatasourceConfig.java | 16 +++- .../src/main/resources/application.yml | 8 +- .../src/main/resources/spring-dubbo.xml | 4 +- .../HmilyTacStarterTransaction.java | 15 ++++ .../executor/HmilyTccTransactionExecutor.java | 3 +- 14 files changed, 236 insertions(+), 65 deletions(-) create mode 100644 hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/config/HmilyTacDatasourceConfig.java create mode 100644 hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/config/HmilyTacDatasourceConfig.java diff --git a/hmily-core/src/main/java/org/dromara/hmily/core/schedule/HmilyTransactionSelfRecoveryScheduled.java b/hmily-core/src/main/java/org/dromara/hmily/core/schedule/HmilyTransactionSelfRecoveryScheduled.java index bdd7a621..8ba382cc 100644 --- a/hmily-core/src/main/java/org/dromara/hmily/core/schedule/HmilyTransactionSelfRecoveryScheduled.java +++ b/hmily-core/src/main/java/org/dromara/hmily/core/schedule/HmilyTransactionSelfRecoveryScheduled.java @@ -66,15 +66,9 @@ public class HmilyTransactionSelfRecoveryScheduled implements AutoCloseable { public HmilyTransactionSelfRecoveryScheduled() { hmilyRepository = ExtensionLoaderFactory.load(HmilyRepository.class, hmilyConfig.getRepository()); - this.selfTccRecoveryExecutor = - new ScheduledThreadPoolExecutor(1, - HmilyThreadFactory.create("hmily-tcc-self-recovery", true)); - this.selfTacRecoveryExecutor = - new ScheduledThreadPoolExecutor(1, - HmilyThreadFactory.create("hmily-tac-self-recovery", true)); - this.cleanHmilyTransactionExecutor = - new ScheduledThreadPoolExecutor(1, - HmilyThreadFactory.create("hmily-transaction-clean", true)); + this.selfTccRecoveryExecutor = new ScheduledThreadPoolExecutor(1, HmilyThreadFactory.create("hmily-tcc-self-recovery", true)); + this.selfTacRecoveryExecutor = new ScheduledThreadPoolExecutor(1, HmilyThreadFactory.create("hmily-tac-self-recovery", true)); + this.cleanHmilyTransactionExecutor = new ScheduledThreadPoolExecutor(1, HmilyThreadFactory.create("hmily-transaction-clean", true)); hmilyTransactionRecoveryService = new HmilyTransactionRecoveryService(); selfTccRecovery(); selfTacRecovery(); @@ -85,9 +79,7 @@ public class HmilyTransactionSelfRecoveryScheduled implements AutoCloseable { private void phyDeleted() { if (!hmilyConfig.isPhyDeleted()) { int seconds = hmilyConfig.getStoreDays() * 24 * 60 * 60; - phyDeletedExecutor = - new ScheduledThreadPoolExecutor(1, - HmilyThreadFactory.create("hmily-phyDeleted-clean", true)); + phyDeletedExecutor = new ScheduledThreadPoolExecutor(1, HmilyThreadFactory.create("hmily-phyDeleted-clean", true)); phyDeletedExecutor .scheduleWithFixedDelay(() -> { try { @@ -127,21 +119,10 @@ public class HmilyTransactionSelfRecoveryScheduled implements AutoCloseable { LOGGER.info("hmily tcc transaction begin self recovery: {}", hmilyParticipant.toString()); HmilyTransaction globalHmilyTransaction = hmilyRepository.findByTransId(hmilyParticipant.getTransId()); if (Objects.isNull(globalHmilyTransaction)) { - if (hmilyParticipant.getStatus() == HmilyActionEnum.TRYING.getCode() - || hmilyParticipant.getStatus() == HmilyActionEnum.CANCELING.getCode()) { - hmilyTransactionRecoveryService.cancel(hmilyParticipant); - } else if (hmilyParticipant.getStatus() == HmilyActionEnum.CONFIRMING.getCode()) { - hmilyTransactionRecoveryService.confirm(hmilyParticipant); - } + tccRecovery(hmilyParticipant.getStatus(), hmilyParticipant); } else { - if (globalHmilyTransaction.getStatus() == HmilyActionEnum.TRYING.getCode() - || globalHmilyTransaction.getStatus() == HmilyActionEnum.CANCELING.getCode()) { - hmilyTransactionRecoveryService.cancel(hmilyParticipant); - } else if (globalHmilyTransaction.getStatus() == HmilyActionEnum.CONFIRMING.getCode()) { - hmilyTransactionRecoveryService.confirm(hmilyParticipant); - } + tccRecovery(globalHmilyTransaction.getStatus(), hmilyParticipant); } - } } } catch (Exception e) { @@ -150,13 +131,21 @@ public class HmilyTransactionSelfRecoveryScheduled implements AutoCloseable { }, hmilyConfig.getScheduledInitDelay(), hmilyConfig.getScheduledRecoveryDelay(), TimeUnit.SECONDS); } + private void tccRecovery(final int status, final HmilyParticipant hmilyParticipant) { + if (status == HmilyActionEnum.TRYING.getCode() || status == HmilyActionEnum.CANCELING.getCode()) { + hmilyTransactionRecoveryService.cancel(hmilyParticipant); + } else if (status == HmilyActionEnum.CONFIRMING.getCode()) { + hmilyTransactionRecoveryService.confirm(hmilyParticipant); + } + } + private void selfTacRecovery() { selfTacRecoveryExecutor .scheduleWithFixedDelay(() -> { try { List hmilyParticipantList = hmilyRepository.listHmilyParticipant(acquireDelayData(hmilyConfig.getRecoverDelayTime()), TransTypeEnum.TAC.name(), hmilyConfig.getLimit()); - + if (CollectionUtils.isEmpty(hmilyParticipantList)) { return; } @@ -170,27 +159,15 @@ public class HmilyTransactionSelfRecoveryScheduled implements AutoCloseable { final boolean successful = hmilyRepository.lockHmilyParticipant(hmilyParticipant); // determine that rows > 0 is executed to prevent concurrency when the business side is in cluster mode if (successful) { - HmilyTransaction globalHmilyTransaction = hmilyRepository.findByTransId(hmilyParticipant.getTransId()); - if (Objects.isNull(globalHmilyTransaction)) { - //do remove - hmilyRepository.removeHmilyParticipant(hmilyParticipant.getParticipantId()); - } List participantUndoList = hmilyRepository.findHmilyParticipantUndoByParticipantId(hmilyParticipant.getParticipantId()); if (CollectionUtils.isEmpty(participantUndoList)) { continue; } - if (globalHmilyTransaction.getStatus() == HmilyActionEnum.TRYING.getCode() - || globalHmilyTransaction.getStatus() == HmilyActionEnum.CANCELING.getCode()) { - for (HmilyParticipantUndo undo : participantUndoList) { - boolean success = UndoHook.INSTANCE.run(undo); - if (success) { - hmilyRepository.removeHmilyParticipantUndo(undo.getUndoId()); - } - } - } else if (globalHmilyTransaction.getStatus() == HmilyActionEnum.CONFIRMING.getCode()) { - for (HmilyParticipantUndo undo : participantUndoList) { - hmilyRepository.removeHmilyParticipantUndo(undo.getUndoId()); - } + HmilyTransaction globalHmilyTransaction = hmilyRepository.findByTransId(hmilyParticipant.getTransId()); + if (Objects.isNull(globalHmilyTransaction)) { + tacRecovery(hmilyParticipant.getStatus(), participantUndoList); + } else { + tacRecovery(globalHmilyTransaction.getStatus(), participantUndoList); } } hmilyRepository.removeHmilyParticipant(hmilyParticipant.getParticipantId()); @@ -201,6 +178,21 @@ public class HmilyTransactionSelfRecoveryScheduled implements AutoCloseable { }, hmilyConfig.getScheduledInitDelay(), hmilyConfig.getScheduledRecoveryDelay(), TimeUnit.SECONDS); } + private void tacRecovery(final int status, final List participantUndoList) { + if (status == HmilyActionEnum.TRYING.getCode() || status == HmilyActionEnum.CANCELING.getCode()) { + for (HmilyParticipantUndo undo : participantUndoList) { + boolean success = UndoHook.INSTANCE.run(undo); + if (success) { + hmilyRepository.removeHmilyParticipantUndo(undo.getUndoId()); + } + } + } else if (status == HmilyActionEnum.CONFIRMING.getCode()) { + for (HmilyParticipantUndo undo : participantUndoList) { + hmilyRepository.removeHmilyParticipantUndo(undo.getUndoId()); + } + } + } + private void cleanHmilyTransaction() { cleanHmilyTransactionExecutor .scheduleWithFixedDelay(() -> { diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/config/HmilyTacDatasourceConfig.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/config/HmilyTacDatasourceConfig.java new file mode 100644 index 00000000..a3f186c2 --- /dev/null +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/config/HmilyTacDatasourceConfig.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.demo.dubbo.account.config; + +import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; +import org.dromara.hmily.tac.p6spy.HmilyP6Datasource; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +/** + * The type Hmily tac datasource config. + * + * @author xiaoyu + */ +@Configuration +public class HmilyTacDatasourceConfig { + + private final DataSourceProperties dataSourceProperties; + + /** + * Instantiates a new Hmily tac datasource config. + * + * @param dataSourceProperties the data source properties + */ + public HmilyTacDatasourceConfig(DataSourceProperties dataSourceProperties) { + this.dataSourceProperties = dataSourceProperties; + } + + /** + * Data source data source. + * + * @return the data source + */ + @Bean + @Primary + public DataSource dataSource() { + HikariDataSource hikariDataSource = new HikariDataSource(); + hikariDataSource.setJdbcUrl(dataSourceProperties.getUrl()); + hikariDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); + hikariDataSource.setUsername(dataSourceProperties.getUsername()); + hikariDataSource.setPassword(dataSourceProperties.getPassword()); + hikariDataSource.setMaximumPoolSize(20); + hikariDataSource.setMinimumIdle(10); + hikariDataSource.setConnectionTimeout(30000); + hikariDataSource.setIdleTimeout(600000); + hikariDataSource.setMaxLifetime(1800000); + return new HmilyP6Datasource(hikariDataSource); + } +} diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/mapper/AccountMapper.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/mapper/AccountMapper.java index 525e7162..c82c551e 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/mapper/AccountMapper.java +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/mapper/AccountMapper.java @@ -40,6 +40,16 @@ public interface AccountMapper { " where user_id =#{userId} and balance > 0 ") int update(AccountDTO accountDTO); + /** + * Update tac int. + * + * @param accountDTO the account dto + * @return the int + */ + @Update("update account set balance = balance - #{amount}, update_time = now()" + + " where user_id =#{userId} and balance > 0 ") + int updateTAC(AccountDTO accountDTO); + /** * Test update int. * diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/service/AccountServiceImpl.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/service/AccountServiceImpl.java index 1afbb0b3..a4e950a0 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/service/AccountServiceImpl.java +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-account/src/main/java/org/dromara/hmily/demo/dubbo/account/service/AccountServiceImpl.java @@ -109,8 +109,7 @@ public class AccountServiceImpl implements AccountService { @Override @HmilyTAC public boolean paymentTAC(AccountDTO accountDTO) { - accountMapper.update(accountDTO); - return true; + return accountMapper.updateTAC(accountDTO) > 0; } @Override diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/DubboHmilyInventoryApplication.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/DubboHmilyInventoryApplication.java index 5f3b906e..41d64248 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/DubboHmilyInventoryApplication.java +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/DubboHmilyInventoryApplication.java @@ -23,7 +23,6 @@ import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; - /** * DubboTccInventoryApplication. * diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/config/HmilyTacDatasourceConfig.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/config/HmilyTacDatasourceConfig.java new file mode 100644 index 00000000..c684d520 --- /dev/null +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/config/HmilyTacDatasourceConfig.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.demo.dubbo.inventory.config; + +import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; +import org.dromara.hmily.tac.p6spy.HmilyP6Datasource; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +/** + * The type Hmily tac datasource config. + * + * @author xiaoyu + */ +@Configuration +public class HmilyTacDatasourceConfig { + + private final DataSourceProperties dataSourceProperties; + + /** + * Instantiates a new Hmily tac datasource config. + * + * @param dataSourceProperties the data source properties + */ + public HmilyTacDatasourceConfig(DataSourceProperties dataSourceProperties) { + this.dataSourceProperties = dataSourceProperties; + } + + /** + * Data source data source. + * + * @return the data source + */ + @Bean + @Primary + public DataSource dataSource() { + HikariDataSource hikariDataSource = new HikariDataSource(); + hikariDataSource.setJdbcUrl(dataSourceProperties.getUrl()); + hikariDataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); + hikariDataSource.setUsername(dataSourceProperties.getUsername()); + hikariDataSource.setPassword(dataSourceProperties.getPassword()); + hikariDataSource.setMaximumPoolSize(20); + hikariDataSource.setMinimumIdle(10); + hikariDataSource.setConnectionTimeout(30000); + hikariDataSource.setIdleTimeout(600000); + hikariDataSource.setMaxLifetime(1800000); + return new HmilyP6Datasource(hikariDataSource); + } +} diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/mapper/InventoryMapper.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/mapper/InventoryMapper.java index 894b0cea..2c56f7e0 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/mapper/InventoryMapper.java +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/mapper/InventoryMapper.java @@ -40,6 +40,16 @@ public interface InventoryMapper { " where product_id =#{productId} and total_inventory > 0 ") int decrease(InventoryDTO inventoryDTO); + /** + * Decrease tac int. + * + * @param inventoryDTO the inventory dto + * @return the int + */ + @Update("update inventory set total_inventory = total_inventory - #{count} " + + " where product_id =#{productId} and total_inventory > 0 ") + int decreaseTAC(InventoryDTO inventoryDTO); + /** * Test decrease int. * diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/service/InventoryServiceImpl.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/service/InventoryServiceImpl.java index 262721df..e9d1510a 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/service/InventoryServiceImpl.java +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/java/org/dromara/hmily/demo/dubbo/inventory/service/InventoryServiceImpl.java @@ -18,6 +18,9 @@ package org.dromara.hmily.demo.dubbo.inventory.service; import com.google.common.collect.Lists; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import org.dromara.hmily.annotation.HmilyTAC; import org.dromara.hmily.annotation.HmilyTCC; import org.dromara.hmily.common.exception.HmilyRuntimeException; @@ -31,10 +34,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; - /** * The type Inventory service. * @@ -72,15 +71,13 @@ public class InventoryServiceImpl implements InventoryService { @Override @HmilyTCC(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod") public Boolean decrease(InventoryDTO inventoryDTO) { - inventoryMapper.decrease(inventoryDTO); - return true; + return inventoryMapper.decrease(inventoryDTO) > 0; } @Override @HmilyTAC public Boolean decreaseTAC(InventoryDTO inventoryDTO) { - inventoryMapper.decrease(inventoryDTO); - return true; + return inventoryMapper.decreaseTAC(inventoryDTO) > 0; } @Override diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/resources/application.yml b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/resources/application.yml index 45ef4538..1a8fe78d 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/resources/application.yml +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-inventory/src/main/resources/application.yml @@ -21,8 +21,8 @@ logging: root: error org.springframework.web: info org.apache.ibatis: debug - org.dromara.hmily.bonuspoint: debug - org.dromara.hmily.lottery: debug - org.dromara.hmily: debug + org.dromara.hmily.demo.bonuspoint: debug + org.dromara.hmily.demo.lottery: debug + org.dromara.hmily.demo: debug path: "./logs" diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/java/org/dromara/hmily/demo/dubbo/order/configuration/HmilyTacDatasourceConfig.java b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/java/org/dromara/hmily/demo/dubbo/order/configuration/HmilyTacDatasourceConfig.java index 9f6927f6..5fbde80e 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/java/org/dromara/hmily/demo/dubbo/order/configuration/HmilyTacDatasourceConfig.java +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/java/org/dromara/hmily/demo/dubbo/order/configuration/HmilyTacDatasourceConfig.java @@ -25,16 +25,30 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; - +/** + * The type Hmily tac datasource config. + * + * @author xiaoyu + */ @Configuration public class HmilyTacDatasourceConfig { private final DataSourceProperties dataSourceProperties; + /** + * Instantiates a new Hmily tac datasource config. + * + * @param dataSourceProperties the data source properties + */ public HmilyTacDatasourceConfig(DataSourceProperties dataSourceProperties) { this.dataSourceProperties = dataSourceProperties; } + /** + * Data source data source. + * + * @return the data source + */ @Bean @Primary public DataSource dataSource() { diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/application.yml b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/application.yml index 67bd8dcf..80ceec1e 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/application.yml +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/application.yml @@ -19,10 +19,10 @@ logging: level: root: error org.springframework.web: info - org.apache.ibatis: info - org.dromara.hmily.bonuspoint: info - org.dromara.hmily.lottery: info - org.dromara.hmily: info + org.apache.ibatis: debug + org.dromara.hmily.demo.bonuspoint: debug + org.dromara.hmily.demo.lottery: debug + org.dromara.hmily.demo: debug io.netty: info path: "./logs" diff --git a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/spring-dubbo.xml b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/spring-dubbo.xml index 13851f98..5d7ff1e6 100644 --- a/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/spring-dubbo.xml +++ b/hmily-demo/hmily-demo-dubbo/hmily-demo-dubbo-order/src/main/resources/spring-dubbo.xml @@ -18,11 +18,11 @@ charset="UTF-8" threadpool="fixed" threads="500" queues="0" buffer="8192" accepts="0" payload="8388608"/> - - diff --git a/hmily-tac/hmily-tac-core/src/main/java/org/dromara/hmily/tac/core/transaction/HmilyTacStarterTransaction.java b/hmily-tac/hmily-tac-core/src/main/java/org/dromara/hmily/tac/core/transaction/HmilyTacStarterTransaction.java index 3ced5c9f..65dce4ef 100644 --- a/hmily-tac/hmily-tac-core/src/main/java/org/dromara/hmily/tac/core/transaction/HmilyTacStarterTransaction.java +++ b/hmily-tac/hmily-tac-core/src/main/java/org/dromara/hmily/tac/core/transaction/HmilyTacStarterTransaction.java @@ -17,6 +17,7 @@ package org.dromara.hmily.tac.core.transaction; +import com.google.common.collect.Lists; import java.util.List; import java.util.Objects; import org.dromara.hmily.annotation.TransTypeEnum; @@ -25,6 +26,7 @@ import org.dromara.hmily.common.enums.HmilyActionEnum; import org.dromara.hmily.common.enums.HmilyRoleEnum; import org.dromara.hmily.common.utils.CollectionUtils; import org.dromara.hmily.common.utils.IdWorkerUtils; +import org.dromara.hmily.core.cache.HmilyParticipantCacheManager; import org.dromara.hmily.core.context.HmilyContextHolder; import org.dromara.hmily.core.context.HmilyTransactionContext; import org.dromara.hmily.core.holder.HmilyTransactionHolder; @@ -147,6 +149,7 @@ public class HmilyTacStarterTransaction { if (CollectionUtils.isEmpty(hmilyParticipants)) { return; } + List successList = Lists.newArrayList(); for (HmilyParticipant participant : hmilyParticipants) { try { if (participant.getRole() == HmilyRoleEnum.START.getCode()) { @@ -156,15 +159,22 @@ public class HmilyTacStarterTransaction { //clean undo cleanUndo(undo); } + cleanHmilyParticipant(participant); } else { HmilyReflector.executor(HmilyActionEnum.CONFIRMING, ExecutorTypeEnum.RPC, participant); } + successList.add(true); } catch (Throwable e) { + successList.add(false); LOGGER.error("HmilyParticipant rollback exception :{} ", participant.toString()); } finally { HmilyContextHolder.remove(); } } + if (successList.stream().allMatch(e -> e)) { + // remove global + HmilyRepositoryStorage.removeHmilyTransaction(currentTransaction); + } } /** @@ -180,6 +190,11 @@ public class HmilyTacStarterTransaction { HmilyParticipantUndoCacheManager.getInstance().removeByKey(hmilyParticipantUndo.getParticipantId()); } + private void cleanHmilyParticipant(final HmilyParticipant hmilyParticipant) { + HmilyParticipantCacheManager.getInstance().removeByKey(hmilyParticipant.getParticipantId()); + HmilyRepositoryStorage.removeHmilyParticipant(hmilyParticipant); + } + /** * Gets hmily transaction. * diff --git a/hmily-tcc/src/main/java/org/dromara/hmily/tcc/executor/HmilyTccTransactionExecutor.java b/hmily-tcc/src/main/java/org/dromara/hmily/tcc/executor/HmilyTccTransactionExecutor.java index cbba58dd..4a7b8475 100644 --- a/hmily-tcc/src/main/java/org/dromara/hmily/tcc/executor/HmilyTccTransactionExecutor.java +++ b/hmily-tcc/src/main/java/org/dromara/hmily/tcc/executor/HmilyTccTransactionExecutor.java @@ -19,6 +19,7 @@ package org.dromara.hmily.tcc.executor; import com.google.common.collect.Lists; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import org.aspectj.lang.ProceedingJoinPoint; @@ -130,7 +131,7 @@ public final class HmilyTccTransactionExecutor { currentTransaction.setStatus(HmilyActionEnum.CONFIRMING.getCode()); HmilyRepositoryStorage.updateHmilyTransactionStatus(currentTransaction); final List hmilyParticipants = currentTransaction.getHmilyParticipants(); - List successList = Lists.newArrayListWithCapacity(hmilyParticipants.size()); + List successList = new ArrayList<>(); for (HmilyParticipant hmilyParticipant : hmilyParticipants) { try { if (hmilyParticipant.getRole() == HmilyRoleEnum.START.getCode()) {