我试图使用OrientDB连接池,但是当我运行我的代码时,它在tearDown方法中失败并出现ODatabaseException:
com.orientechnologies.orient.core.exception.ODatabaseException: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db);
这是我的代码:
package com.orientdb.demo.repository.object;
import com.orientdb.demo.domain.Author;
import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
public class ObjectAuthorRepositoryTest {
private OObjectDatabaseTx db;
private MyObjectAuthorRepository repository = new MyObjectAuthorRepository();
@Before
public void setUp() {
db = new OObjectDatabaseTx("memory:test");
if (db.exists()) {
db.open("admin", "admin");
} else {
db.create();
}
db.setAutomaticSchemaGeneration(true);
db.getEntityManager().registerEntityClasses("com.orientdb.demo.domain");
}
@After
public void tearDown() {
db.drop(); // <--- this fails
}
@Test
public void testCount() {
db.save(new Author("Erik", Collections.emptyList()));
assertEquals(1, repository.count());
}
}
class MyObjectAuthorRepository {
private final OPartitionedDatabasePool pool;
public MyObjectAuthorRepository() {
pool = new OPartitionedDatabasePool("memory:test", "admin", "admin");
}
public long count() {
try (OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
return db.countClass(Author.class);
}
}
}
答案 0 :(得分:2)
尝试此修改:
@After
public void tearDown()
{
db.activateOnCurrentThread();
db.drop();
}