Oracle Berkeley DB Java版 - 次要密钥unicity

时间:2013-06-09 20:50:20

标签: java database berkeley-db-je

我有一个简单的实体类,它应该包含 唯一名称

@Entity
class Package {
    @PrimaryKey(sequence = "ID")
    public Long id;

    @SecondaryKey(relate = Relationship.ONE_TO_ONE)
    public String name;

    private Package() {}

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

    @Override
    public String toString() { return id + " : " + name; }
}

由于大量修改,我想使用 延迟 写入选项。这是我试过的测试及其输出。

final String dbfilename = "test01";
new File(dbfilename).mkdirs();
EnvironmentConfig config = new EnvironmentConfig().setAllowCreate(true);
Environment environment = new Environment(new File(dbfilename), config);
StoreConfig storeConfig = new StoreConfig().setAllowCreate(true).setDeferredWrite(true);
EntityStore store = new EntityStore(environment, "", storeConfig);

PrimaryIndex<Long, Package> primaryIndex = store.getPrimaryIndex(Long.class, Package.class);

try {
    primaryIndex.put(new Package("package01")); // will be put.
    primaryIndex.put(new Package("package01")); // throws exception.
} catch (UniqueConstraintException ex) {
    System.out.println(ex.getMessage());
}

store.sync(); // flush them all

// expecting to find one element
SortedMap<Long,Package> sortedMap = primaryIndex.sortedMap();
for (Package entity : sortedMap.values()) {
    System.out.println(entity);
} 

输出

(JE 5.0.73) Unique secondary key is already present
1 : package01
2 : package01

所以我的问题是,即使它在放入第二个包时抛出异常,为什么它会列出两个包。没有使用交易可以避免这种情况吗?

感谢。

0 个答案:

没有答案