當我啓動我的tomcat服務器時,出現此錯誤: 2015 10:25:50 PM org.mongodb.morphia.mapping.MappedClass getOrCreateInstance SEVERE:Race-condition,創建重複的類:class com.calendar.model.watchers.AccountWatcher如何解決Race-condition,創建重複類錯誤
從我可以告訴它來自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 ...");
}
}
}
併爲該帳戶類的代碼是:
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>();
}
}
在此先感謝您提供任何幫助。
實際的堆棧跟蹤會有所幫助。 – evanchooly
你可以使用http://vmlens.com。它應該檢測所有的數據競賽。 –
@evanchooly這個錯誤沒有stackrace。該錯誤來自morphia,它所做的只是記錄該錯誤以及它遇到問題的類。我會更新我的原始問題,以包含來自Morphia的記錄錯誤的代碼。謝謝托馬斯,我會仔細研究一下。 – Ben