Unique constraint not checked after server shutdown/start
I have a unique constraint on a property of a class but I'm still able to insert new documents after a server shutdown/start.
In my test case, the second time that I call db.save(), the user is created even though the name is supposed to be unique...
Class UserTest where a Unique index is set on the name property:
import javax.persistence.Version;
public class UserTest {
String name;
@Version
private Integer version;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
void setVersion(Integer version) {
this.version = version;
}
Integer getVersion() {
return version;
}
}
The actual test case is the following:
import com.orientechnologies.common.exception.OException;
import com.orientechnologies.orient.core.index.OIndexManager;
import com.orientechnologies.orient.core.index.ONullOutputListener;
import com.orientechnologies.orient.core.index.OPropertyIndexDefinition;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.tx.OTransaction;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.OServerMain;
public class OrientDbServiceSaveRollback {
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void main(String[] args) throws Exception {
System.setProperty("ORIENTDB_HOME", "PATH");
System.out.println("ORIENTDB_HOME: " + System.getProperty("ORIENTDB_HOME"));
for(int i = 0; i < 2; i++) {
System.out.println("Iteration " + i);
OServer server = OServerMain.create()
.startup(OrientDbServiceImpl.class.getResourceAsStream("/orientdb-server-config.xml")).activate();
// create database if does not exist
OObjectDatabaseTx db = new OObjectDatabaseTx("local:" + System.getProperty("ORIENTDB_HOME") + "/config-db");
if(i == 0) {
db.open("admin", "admin");
db.drop();
}
if(!db.exists()) {
db.create();
db.getEntityManager().registerEntityClass(UserTest.class);
String className = UserTest.class.getSimpleName();
int clusterId = db.getClusterIdByName(className.toLowerCase());
OIndexManager indexManager = db.getMetadata().getIndexManager();
indexManager.createIndex(className + "." + "name", OClass.INDEX_TYPE.UNIQUE.name(),
new OPropertyIndexDefinition(className, "name", OType.STRING), new int[] { clusterId },
ONullOutputListener.INSTANCE);
}
if (db.isClosed()){
db.open("admin", "admin");
}
System.out.println("Count users: " + db.countClass(UserTest.class));
try {
db.begin(OTransaction.TXTYPE.OPTIMISTIC);
// Create first user
UserTest u = new UserTest();
u.setName("uniquename");
db.save(u);
db.commit();
} catch(OException e) {
System.out.println(e.getMessage());
db.rollback();
throw e;
}
System.out.println("Count users: " + db.countClass(UserTest.class));
try{
db.close();
server.shutdown();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
What version?
On 1.6.0-SNAPSHOT.
That is from final snapshot, duplication check works. Please reopen issue if problem still is reproduced on your side. ORIENTDB_HOME: /home/andrey/Development/unique-test Iteration 0
2013-09-20 09:28:45:743 INFO Loading configuration from input stream [OServerConfigurationLoaderXml] 2013-09-20 09:28:45:873 INFO OrientDB Server v1.6.0-SNAPSHOT is starting up... [OServer] 2013-09-20 09:28:45:878 INFO Databases directory: unique-test/databases [OServer] 2013-09-20 09:28:45:893 INFO Listening binary connections on 0.0.0.0:2424 (protocol v.17) [OServerNetworkListener] 2013-09-20 09:28:45:893 INFO Listening http connections on 0.0.0.0:2480 (protocol v.10) [OServerNetworkListener] 2013-09-20 09:28:45:900 INFO JMX plugin installed and active: profilerManaged=true [OJMXPlugin] 2013-09-20 09:28:45:933 INFO OrientDB Server v1.6.0-SNAPSHOT is active. [OServer]Count users: 0 Count users: 1
2013-09-20 09:28:47:389 INFO OrientDB Server is shutting down... [OServer] 2013-09-20 09:28:47:390 INFO Shutting down storage: config-db... [Orient] 2013-09-20 09:28:47:969 INFO Orient Engine shutdown complete [Orient] 2013-09-20 09:28:47:969 INFO Shutting down plugins: [OServer] 2013-09-20 09:28:47:969 INFO - automaticBackup [OServer] 2013-09-20 09:28:47:969 INFO - script-interpreter [OServer] 2013-09-20 09:28:47:969 INFO - jmx [OServer] 2013-09-20 09:28:47:969 INFO Shutting down protocols [OServer] 2013-09-20 09:28:47:970 INFO Shutting down listeners: [OServer] 2013-09-20 09:28:47:970 INFO - ONetworkProtocolBinary /0.0.0.0:2424: [OServer] 2013-09-20 09:28:47:970 INFO - ONetworkProtocolHttpDb /0.0.0.0:2480: [OServer] 2013-09-20 09:28:47:970 INFO OrientDB Server shutdown complete [OServer] 2013-09-20 09:28:47:971 INFO Loading configuration from input stream [OServerConfigurationLoaderXml] Iteration 1
2013-09-20 09:28:48:004 INFO OrientDB Server v1.6.0-SNAPSHOT is starting up... [OServer] 2013-09-20 09:28:48:004 INFO Databases directory: /home/andrey/Development/unique-test/databases [OServer] 2013-09-20 09:28:48:005 INFO Listening binary connections on 0.0.0.0:2424 (protocol v.17) [OServerNetworkListener] 2013-09-20 09:28:48:005 INFO Listening http connections on 0.0.0.0:2480 (protocol v.10) [OServerNetworkListener] 2013-09-20 09:28:48:006 INFO JMX plugin installed and active: profilerManaged=true [OJMXPlugin] 2013-09-20 09:28:48:006 INFO OrientDB Server v1.6.0-SNAPSHOT is active. [OServer]Count users: 1 Cannot index record UserTest{name:uniquename}: found duplicated key 'uniquename' in index 'UserTest.name' previously assigned to the record #9:0 Exception in thread "main" com.orientechnologies.orient.core.index.OIndexException: Cannot index record UserTest{name:uniquename}: found duplicated key 'uniquename' in index 'UserTest.name' previously assigned to the record #9:0 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.orientechnologies.common.log.OLogManager.exception(OLogManager.java:170) at com.orientechnologies.orient.core.index.OIndexTxAwareOneValue.checkEntry(OIndexTxAwareOneValue.java:49) at com.orientechnologies.orient.core.index.OClassIndexManager.checkIndexedPropertiesOnCreation(OClassIndexManager.java:530) at com.orientechnologies.orient.core.index.OClassIndexManager.checkIndexesAndAquireLock(OClassIndexManager.java:508) at com.orientechnologies.orient.core.index.OClassIndexManager.onRecordBeforeCreate(OClassIndexManager.java:62) at com.orientechnologies.orient.core.hook.ODocumentHookAbstract.onTrigger(ODocumentHookAbstract.java:232) at com.orientechnologies.orient.core.db.record.ODatabaseRecordAbstract.callbackHooks(ODatabaseRecordAbstract.java:1027) at com.orientechnologies.orient.core.tx.OTransactionOptimistic.addRecord(OTransactionOptimistic.java:276) at com.orientechnologies.orient.core.tx.OTransactionOptimistic.saveRecord(OTransactionOptimistic.java:268) at com.orientechnologies.orient.core.db.record.ODatabaseRecordTx.save(ODatabaseRecordTx.java:270) at com.orientechnologies.orient.core.db.record.ODatabaseRecordTx.save(ODatabaseRecordTx.java:38) at com.orientechnologies.orient.core.db.ODatabaseRecordWrapperAbstract.save(ODatabaseRecordWrapperAbstract.java:281) at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.save(ODatabaseDocumentTx.java:404) at com.orientechnologies.orient.object.db.OObjectDatabaseTx.save(OObjectDatabaseTx.java:430) at com.orientechnologies.orient.object.db.OObjectDatabaseTx.save(OObjectDatabaseTx.java:342) at com.orientechnologies.orient.test.internal.OrientDbServiceSaveRollback.main(OrientDbServiceSaveRollback.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)