我正在使用带有键/值格式的表的Oracle Berkeley DB Java Edition。我正在尝试插入重复的键,但不断获得SecondaryIntegrityException。根据Oracle,如果setSortedDuplicates()设置为true,则允许重复。这在我的情况下不起作用。下面是一些代码,其中key = bob,value = smith。第一个我运行它,它按预期运行。如果我再次运行它只更改value = johnson,我会得到SecondaryIntegrityException。有什么我做错了吗?感谢。
String key = "bob";
String value = "smith";
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setTransactional(false);
Environment myDBenvironment = new Environment(new File(filePath), envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(false);
Database myDatabase = myDBenvironment.openDatabase(null, dbname,
dbConfig);
// create secondary database
SecondaryConfig mySecConfig = new SecondaryConfig();
mySecConfig.setAllowCreate(true);
mySecConfig.setSortedDuplicates(true);
mySecConfig.setTransactional(false);
mySecConfig.setKeyCreator(new SecondKeyCreator());
SecondaryDatabase mySecondaryDatabase = myDBenvironment
.openSecondaryDatabase(null, secdbname, myDatabase,
mySecConfig);
DatabaseEntry myKey = new DatabaseEntry(key.getBytes("UTF-8"));
Record mydata = new Record();
mydata.setobjectVal(value);
DatabaseEntry myrecord = new DatabaseEntry();
new RecordTupleBinding().objectToEntry(mydata, myrecord);
myDatabase.put(null, myKey, myrecord);
mySecondaryDatabase.close();
myDatabase.close();
myDBenvironment.close();
public class SecondKeyCreator implements SecondaryKeyCreator{
@Override
public boolean createSecondaryKey(SecondaryDatabase arg0,
DatabaseEntry key, DatabaseEntry data, DatabaseEntry secondKey) {
RecordTupleBinding binding = new RecordTupleBinding();
Record record = (Record) binding.entryToObject(data);
try {
secondKey.setData(data.getData());
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
答案 0 :(得分:0)
虽然我不是这方面的专家,但我试着帮助你。
根据Oracle文档,“如果要将主数据库与一个或多个辅助数据库相关联,则可能不会将其配置为重复数据库”。你有这个数据库的关联吗?如果是这样,这可能是原因。
我希望它有所帮助。
答案 1 :(得分:0)
需要辅助数据库且必需以允许重复。上述工作如果
secondKey.setData(data.getData());
更改为
secondKey.setData(((String)record.getobjectVal()).getBytes());