2015-11-09 73 views
0

當我啓動我的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>(); 
    } 
} 

在此先感謝您提供任何幫助。

+0

實際的堆棧跟蹤會有所幫助。 – evanchooly

+0

你可以使用http://vmlens.com。它應該檢測所有的數據競賽。 –

+0

@evanchooly這個錯誤沒有stackrace。該錯誤來自morphia,它所做的只是記錄該錯誤以及它遇到問題的類。我會更新我的原始問題,以包含來自Morphia的記錄錯誤的代碼。謝謝托馬斯,我會仔細研究一下。 – Ben

回答

0

啊,我明白了。我不知道爲什麼做出決定將其記錄爲錯誤(並且相反,爲什麼該邏輯不同步),但它只是一條記錄消息。這不是一個真正的問題,除非你的監聽器是可變的,並且有兩個實例是一個實際問題。

+0

聽衆是由Morphia預定義的,我沒有改變任何東西。從我所能告訴的類本身沒有任何改變,所以它似乎並不可變,儘管它爲每個實例創建了記錄器。看起來奇怪的是,它會很好,但記錄錯誤,除非聽衆默認是可變的,但它也完全有可能不是實際的問題,而Morphia只是穿過它的T並點了它的I。你的建議是否可以忽略呢?或者需要更多研究/信息? – Ben

+0

因爲AccountWatcher是無狀態的,所以你可以忽略這個錯誤。 – evanchooly

+0

好,非常感謝您的幫助! – Ben

相關問題