You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/18 07:34:15 UTC
[incubator-servicecomb-saga] 01/04: SCB-249 replaced hibernate with
eclipselink to comply with APL2
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 ce221356520868a0a0e4d1b642365acec16f7f22
Author: seanyinx <se...@huawei.com>
AuthorDate: Wed Jan 17 15:53:14 2018 +0800
SCB-249 replaced hibernate with eclipselink to comply with APL2
Signed-off-by: seanyinx <se...@huawei.com>
---
alpha/alpha-core/pom.xml | 4 +
.../servicecomb/saga/alpha/core/TxEvent.java | 10 +++
alpha/alpha-server/pom.xml | 14 ++++
.../servicecomb/saga/alpha/server/AlphaConfig.java | 2 +
.../alpha/server/JpaBaseConfigurationImpl.java | 50 +++++++++++
.../saga/alpha/server/SpringTxEventRepository.java | 4 +-
.../saga/alpha/server/TxEventEnvelope.java | 96 ----------------------
.../alpha/server/TxEventEnvelopeRepository.java | 38 +++++----
.../saga/alpha/server/AlphaIntegrationTest.java | 6 +-
alpha/alpha-server/src/test/resources/schema.sql | 13 +++
pom.xml | 31 +++++++
11 files changed, 150 insertions(+), 118 deletions(-)
diff --git a/alpha/alpha-core/pom.xml b/alpha/alpha-core/pom.xml
index 4a6c1b0..aa74718 100644
--- a/alpha/alpha-core/pom.xml
+++ b/alpha/alpha-core/pom.xml
@@ -38,6 +38,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>javax.persistence-api</artifactId>
+ </dependency>
<dependency>
<groupId>junit</groupId>
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 ebea44c..37a29f1 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
@@ -19,7 +19,17 @@ package org.apache.servicecomb.saga.alpha.core;
import java.util.Date;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
public class TxEvent {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long surrogateId;
+
private String serviceName;
private String instanceId;
private Date creationTime;
diff --git a/alpha/alpha-server/pom.xml b/alpha/alpha-server/pom.xml
index 304f6c8..6335c91 100644
--- a/alpha/alpha-server/pom.xml
+++ b/alpha/alpha-server/pom.xml
@@ -65,6 +65,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>org.hibernate</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>org.eclipse.persistence.jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
@@ -115,6 +125,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
+ <plugin>
+ <groupId>com.ethlo.persistence.tools</groupId>
+ <artifactId>eclipselink-maven-plugin</artifactId>
+ </plugin>
</plugins>
</build>
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaConfig.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaConfig.java
index f970ecb..bb4ba89 100644
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaConfig.java
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaConfig.java
@@ -31,9 +31,11 @@ import org.apache.servicecomb.saga.alpha.core.PushBackOmegaCallback;
import org.apache.servicecomb.saga.alpha.core.TxConsistentService;
import org.apache.servicecomb.saga.alpha.core.TxEventRepository;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+@EntityScan(basePackages = "org.apache.servicecomb.saga.alpha")
@Configuration
class AlphaConfig {
private final BlockingQueue<Runnable> pendingCompensations = new LinkedBlockingQueue<>();
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/JpaBaseConfigurationImpl.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/JpaBaseConfigurationImpl.java
new file mode 100644
index 0000000..0bf0fad
--- /dev/null
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/JpaBaseConfigurationImpl.java
@@ -0,0 +1,50 @@
+/*
+ * 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.alpha.server;
+
+import java.util.Collections;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.springframework.beans.factory.ObjectProvider;
+import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
+import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
+import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
+import org.springframework.transaction.jta.JtaTransactionManager;
+
+@Configuration
+class JpaBaseConfigurationImpl extends JpaBaseConfiguration {
+ JpaBaseConfigurationImpl(DataSource dataSource,
+ JpaProperties properties,
+ ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
+ super(dataSource, properties, jtaTransactionManagerProvider);
+ }
+
+ @Override
+ protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
+ return new EclipseLinkJpaVendorAdapter();
+ }
+
+ @Override
+ protected Map<String, Object> getVendorProperties() {
+ return Collections.singletonMap("eclipselink.weaving", "false");
+ }
+}
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/SpringTxEventRepository.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/SpringTxEventRepository.java
index 00d10d2..f0743e9 100644
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/SpringTxEventRepository.java
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/SpringTxEventRepository.java
@@ -31,7 +31,7 @@ class SpringTxEventRepository implements TxEventRepository {
@Override
public void save(TxEvent event) {
- eventRepo.save(new TxEventEnvelope(event));
+ eventRepo.save(event);
}
@Override
@@ -41,7 +41,7 @@ class SpringTxEventRepository implements TxEventRepository {
@Override
public TxEvent findFirstTransaction(String globalTxId, String localTxId, String type) {
- return eventRepo.findFirstByEventGlobalTxIdAndEventLocalTxIdAndEventType(globalTxId, localTxId, type).event();
+ return eventRepo.findFirstByGlobalTxIdAndLocalTxIdAndType(globalTxId, localTxId, type);
}
@Override
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelope.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelope.java
deleted file mode 100644
index f4b4c66..0000000
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelope.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * 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.alpha.server;
-
-import java.util.Date;
-
-import javax.persistence.Embedded;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Id;
-
-import org.apache.servicecomb.saga.alpha.core.TxEvent;
-
-@Entity
-class TxEventEnvelope {
- @Id
- @GeneratedValue
- private long surrogateId;
-
- @Embedded
- private TxEvent event;
-
- private TxEventEnvelope() {
- }
-
- TxEventEnvelope(TxEvent event) {
- this.event = event;
- }
-
- public TxEventEnvelope(
- String serviceName,
- String instanceId,
- String globalTxId,
- String localTxId,
- String parentTxId,
- String type,
- String compensationMethod,
- byte[] payloads) {
- this.event = new TxEvent(serviceName, instanceId, new Date(), globalTxId, localTxId, parentTxId, type, compensationMethod, payloads);
- }
-
- String serviceName() {
- return event.serviceName();
- }
-
- String instanceId() {
- return event.instanceId();
- }
-
- public long creationTime() {
- return event.creationTime().getTime();
- }
-
- String globalTxId() {
- return event.globalTxId();
- }
-
- String localTxId() {
- return event.localTxId();
- }
-
- String parentTxId() {
- return event.parentTxId();
- }
-
- String type() {
- return event.type();
- }
-
- String compensationMethod() {
- return event.compensationMethod();
- }
-
- byte[] payloads() {
- return event.payloads();
- }
-
- TxEvent event() {
- return event;
- }
-}
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelopeRepository.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelopeRepository.java
index 1e1859c..bdf82f1 100644
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelopeRepository.java
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/TxEventEnvelopeRepository.java
@@ -23,29 +23,33 @@ import org.apache.servicecomb.saga.alpha.core.TxEvent;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
-interface TxEventEnvelopeRepository extends CrudRepository<TxEventEnvelope, Long> {
- List<TxEventEnvelope> findByEventGlobalTxId(String globalTxId);
+interface TxEventEnvelopeRepository extends CrudRepository<TxEvent, Long> {
+ List<TxEvent> findByGlobalTxId(String globalTxId);
@Query("SELECT DISTINCT new org.apache.servicecomb.saga.alpha.core.TxEvent("
- + "t.event.serviceName, t.event.instanceId, t.event.globalTxId, t.event.localTxId, t.event.parentTxId, t.event.type, t.event.compensationMethod, t.event.payloads"
- + ") FROM TxEventEnvelope t "
- + "WHERE t.event.globalTxId = ?1 AND t.event.type = ?2")
+ + "t.serviceName, t.instanceId, t.globalTxId, t.localTxId, t.parentTxId, t.type, t.compensationMethod, t.payloads"
+ + ") FROM TxEvent t "
+ + "WHERE t.globalTxId = ?1 AND t.type = ?2")
List<TxEvent> findByEventGlobalTxIdAndEventType(String globalTxId, String type);
- TxEventEnvelope findFirstByEventGlobalTxIdAndEventLocalTxIdAndEventType(String globalTxId, String localTxId, String type);
+ TxEvent findFirstByGlobalTxIdAndLocalTxIdAndType(String globalTxId, String localTxId, String type);
@Query("SELECT DISTINCT new org.apache.servicecomb.saga.alpha.core.TxEvent("
- + "t.event.serviceName, t.event.instanceId, t.event.globalTxId, t.event.localTxId, t.event.parentTxId, t.event.type, t.event.compensationMethod, t.event.payloads"
- + ") FROM TxEventEnvelope t "
- + "WHERE t.event.globalTxId = ?1 AND t.event.type = 'TxStartedEvent' AND EXISTS ( "
- + " FROM TxEventEnvelope t1 "
- + " WHERE t1.event.globalTxId = ?1 "
- + " AND t1.event.localTxId = t.event.localTxId "
- + " AND t1.event.type = 'TxEndedEvent'"
+ + "t.serviceName, t.instanceId, t.globalTxId, t.localTxId, t.parentTxId, t.type, t.compensationMethod, t.payloads"
+ + ") FROM TxEvent t "
+ + "WHERE t.globalTxId = ?1 AND t.type = 'TxStartedEvent'"
+ + "AND EXISTS ("
+ + " SELECT t1.globalTxId"
+ + " FROM TxEvent t1 "
+ + " WHERE t1.globalTxId = ?1 "
+ + " AND t1.localTxId = t.localTxId "
+ + " AND t1.type = 'TxEndedEvent'"
+ ") AND NOT EXISTS ( "
- + " FROM TxEventEnvelope t2 "
- + " WHERE t2.event.globalTxId = ?1 "
- + " AND t2.event.localTxId = t.event.localTxId "
- + " AND t2.event.type = 'TxCompensatedEvent')")
+ + " SELECT t2.globalTxId"
+ + " FROM TxEvent t2 "
+ + " WHERE t2.globalTxId = ?1 "
+ + " AND t2.localTxId = t.localTxId "
+ + " AND t2.type = 'TxCompensatedEvent')"
+ )
List<TxEvent> findStartedEventsWithMatchingEndedButNotCompensatedEvents(String globalTxId);
}
diff --git a/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaIntegrationTest.java b/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaIntegrationTest.java
index 0048311..6d4f91f 100644
--- a/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaIntegrationTest.java
+++ b/alpha/alpha-server/src/test/java/org/apache/servicecomb/saga/alpha/server/AlphaIntegrationTest.java
@@ -120,11 +120,11 @@ public class AlphaIntegrationTest {
asyncStub.onConnected(serviceConfig, compensateResponseObserver);
blockingStub.onTxEvent(someGrpcEvent(TxStartedEvent));
// use the asynchronous stub need to wait for some time
- await().atMost(1, SECONDS).until(() -> !eventRepo.findByEventGlobalTxId(globalTxId).isEmpty());
+ await().atMost(1, SECONDS).until(() -> !eventRepo.findByGlobalTxId(globalTxId).isEmpty());
assertThat(receivedCommands.isEmpty(), is(true));
- TxEventEnvelope envelope = eventRepo.findByEventGlobalTxId(globalTxId).get(0);
+ TxEvent envelope = eventRepo.findByGlobalTxId(globalTxId).get(0);
assertThat(envelope.serviceName(), is(serviceName));
assertThat(envelope.instanceId(), is(instanceId));
@@ -235,7 +235,7 @@ public class AlphaIntegrationTest {
asyncStub.onConnected(serviceConfig, compensateResponseObserver);
blockingStub.onTxEvent(someGrpcEvent(TxStartedEvent));
blockingStub.onTxEvent(someGrpcEvent(TxEndedEvent));
- await().atMost(1, SECONDS).until(() -> !eventRepo.findByEventGlobalTxId(globalTxId).isEmpty());
+ await().atMost(1, SECONDS).until(() -> !eventRepo.findByGlobalTxId(globalTxId).isEmpty());
blockingStub.onTxEvent(someGrpcEvent(TxAbortedEvent));
await().atMost(1, SECONDS).until(() -> !receivedCommands.isEmpty());
diff --git a/alpha/alpha-server/src/test/resources/schema.sql b/alpha/alpha-server/src/test/resources/schema.sql
new file mode 100644
index 0000000..08945a7
--- /dev/null
+++ b/alpha/alpha-server/src/test/resources/schema.sql
@@ -0,0 +1,13 @@
+CREATE TABLE IF NOT EXISTS `TxEvent` (
+ `surrogateId` bigint NOT NULL AUTO_INCREMENT,
+ `serviceName` varchar(36) NOT NULL,
+ `instanceId` varchar(36) NOT NULL,
+ `creationTime` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
+ `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,
+ `payloads` varbinary(10240),
+ PRIMARY KEY (`surrogateId`)
+) DEFAULT CHARSET=utf8;
diff --git a/pom.xml b/pom.xml
index 9338b2f..417e0ee 100755
--- a/pom.xml
+++ b/pom.xml
@@ -61,6 +61,7 @@
<grpc.version>1.8.0</grpc.version>
<kryo.version>4.0.1</kryo.version>
<javax.transaction.version>1.2</javax.transaction.version>
+ <eclipse.link.version>2.7.1</eclipse.link.version>
</properties>
<name>ServiceComb Saga</name>
@@ -353,6 +354,16 @@
<artifactId>javax.transaction-api</artifactId>
<version>${javax.transaction.version}</version>
</dependency>
+ <dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>javax.persistence-api</artifactId>
+ <version>2.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>org.eclipse.persistence.jpa</artifactId>
+ <version>${eclipse.link.version}</version>
+ </dependency>
<!-- test dependencies -->
<dependency>
@@ -586,6 +597,26 @@
</execution>
</executions>
</plugin>
+ <plugin>
+ <groupId>com.ethlo.persistence.tools</groupId>
+ <artifactId>eclipselink-maven-plugin</artifactId>
+ <version>2.7.0</version>
+ <executions>
+ <execution>
+ <phase>process-classes</phase>
+ <goals>
+ <goal>weave</goal>
+ </goals>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>org.eclipse.persistence.jpa</artifactId>
+ <version>${eclipse.link.version}</version>
+ </dependency>
+ </dependencies>
+ </plugin>
</plugins>
</pluginManagement>
<!-- enable the rat check by default -->
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.