java - ODatabaseException : Database instance is not set in current thread while dropping database

标签 java database connection-pooling orientdb

我正在尝试使用 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);
        }
    }
}

最佳答案

尝试这个修改:

@After
public void tearDown()
{
    db.activateOnCurrentThread();
    db.drop();      
}

关于java - ODatabaseException : Database instance is not set in current thread while dropping database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483679/

相关文章:

java - Jena 未从导入的 RDF 文件加载数据

java - Android 应用程序立即停止工作

数据库功能依赖分解

sql - Azure SQL 数据库查询性能下降(查询存储的原因?)

java - 没有分配变量的 getConnection() 将泄漏连接

spring - 在 Spring MVC 中设置连接池

java - Spring Webflow : How to change the destination of a transition in a child flow

java - 在不失去通用性的情况下包装逆变函数接口(interface)

mysql - 查看整个 MySQL 数据库的所有外键约束

postgresql - setAutoCommit(false) 不适用于 c3p0