WillemJiang closed pull request #137: SCB-344 add document for how to use mysql 
as alpha's backend database
URL: https://github.com/apache/incubator-servicecomb-saga/pull/137
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/README.md b/README.md
index aa9b8400..61c9c8f8 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,10 @@ See [Booking Demo](saga-demo/pack-demo/README.md) for 
details.
 ## User Guide
 See [User Guide](docs/user_guide.md) for details.
 
+
+## FAQ
+* [How to use MySQL as alpha's backend 
database?](docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md)
+
 ## Contact Us
 * [issues](https://issues.apache.org/jira/browse/SCB)
 * [gitter](https://gitter.im/ServiceCombUsers/Lobby)
diff --git a/README_ZH.md b/README_ZH.md
index 2ea467be..ac5f8a3e 100644
--- a/README_ZH.md
+++ b/README_ZH.md
@@ -25,6 +25,9 @@ Saga?? **alpha** ? **omega**??????
 ## ????
 ?????[????](docs/user_guide_zh.md)?
 
+## ????
+* 
[????MySQL??alpha???????](docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md)
+
 ## ????
 * [??issues](https://issues.apache.org/jira/browse/SCB)
 * [gitter???](https://gitter.im/ServiceCombUsers/Lobby)
diff --git 
a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java
 
b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java
index 1e6f21be..0f016d38 100644
--- 
a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java
+++ 
b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java
@@ -25,9 +25,11 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.Table;
 import javax.persistence.Version;
 
 @Entity
+@Table(name = "Command")
 public class Command {
 
   @Id
diff --git 
a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
 
b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
index 42a202fd..b17a1209 100644
--- 
a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
+++ 
b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java
@@ -25,9 +25,11 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.Table;
 import javax.persistence.Transient;
 
 @Entity
+@Table(name = "TxEvent")
 public class TxEvent {
   @Transient
   private static final long MAX_TIMESTAMP = 253402214400000L; // 9999-12-31 
00:00:00
diff --git 
a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java
 
b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java
index 00ca2ec7..342321fd 100644
--- 
a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java
+++ 
b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java
@@ -23,9 +23,11 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.Table;
 import javax.persistence.Version;
 
 @Entity
+@Table(name = "TxTimeout")
 public class TxTimeout {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
diff --git a/alpha/alpha-server/src/main/resources/application.yaml 
b/alpha/alpha-server/src/main/resources/application.yaml
index a4790e25..43b39dc6 100644
--- a/alpha/alpha-server/src/main/resources/application.yaml
+++ b/alpha/alpha-server/src/main/resources/application.yaml
@@ -14,6 +14,10 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
+server:
+  port: 8090
+
+---
 spring:
   profiles: prd
   datasource:
@@ -23,5 +27,16 @@ spring:
     platform: postgresql
 #    continue-on-error: true
 
-server:
-  port: 8090
+---
+spring:
+  profiles: mysql
+  datasource:
+    username: saga
+    password: password
+    url: jdbc:mysql://mysql.servicecomb.io:3306/saga?useSSL=false
+    platform: mysql
+    continue-on-error: true
+  jpa:
+    properties:
+      eclipselink:
+        ddl-generation: none
\ No newline at end of file
diff --git a/alpha/alpha-server/src/main/resources/schema-mysql.sql 
b/alpha/alpha-server/src/main/resources/schema-mysql.sql
new file mode 100644
index 00000000..b0bc8d71
--- /dev/null
+++ b/alpha/alpha-server/src/main/resources/schema-mysql.sql
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+CREATE TABLE IF NOT EXISTS TxEvent (
+  surrogateId bigint NOT NULL AUTO_INCREMENT,
+  serviceName varchar(16) NOT NULL,
+  instanceId varchar(36) NOT NULL,
+  creationTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  globalTxId varchar(36) NOT NULL,
+  localTxId varchar(36) NOT NULL,
+  parentTxId varchar(36) DEFAULT NULL,
+  type varchar(50) NOT NULL,
+  compensationMethod varchar(256) NOT NULL,
+  expiryTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  payloads varbinary(10240),
+  PRIMARY KEY (surrogateId),
+  INDEX saga_events_index (surrogateId, globalTxId, localTxId, type, 
expiryTime)
+) DEFAULT CHARSET=utf8;
+
+
+CREATE TABLE IF NOT EXISTS Command (
+  surrogateId bigint NOT NULL AUTO_INCREMENT,
+  eventId bigint NOT NULL UNIQUE,
+  serviceName varchar(16) NOT NULL,
+  instanceId varchar(36) NOT NULL,
+  globalTxId varchar(36) NOT NULL,
+  localTxId varchar(36) NOT NULL,
+  parentTxId varchar(36) DEFAULT NULL,
+  compensationMethod varchar(256) NOT NULL,
+  payloads varbinary(10240),
+  status varchar(12),
+  lastModified datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  version bigint NOT NULL,
+  PRIMARY KEY (surrogateId),
+  INDEX saga_commands_index (surrogateId, eventId, globalTxId, localTxId, 
status)
+) DEFAULT CHARSET=utf8;
+
+CREATE TABLE IF NOT EXISTS TxTimeout (
+  surrogateId bigint NOT NULL AUTO_INCREMENT,
+  eventId bigint NOT NULL UNIQUE,
+  serviceName varchar(16) NOT NULL,
+  instanceId varchar(36) NOT NULL,
+  globalTxId varchar(36) NOT NULL,
+  localTxId varchar(36) NOT NULL,
+  parentTxId varchar(36) DEFAULT NULL,
+  type varchar(50) NOT NULL,
+  expiryTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  status varchar(12),
+  version bigint NOT NULL,
+  PRIMARY KEY (surrogateId),
+  INDEX saga_timeouts_index (surrogateId, expiryTime, globalTxId, localTxId, 
status)
+) DEFAULT CHARSET=utf8;
\ No newline at end of file
diff --git a/alpha/alpha-server/src/main/resources/schema-postgresql.sql 
b/alpha/alpha-server/src/main/resources/schema-postgresql.sql
index e7f774b7..41815eeb 100644
--- a/alpha/alpha-server/src/main/resources/schema-postgresql.sql
+++ b/alpha/alpha-server/src/main/resources/schema-postgresql.sql
@@ -1,3 +1,20 @@
+/*
+ * 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.
+ */
+
 CREATE TABLE IF NOT EXISTS TxEvent (
   surrogateId BIGSERIAL PRIMARY KEY,
   serviceName varchar(16) NOT NULL,
@@ -12,7 +29,7 @@ CREATE TABLE IF NOT EXISTS TxEvent (
   payloads bytea
 );
 
-CREATE INDEX IF NOT EXISTS saga_events_index ON TxEvent (surrogateId, 
globalTxId, localTxId, type);
+CREATE INDEX IF NOT EXISTS saga_events_index ON TxEvent (surrogateId, 
globalTxId, localTxId, type, expiryTime);
 
 
 CREATE TABLE IF NOT EXISTS Command (
diff --git a/alpha/alpha-server/src/test/resources/schema.sql 
b/alpha/alpha-server/src/test/resources/schema.sql
index 929c69f8..a10a4e02 100644
--- a/alpha/alpha-server/src/test/resources/schema.sql
+++ b/alpha/alpha-server/src/test/resources/schema.sql
@@ -1,3 +1,20 @@
+/*
+ * 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.
+ */
+
 CREATE TABLE IF NOT EXISTS TxEvent (
   surrogateId bigint GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT 
BY 1) PRIMARY KEY,
   serviceName varchar(36) NOT NULL,
diff --git a/docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md 
b/docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md
new file mode 100644
index 00000000..891a1244
--- /dev/null
+++ b/docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md
@@ -0,0 +1,12 @@
+**????:** ????MySQL??alpha???????
+
+**????:** ??MySQL?????????PostgreSQL????????
+1. ?`alpha/alpha-server/pom.xml`??`mysql-connector-java`????
+   ```xml
+       <dependency>
+         <groupId>mysql</groupId>
+         <artifactId>mysql-connector-java</artifactId>
+       </dependency>
+   ```
+   
+2. ?alpha???????`-Dspring.profiles.active=mysql`???????mysql??????
diff --git a/docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md 
b/docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md
new file mode 100644
index 00000000..adb44e27
--- /dev/null
+++ b/docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md
@@ -0,0 +1,12 @@
+**Problem:** How to use MySQL as alpha's backend database?
+
+**Solution:** To replace the default database from postgreSQL to MySQL:
+1. add dependency of `mysql-connector-java` in `alpha/alpha-server/pom.xml`
+   ```xml
+       <dependency>
+         <groupId>mysql</groupId>
+         <artifactId>mysql-connector-java</artifactId>
+       </dependency>
+   ```
+   
+2. activate mysql profile by specifying option 
`-Dspring.profiles.active=mysql` when booting alpha.
\ No newline at end of file
diff --git a/omega/omega-spring-tx/src/test/resources/schema.sql 
b/omega/omega-spring-tx/src/test/resources/schema.sql
index cbc4330a..e3b36c44 100644
--- a/omega/omega-spring-tx/src/test/resources/schema.sql
+++ b/omega/omega-spring-tx/src/test/resources/schema.sql
@@ -1,3 +1,20 @@
+/*
+ * 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.
+ */
+
 CREATE TABLE IF NOT EXISTS `User` (
   `id` bigint NOT NULL AUTO_INCREMENT,
   `username` varchar(36) NOT NULL,
diff --git a/saga-demo/pack-demo/README.md b/saga-demo/pack-demo/README.md
index 9eecd623..4ceedaf4 100644
--- a/saga-demo/pack-demo/README.md
+++ b/saga-demo/pack-demo/README.md
@@ -18,14 +18,28 @@ You will need:
 
 ## Running Demo
 1. run the following command to create docker images in saga project root 
folder.
-```
-mvn package -DskipTests -Pdocker -Pdemo
-```
+   ```
+   mvn package -DskipTests -Pdocker -Pdemo
+   ```
 
 2. start application up in saga/saga-demo/pack-demo with the following command
-```
-docker-compose up
-```
+   ```
+   docker-compose up
+   ```
+
+   **Note:** If you prefer to use MySQL as alpha's backend database, you need 
to try the following steps instead:
+   1. add dependency of `mysql-connector-java` in `alpha/alpha-server/pom.xml`
+      ```xml
+          <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+          </dependency>
+      ```
+   
+   2. start application up in saga/saga-demo/pack-demo with the following 
command
+      ```
+      docker-compose -f docker-compose.yaml -f docker-compose.mysql.yaml up
+      ```
 
 ## User Requests
 1. Booking 2 rooms and 2 cars, this booking will be OK.
diff --git a/saga-demo/pack-demo/docker-compose.mysql.yaml 
b/saga-demo/pack-demo/docker-compose.mysql.yaml
new file mode 100644
index 00000000..43f3b5e8
--- /dev/null
+++ b/saga-demo/pack-demo/docker-compose.mysql.yaml
@@ -0,0 +1,39 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+version: '2.1'
+
+services:
+  database:
+    image: "mysql/mysql-server:5.7"
+    hostname: mysql
+    environment:
+      - MYSQL_ROOT_PASSWORD=password
+      - MYSQL_DATABASE=saga
+      - MYSQL_USER=saga
+      - MYSQL_PASSWORD=password
+    healthcheck:
+        test: ["CMD-SHELL", "nc -z localhost 3306 &> /dev/null; echo $$?"]
+        interval: 30s
+        timeout: 10s
+        retries: 5
+
+  alpha:
+    links:
+      - "database:mysql.servicecomb.io"
+    environment:
+      - JAVA_OPTS=-Dspring.profiles.active=mysql
diff --git a/saga-demo/pack-demo/docker-compose.yaml 
b/saga-demo/pack-demo/docker-compose.yaml
index b452d242..e528c83a 100644
--- a/saga-demo/pack-demo/docker-compose.yaml
+++ b/saga-demo/pack-demo/docker-compose.yaml
@@ -18,7 +18,7 @@
 version: '2.1'
 
 services:
-  postgres:
+  database:
     image: "postgres"
     hostname: postgres
     environment:
@@ -35,11 +35,16 @@ services:
     image: "alpha-server:0.0.3-SNAPSHOT"
     hostname: alpha-server
     links:
-      - "postgres:postgresql.servicecomb.io"
+      - "database:postgresql.servicecomb.io"
     environment:
       - JAVA_OPTS=-Dspring.profiles.active=prd
+    healthcheck:
+        test: ["CMD-SHELL", "nc -z localhost 8080 &> /dev/null; echo $$?"]
+        interval: 30s
+        timeout: 10s
+        retries: 5
     depends_on:
-      postgres:
+      database:
         condition: service_healthy
 
   pack-hotel:
@@ -50,7 +55,8 @@ services:
     ports:
       - "8081:8080"
     depends_on:
-      - alpha
+      alpha:
+        condition: service_healthy
 
   pack-car:
     image: "pack-car:0.0.3-SNAPSHOT"
@@ -60,7 +66,8 @@ services:
     ports:
       - "8082:8080"
     depends_on:
-      - alpha
+      alpha:
+        condition: service_healthy
 
   pack-booking:
     image: "pack-booking:0.0.3-SNAPSHOT"
diff --git a/saga-spring/src/main/resources/schema-postgresql.sql 
b/saga-spring/src/main/resources/schema-postgresql.sql
index c4a5c70f..7a2cd1fe 100644
--- a/saga-spring/src/main/resources/schema-postgresql.sql
+++ b/saga-spring/src/main/resources/schema-postgresql.sql
@@ -1,3 +1,20 @@
+/*
+ * 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.
+ */
+
 CREATE TABLE IF NOT EXISTS SagaEventEntity (
   id BIGSERIAL PRIMARY KEY,
   sagaId varchar(36) NOT NULL,
diff --git a/saga-spring/src/test/resources/data.sql 
b/saga-spring/src/test/resources/data.sql
index d0db33eb..5cdb7d92 100644
--- a/saga-spring/src/test/resources/data.sql
+++ b/saga-spring/src/test/resources/data.sql
@@ -1,3 +1,20 @@
+/*
+ * 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.
+ */
+
 DROP TABLE IF EXISTS SagaEventEntity;
 
 CREATE TABLE `SagaEventEntity` (


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to