Skip to content

Commit

Permalink
optimize: optimize spring-seata sample (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly committed May 16, 2024
1 parent 56bc1a8 commit 651c8af
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.seata.provider.config;
package org.apache.seata.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.seata.provider.config;
package org.apache.seata.config;

import io.seata.spring.annotation.GlobalTransactionScanner;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* The interface Stock service.
*/
public interface StorageService {
public interface StockService {

/**
* 扣减库存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.apache.seata.service.BusinessService;
import org.apache.seata.service.OrderService;
import org.apache.seata.service.StorageService;
import org.apache.seata.service.StockService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,7 +41,7 @@ public class BusinessServiceImpl implements BusinessService {
private static final Logger LOGGER = LoggerFactory.getLogger(BusinessService.class);

@Resource
private StorageService storageService;
private StockService stockService;
@Resource
private OrderService orderService;
private final Random random = new Random();
Expand All @@ -50,7 +50,7 @@ public class BusinessServiceImpl implements BusinessService {
@GlobalTransactional(timeoutMills = 300000, name = "spring-seata-tx")
public void purchase(String userId, String commodityCode, int orderCount) {
LOGGER.info("purchase begin ... xid: " + RootContext.getXID());
storageService.deduct(commodityCode, orderCount);
stockService.deduct(commodityCode, orderCount);
orderService.create(userId, commodityCode, orderCount);
if (random.nextBoolean()) {
throw new RuntimeException("random exception mock!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void create(String userId, String commodityCode, int orderCount) {
}

private int calculate(String commodityId, int orderCount) {
return 200 * orderCount;
return 100 * orderCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.apache.seata.service.impl;

import io.seata.core.context.RootContext;
import org.apache.seata.service.StorageService;
import org.apache.seata.service.StockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -30,9 +30,9 @@
* @author jimin.jm @alibaba-inc.com
*/
@Service
public class StorageServiceImpl implements StorageService {
public class StockServiceImpl implements StockService {

private static final Logger LOGGER = LoggerFactory.getLogger(StorageService.class);
private static final Logger LOGGER = LoggerFactory.getLogger(StockService.class);

@Resource
private JdbcTemplate jdbcTemplate;
Expand Down
5 changes: 4 additions & 1 deletion at-sample/spring-seata/src/main/resources/all.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ CREATE TABLE IF NOT EXISTS `undo_log`
`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';
ALTER TABLE `undo_log` ADD INDEX `ix_log_created` (`log_created`);
ALTER TABLE `undo_log` ADD INDEX `ix_log_created` (`log_created`);

insert into account_tbl (user_id, money) values ('U100001', 10000);
insert into stock_tbl (commodity_code, count) values ('C00321', 100);

0 comments on commit 651c8af

Please sign in to comment.