MongoTemplate.save上出现意外的OptimisticLockingException

时间:2013-03-07 17:24:14

标签: mongodb spring-data-mongodb

使用MongoTemplate我正在尝试存储一个名为Person的实体。由于我想使用乐观锁定,因此使用@Version注释对实体进行版本控制。

当我尝试使用MongoTemplate.save将新的Person实体存储到空集合中时,我得到OptimisticLockingException。我没想到这一点,因为我正在创建一个新对象而不是更新现有对象。 (没有其他线程正在访问该集合。)

这是预期的行为,还是我做错了什么?

(如果我使用MongoOperations.insert,一切正常。(我想使用save,因为CrudRepository只有保存,而不是更新。)如果我删除@Version注释,它也可以。)

谢谢, 丹尼尔

我的实体类:

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Person {

    @Version
    private long versionId;

    @Id
    private ObjectId id;

    private final String name;

    public Person(String name) {
        this.name = name;
    }

    public long getVersionId() {
        return versionId;
    }

    public ObjectId getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (id != null ? !id.equals(person.id) : person.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}

我的测试设置(使用EmbedMongo设置MongoDb实例):

import com.mongodb.Mongo;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

public class MongoDbTest {

    private MongodExecutable mongodExe;
    private MongodProcess mongod;
    private Mongo mongo;
    private MongoOperations mongoOperations;

    @Before
    public void setUp() throws Exception {
        MongodStarter runtime = MongodStarter.getDefaultInstance();

        mongodExe = runtime.prepare(new MongodConfig(Version.Main.V2_0, 12345, Network.localhostIsIPv6()));
        mongod = mongodExe.start();
        mongo = new Mongo("localhost", 12345);

        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo, "database");

        MappingContext mappingContext = new MongoMappingContext();
        MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(mongoDbFactory, mappingContext);

        mongoOperations = new MongoTemplate(mongoDbFactory, mappingMongoConverter);
    }

    @After
    public void tearDown() {
       mongod.stop();
       mongodExe.stop();
    }

    @Test
    public void testSave() {
        Person person = new Person("Joe");
        mongoOperations.save(person);  // This call throws OptimisticLockingException.
    }
}

1 个答案:

答案 0 :(得分:9)

当version属性的类型为long时,默认情况下它的值为0,这似乎是Spring Data MongoDB无法接受的。

将Person类中的versionId类型更改为Long / Integer。

@Version
private Long versionId;