如何修复Race-condition,创建了重复的类Error

时间:2015-11-09 18:58:31

标签: java multithreading mongodb tomcat morphia

当我启动我的tomcat服务器时,我收到此错误: 2015 10:25:50 PM org.mongodb.morphia.mapping.MappedClass getOrCreateInstance 严重:竞争条件,创建重复类:class com.calendar.model.watchers.AccountWatcher

据我所知,它来自Morphia,但我不确定为什么会发生这种情况或如何解决它。

Morphia中错误似乎来自的代码是:

private Object getOrCreateInstance(Class<?> clazz) {
    if (mapr.instanceCache.containsKey(clazz))
        return mapr.instanceCache.get(clazz);

    Object o = mapr.getOptions().objectFactory.createInstance(clazz);
    Object nullO = mapr.instanceCache.put(clazz, o);
    if (nullO != null)
        if(log.isErrorEnabled())
            log.error("Race-condition, created duplicate class: " + clazz);

    return o;

}

AccountWatcher的代码是:

package com.calendar.model.watchers;

import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;  
import org.mongodb.morphia.annotations.PreLoad;
import org.mongodb.morphia.annotations.PrePersist;
import com.calendar.exception.DataAccessException;
import com.calendar.model.Account;
import com.calendar.model.Partner;
import com.calendar.util.MongoUtils;

public class AccountWatcher {
  final static Logger log = LoggerFactory.getLogger(AccountWatcher.class);

  @PrePersist
  void prePersist(Account account) {
    if (account.getId() == null) {// This is for create
      account.setId(MongoUtils.getGuid());
      account.setDateCreated(new Date());
      Partner owner = account.getOwner();
      if (owner == null || StringUtils.isEmpty(owner.getId())) {
        throw new DataAccessException("Owner :" + owner
            + " doesn't exist for account :" + account);
      }
    } else { // This is for update

    }
    account.setDateModified(new Date());
  }

  @PreLoad
  void preLoad(Account account) {
    if (log.isDebugEnabled()) {
      log.debug("Account watcher @PreLoad executing ...");
    }
  }
}

Account类的代码是:

package com.calendar.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.Data;
import lombok.ToString;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Typing;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.EntityListeners;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import org.mongodb.morphia.annotations.Reference;
import com.calendar.model.serializers.PartnerDeSerializer;
import com.calendar.model.serializers.PartnerListSerializer;
import com.calendar.model.serializers.PartnerSerializer;
import com.calendar.model.watchers.AccountWatcher;

@ToString
@EntityListeners(AccountWatcher.class)
@Entity("accounts")
@Data
public class Account {
  @Id
  private String id;
  private String name;
  private String timezone;

  @JsonSerialize(using = PartnerSerializer.class, typing = Typing.STATIC)
  @JsonDeserialize(using = PartnerDeSerializer.class)
  @Reference
  private Partner owner;

  @JsonSerialize(using = PartnerListSerializer.class, typing = Typing.STATIC)
  @Reference
  private List<Partner> associatedPartners;

  @JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
  private Date dateCreated;
  @JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
  private Date dateModified;
  @Indexed(unique = true, dropDups = true)
  private String externalId;
  private Integer externalVersionNumber;
  private List<EList> elists;

  public Account() {
    associatedPartners = new ArrayList<Partner>();
  }
}

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

啊,我明白了。我不确定为什么决定将其记录为错误(相反,为什么逻辑不同步),但它只是一个记录消息。这不是一个真正的问题,除非你的监听器是可变的并且有两个实例是一个实际的问题。