Commit 3fc83f1e by nobodyiam

hibernate will persist every changes made to entities during the session, so in…

hibernate will persist every changes made to entities during the session, so in ConfigChangeContentBuilder case, we should clone the entity first to not trigger unnecessary writes to the db
parent 13eda7c9
...@@ -9,6 +9,7 @@ import com.ctrip.framework.apollo.core.utils.StringUtils; ...@@ -9,6 +9,7 @@ import com.ctrip.framework.apollo.core.utils.StringUtils;
import java.util.Date; import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.springframework.beans.BeanUtils;
public class ConfigChangeContentBuilder { public class ConfigChangeContentBuilder {
...@@ -22,14 +23,14 @@ public class ConfigChangeContentBuilder { ...@@ -22,14 +23,14 @@ public class ConfigChangeContentBuilder {
public ConfigChangeContentBuilder createItem(Item item) { public ConfigChangeContentBuilder createItem(Item item) {
if (!StringUtils.isEmpty(item.getKey())){ if (!StringUtils.isEmpty(item.getKey())){
createItems.add(item); createItems.add(cloneItem(item));
} }
return this; return this;
} }
public ConfigChangeContentBuilder updateItem(Item oldItem, Item newItem) { public ConfigChangeContentBuilder updateItem(Item oldItem, Item newItem) {
if (!oldItem.getValue().equals(newItem.getValue())){ if (!oldItem.getValue().equals(newItem.getValue())){
ItemPair itemPair = new ItemPair(oldItem, newItem); ItemPair itemPair = new ItemPair(cloneItem(oldItem), cloneItem(newItem));
updateItems.add(itemPair); updateItems.add(itemPair);
} }
return this; return this;
...@@ -37,7 +38,7 @@ public class ConfigChangeContentBuilder { ...@@ -37,7 +38,7 @@ public class ConfigChangeContentBuilder {
public ConfigChangeContentBuilder deleteItem(Item item) { public ConfigChangeContentBuilder deleteItem(Item item) {
if (!StringUtils.isEmpty(item.getKey())) { if (!StringUtils.isEmpty(item.getKey())) {
deleteItems.add(item); deleteItems.add(cloneItem(item));
} }
return this; return this;
} }
...@@ -48,16 +49,18 @@ public class ConfigChangeContentBuilder { ...@@ -48,16 +49,18 @@ public class ConfigChangeContentBuilder {
public String build() { public String build() {
//因为事务第一段提交并没有更新时间,所以build时统一更新 //因为事务第一段提交并没有更新时间,所以build时统一更新
Date now = new Date();
for (Item item : createItems) { for (Item item : createItems) {
item.setDataChangeLastModifiedTime(new Date()); item.setDataChangeLastModifiedTime(now);
} }
for (ItemPair item : updateItems) { for (ItemPair item : updateItems) {
item.newItem.setDataChangeLastModifiedTime(new Date()); item.newItem.setDataChangeLastModifiedTime(now);
} }
for (Item item : deleteItems) { for (Item item : deleteItems) {
item.setDataChangeLastModifiedTime(new Date()); item.setDataChangeLastModifiedTime(now);
} }
return gson.toJson(this); return gson.toJson(this);
} }
...@@ -73,4 +76,12 @@ public class ConfigChangeContentBuilder { ...@@ -73,4 +76,12 @@ public class ConfigChangeContentBuilder {
} }
} }
Item cloneItem(Item source) {
Item target = new Item();
BeanUtils.copyProperties(source, target);
return target;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment