This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git

commit 239cc4c23b0e209292ffab1d16078044427b345d
Author: imlijinting <[email protected]>
AuthorDate: Tue Sep 4 12:29:48 2018 +0800

    [SCB-820] Demo of TCC model.
---
 .../saga/demo/pack/inventory/InventoryService.java | 32 +++++---
 .../saga/demo/pack/inventory/Product.java          | 47 ++++-------
 .../demo/pack/inventory/ProductController.java     | 16 ++++
 .../saga/demo/pack/inventory/ProductDao.java       | 16 ++++
 .../pack/inventory/TccInventoryApplication.java    |  4 +-
 .../inventory/src/main/resources/application.yaml  |  2 +-
 .../demo/pack/ordering/OrderingController.java     | 38 ++++++++-
 .../saga/demo/pack/ordering/PurchaseOrder.java     | 70 +++++++++++++++++
 .../saga/demo/pack/ordering/PurchaseOrderDao.java} | 23 +-----
 .../ordering/src/main/resources/application.yaml   |  8 ++
 .../saga/demo/pack/payment/Account.java            | 90 ++++++++++++++++++++++
 .../saga/demo/pack/payment/AccountDao.java}        | 28 ++-----
 .../saga/demo/pack/payment/PaymentController.java} | 24 +-----
 .../saga/demo/pack/payment/PaymentService.java     | 89 +++++++++++++++++++++
 .../payment/src/main/resources/application.yaml    | 10 ++-
 15 files changed, 393 insertions(+), 104 deletions(-)

diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/InventoryService.java
 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/InventoryService.java
index 57279e8..f5c36d7 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/InventoryService.java
+++ 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/InventoryService.java
@@ -1,3 +1,19 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.inventory;
 
 import java.util.Objects;
@@ -15,7 +31,8 @@ public class InventoryService {
    *
    * @param productId Product ID
    * @param requiredCount Required product count
-   * @return return the reserved count, 0 if nothing is available.
+   * @return return the reserved count, 0 if nothing is available. returns 
negative value if
+   * insufficient.
    */
   @Participate(confirmMethod = "confirm", cancelMethod = "cancel")
   public Integer reserve(Long productId, int requiredCount) {
@@ -25,25 +42,22 @@ public class InventoryService {
     }
 
     // if it is sufficient
-    if (product.getAvailable() > requiredCount) {
-      product.setReserved(product.getReserved() + requiredCount); // reserve 
some product in stock
+    if (product.getInStock() > requiredCount) {
+      product.setInStock(product.getInStock() - requiredCount);
       productDao.save(product);
       return requiredCount;
     } else {
-      return product.getAvailable() - requiredCount;
+      return product.getInStock() - requiredCount;
     }
   }
 
   public void confirm(Long productId, int requiredCount) {
-    Product product = productDao.findOne(productId);
-    product.setInStock(product.getInStock() - requiredCount); // actually 
reduce the in stock count
-    product.setReserved(product.getReserved() - requiredCount); // and remove 
the reserved count
-    productDao.save(product);
+    // empty body
   }
 
   public void cancel(Long productId, int requiredCount) {
     Product product = productDao.findOne(productId);
-    product.setReserved(product.getReserved() - requiredCount); // remove the 
reserved count
+    product.setInStock(product.getInStock() + requiredCount);
     productDao.save(product);
   }
 
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/Product.java
 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/Product.java
index 2fbc707..9ddc7e4 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/Product.java
+++ 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/Product.java
@@ -1,3 +1,19 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.inventory;
 
 import java.io.Serializable;
@@ -6,7 +22,6 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.Table;
-import org.hibernate.annotations.Formula;
 
 @Entity
 @Table(name = "t_product")
@@ -23,26 +38,14 @@ public class Product implements Serializable {
    */
   private Integer inStock;
 
-  /**
-   * reserved count
-   */
-  private Integer reserved;
-
   Product() {
   }
 
-  Product(String name, Integer inStock, Integer reserved) {
+  Product(String name, Integer inStock) {
     this.name = name;
     this.inStock = inStock;
-    this.reserved = reserved;
   }
 
-  /**
-   * Available in stock
-   */
-  @Formula("in_Stock - reserved")
-  private Integer available;
-
   public Long getId() {
     return id;
   }
@@ -66,20 +69,4 @@ public class Product implements Serializable {
   public void setInStock(Integer inStock) {
     this.inStock = inStock;
   }
-
-  public Integer getReserved() {
-    return reserved;
-  }
-
-  public void setReserved(Integer reserved) {
-    this.reserved = reserved;
-  }
-
-  public Integer getAvailable() {
-    return available;
-  }
-
-  public void setAvailable(Integer available) {
-    this.available = available;
-  }
 }
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductController.java
 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductController.java
index def42a8..dbf187a 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductController.java
+++ 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductController.java
@@ -1,3 +1,19 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.inventory;
 
 import com.google.common.collect.ImmutableList;
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductDao.java
 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductDao.java
index 0b96353..42bcd4b 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductDao.java
+++ 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/ProductDao.java
@@ -1,3 +1,19 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.inventory;
 
 import org.springframework.data.jpa.repository.JpaRepository;
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
index 909e8c0..92c415c 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
+++ 
b/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
@@ -32,9 +32,9 @@ public class TccInventoryApplication {
   }
 
   @Bean
-  CommandLineRunner kickStart(ProductDao productDao) {
+  CommandLineRunner kickOff(ProductDao productDao) {
     return args -> {
-      productDao.save(new Product("Bottled water", 100, 20));
+      productDao.save(new Product("Bottled water", 100));
     };
   }
 }
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/resources/application.yaml 
b/saga-demo/tcc-spring-demo/inventory/src/main/resources/application.yaml
index c8e3e32..98b79a6 100644
--- a/saga-demo/tcc-spring-demo/inventory/src/main/resources/application.yaml
+++ b/saga-demo/tcc-spring-demo/inventory/src/main/resources/application.yaml
@@ -32,6 +32,6 @@ spring:
         implicit-strategy: 
org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
 alpha:
   cluster:
-    address: alpha-server.servicecomb.io:8080
+    address: ${alpha-server.servicecomb.io:localhost}:8080
 server:
   port: 10020
\ No newline at end of file
diff --git 
a/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/OrderingController.java
 
b/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/OrderingController.java
index 39aacc1..a17f8b7 100644
--- 
a/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/OrderingController.java
+++ 
b/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/OrderingController.java
@@ -1,20 +1,45 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.ordering;
 
 
+import java.math.BigDecimal;
+import org.apache.servicecomb.saga.omega.context.annotations.TccStart;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.client.RestTemplate;
 
 @Controller
 @RequestMapping("/ordering")
 public class OrderingController {
 
+  private RestTemplate restTemplate;
+
+  private PurchaseOrderDao purchaseOrderDao;
+
+  @TccStart
   @PostMapping("/{productId}")
   public String ordering(
       @RequestParam("requiredCount") Integer requiredCount) {
     // place an order
-
+    purchaseOrderDao.save(new PurchaseOrder("Foo", BigDecimal.TEN));
     // try to reserve some product
 
     // create a purchase order and do the payment
@@ -23,4 +48,15 @@ public class OrderingController {
 
     return "";
   }
+
+  @Autowired
+  public void setRestTemplate(RestTemplate restTemplate) {
+    this.restTemplate = restTemplate;
+  }
+
+  @Autowired
+  public void setPurchaseOrderDao(
+      PurchaseOrderDao purchaseOrderDao) {
+    this.purchaseOrderDao = purchaseOrderDao;
+  }
 }
diff --git 
a/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/PurchaseOrder.java
 
b/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/PurchaseOrder.java
new file mode 100644
index 0000000..8b4d719
--- /dev/null
+++ 
b/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/PurchaseOrder.java
@@ -0,0 +1,70 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.ordering;
+
+import java.math.BigDecimal;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "t_order")
+public class PurchaseOrder {
+
+  @Id
+  @GeneratedValue(strategy = GenerationType.AUTO)
+  private Long id;
+
+  private String username;
+
+  private BigDecimal amount;
+
+  public PurchaseOrder() {
+    this("", BigDecimal.ZERO);
+  }
+
+  public PurchaseOrder(String username, BigDecimal amount) {
+    this.username = username;
+    this.amount = amount;
+  }
+
+  public Long getId() {
+    return id;
+  }
+
+  public void setId(Long id) {
+    this.id = id;
+  }
+
+  public BigDecimal getAmount() {
+    return amount;
+  }
+
+  public void setAmount(BigDecimal amount) {
+    this.amount = amount;
+  }
+
+  public String getUsername() {
+    return username;
+  }
+
+  public void setUsername(String username) {
+    this.username = username;
+  }
+}
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
 
b/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/PurchaseOrderDao.java
similarity index 54%
copy from 
saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
copy to 
saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/PurchaseOrderDao.java
index 909e8c0..9f52a76 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
+++ 
b/saga-demo/tcc-spring-demo/ordering/src/main/java/org/apache/servicecomb/saga/demo/pack/ordering/PurchaseOrderDao.java
@@ -14,27 +14,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.servicecomb.saga.demo.pack.ordering;
 
-package org.apache.servicecomb.saga.demo.pack.inventory;
+import org.springframework.data.jpa.repository.JpaRepository;
 
-import org.apache.servicecomb.saga.omega.spring.EnableOmega;
-import org.springframework.boot.CommandLineRunner;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
+public interface PurchaseOrderDao extends JpaRepository<PurchaseOrder, Long> {
 
-@SpringBootApplication
-@EnableOmega
-public class TccInventoryApplication {
-
-  public static void main(String[] args) {
-    SpringApplication.run(TccInventoryApplication.class, args);
-  }
-
-  @Bean
-  CommandLineRunner kickStart(ProductDao productDao) {
-    return args -> {
-      productDao.save(new Product("Bottled water", 100, 20));
-    };
-  }
 }
diff --git 
a/saga-demo/tcc-spring-demo/ordering/src/main/resources/application.yaml 
b/saga-demo/tcc-spring-demo/ordering/src/main/resources/application.yaml
index d2c82e0..57785f5 100644
--- a/saga-demo/tcc-spring-demo/ordering/src/main/resources/application.yaml
+++ b/saga-demo/tcc-spring-demo/ordering/src/main/resources/application.yaml
@@ -22,6 +22,14 @@ spring:
   h2:
     console:
       enabled: true
+  jpa:
+    show-sql: true
+    generate-ddl: true
+    hibernate:
+      ddl-auto: create-drop
+      naming:
+        physical-strategy: 
org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
+        implicit-strategy: 
org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
 alpha:
   cluster:
     address: alpha-server.servicecomb.io:8080
diff --git 
a/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/Account.java
 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/Account.java
new file mode 100644
index 0000000..159453f
--- /dev/null
+++ 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/Account.java
@@ -0,0 +1,90 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.payment;
+
+import java.math.BigDecimal;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "t_account")
+public class Account {
+
+  @Id
+  @GeneratedValue(strategy = GenerationType.AUTO)
+  private Long id;
+
+  @Column(nullable = false, unique = true)
+  private String username;
+
+  @Column(nullable = false)
+  private BigDecimal balance;
+
+  @Column(nullable = false)
+  private BigDecimal reservedAmount;
+
+  public Account() {
+    this(null, BigDecimal.ZERO, BigDecimal.ZERO);
+  }
+
+  public Account(String username) {
+    this(username, BigDecimal.ZERO, BigDecimal.ZERO);
+  }
+
+  public Account(String username, BigDecimal balance, BigDecimal 
reservedAmount) {
+    this.username = username;
+    this.balance = balance;
+    this.reservedAmount = reservedAmount;
+  }
+
+  public Long getId() {
+    return id;
+  }
+
+  public void setId(Long id) {
+    this.id = id;
+  }
+
+  public String getUsername() {
+    return username;
+  }
+
+  public void setUsername(String username) {
+    this.username = username;
+  }
+
+  public BigDecimal getBalance() {
+    return balance;
+  }
+
+  public void setBalance(BigDecimal balance) {
+    this.balance = balance;
+  }
+
+  public BigDecimal getReservedAmount() {
+    return reservedAmount;
+  }
+
+  public void setReservedAmount(BigDecimal reservedAmount) {
+    this.reservedAmount = reservedAmount;
+  }
+}
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/AccountDao.java
similarity index 54%
copy from 
saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
copy to 
saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/AccountDao.java
index 909e8c0..4be494c 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
+++ 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/AccountDao.java
@@ -14,27 +14,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.servicecomb.saga.demo.pack.payment;
 
-package org.apache.servicecomb.saga.demo.pack.inventory;
+import org.springframework.data.jpa.repository.JpaRepository;
 
-import org.apache.servicecomb.saga.omega.spring.EnableOmega;
-import org.springframework.boot.CommandLineRunner;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
+public interface AccountDao extends JpaRepository<Account, Long> {
 
-@SpringBootApplication
-@EnableOmega
-public class TccInventoryApplication {
-
-  public static void main(String[] args) {
-    SpringApplication.run(TccInventoryApplication.class, args);
-  }
-
-  @Bean
-  CommandLineRunner kickStart(ProductDao productDao) {
-    return args -> {
-      productDao.save(new Product("Bottled water", 100, 20));
-    };
-  }
+  /**
+   * @param username username
+   * @return the account specified by username
+   */
+  public Account findByUsername(String username);
 }
diff --git 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/PaymentController.java
similarity index 54%
copy from 
saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
copy to 
saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/PaymentController.java
index 909e8c0..8b11c99 100644
--- 
a/saga-demo/tcc-spring-demo/inventory/src/main/java/org/apache/servicecomb/saga/demo/pack/inventory/TccInventoryApplication.java
+++ 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/PaymentController.java
@@ -14,27 +14,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.servicecomb.saga.demo.pack.payment;
 
-package org.apache.servicecomb.saga.demo.pack.inventory;
+import org.springframework.stereotype.Controller;
 
-import org.apache.servicecomb.saga.omega.spring.EnableOmega;
-import org.springframework.boot.CommandLineRunner;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
+@Controller
+public class PaymentController {
 
-@SpringBootApplication
-@EnableOmega
-public class TccInventoryApplication {
-
-  public static void main(String[] args) {
-    SpringApplication.run(TccInventoryApplication.class, args);
-  }
-
-  @Bean
-  CommandLineRunner kickStart(ProductDao productDao) {
-    return args -> {
-      productDao.save(new Product("Bottled water", 100, 20));
-    };
-  }
 }
diff --git 
a/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/PaymentService.java
 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/PaymentService.java
new file mode 100644
index 0000000..6e58748
--- /dev/null
+++ 
b/saga-demo/tcc-spring-demo/payment/src/main/java/org/apache/servicecomb/saga/demo/pack/payment/PaymentService.java
@@ -0,0 +1,89 @@
+/*
+ * 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.apache.servicecomb.saga.demo.pack.payment;
+
+import java.math.BigDecimal;
+import org.apache.servicecomb.saga.omega.transaction.annotations.Participate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+public class PaymentService {
+
+  private AccountDao accountDao;
+
+  @Transactional
+  @Participate(confirmMethod = "confirm", cancelMethod = "cancel")
+  public BigDecimal execute(String username, BigDecimal amount) {
+    Account account = accountDao.findByUsername(username);
+
+    if (account.getBalance()
+        .subtract(account.getReservedAmount())
+        .compareTo(amount) > 0) {
+
+      account.setReservedAmount(account.getReservedAmount().add(amount));
+      accountDao.save(account);
+      return amount;
+    } else {
+      throw new AccountException("Insufficient funds");
+    }
+  }
+
+  @Transactional
+  public void confirm(String username, BigDecimal amount) {
+    Account account = accountDao.findByUsername(username);
+    account.setReservedAmount(account.getReservedAmount().subtract(amount));
+    account.setBalance(account.getBalance().subtract(amount));
+    accountDao.save(account);
+  }
+
+  @Transactional
+  public void cancel(String username, BigDecimal amount) {
+    Account account = accountDao.findByUsername(username);
+    account.setReservedAmount(account.getReservedAmount().subtract(amount));
+    accountDao.save(account);
+  }
+
+  @Autowired
+  public void setAccountDao(AccountDao accountDao) {
+    this.accountDao = accountDao;
+  }
+}
+
+class AccountException extends RuntimeException {
+
+  public AccountException() {
+  }
+
+  public AccountException(String message) {
+    super(message);
+  }
+
+  public AccountException(String message, Throwable cause) {
+    super(message, cause);
+  }
+
+  public AccountException(Throwable cause) {
+    super(cause);
+  }
+
+  public AccountException(String message, Throwable cause, boolean 
enableSuppression,
+      boolean writableStackTrace) {
+    super(message, cause, enableSuppression, writableStackTrace);
+  }
+}
\ No newline at end of file
diff --git 
a/saga-demo/tcc-spring-demo/payment/src/main/resources/application.yaml 
b/saga-demo/tcc-spring-demo/payment/src/main/resources/application.yaml
index 1838533..7fe65b3 100644
--- a/saga-demo/tcc-spring-demo/payment/src/main/resources/application.yaml
+++ b/saga-demo/tcc-spring-demo/payment/src/main/resources/application.yaml
@@ -16,12 +16,20 @@
 ## ---------------------------------------------------------------------------
 spring:
   application:
-    name: hotel
+    name: payment
   datasource:
     url: jdbc:h2:mem:payment-test
   h2:
     console:
       enabled: true
+  jpa:
+    show-sql: true
+    generate-ddl: true
+    hibernate:
+      ddl-auto: create-drop
+      naming:
+        physical-strategy: 
org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
+        implicit-strategy: 
org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
 alpha:
   cluster:
     address: alpha-server.servicecomb.io:8080

Reply via email to