ODatabaseException:数据库实例未在当前线程中设置

时间:2015-04-06 14:15:07

标签: database-connection orientdb

我是OrientDB的新手,并使用orientdb-community-1.7.4版本。我有两个项目。项目 A 用于使用orientdb数据库(CRUD操作等)进行操作。项目 B 是一个maven项目(Liferay portlet),其中包含Project A 作为依赖项。

在项目 B 中,我创建了新的Edge对象,该对象在数据库中保留。然后我想从数据库中获取此对象:

ApplicationContext appC = new ClassPathXmlApplicationContext("ApplicationContext.xml");
OrientDatabaseConnectionManager connMan = (OrientDatabaseConnectionManager) appC.getBean("orientConnectionManager");
OrientGraph graph = connMan.getGraph();
Edge edge = graph.getEdge("#37:20");

但是我收到了这个错误。当我试图在Project A 中获取Edge对象时,我没有收到此错误。

com.orientechnologies.orient.core.exception.ODatabaseException: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db);
    at com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal.get(ODatabaseRecordThreadLocal.java:31)
    at com.orientechnologies.orient.core.id.ORecordId.getRecord(ORecordId.java:293)
    at com.tinkerpop.blueprints.impls.orient.OrientBaseGraph.getEdge(OrientBaseGraph.java:840)

OrientDatabaseConnectionManager.java (项目A)

package persistence.graphdb.graphDBConnectionInit;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;

    /**
     * Class manages connection to the graph database. 
     * See http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/Java-Tutorial:-Introduction.html
     * for the graph database instantiation
     */

    public class OrientDatabaseConnectionManager {
        private OrientGraphFactory factory;

        public OrientDatabaseConnectionManager(String path, String name, String pass) {
            factory = new OrientGraphFactory(path, name, pass).setupPool(1,10);
        }

        /**
         * Method returns graph instance from the factory's pool.
         * @return 
         */

        public OrientGraph getGraph(){
            return factory.getTx();
        } 

    }

ApplicationContext.xml (项目A)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">


    <context:property-placeholder properties-ref="graphDbProperties"/>


    <util:properties id="graphDbProperties">
        <prop key="orientEmbeddedDbPath">plocal:/Users/and/orientdb-community-1.7.4/databases/graphDb</prop>
        <prop key="orientName">admin</prop>
        <prop key="orientPass">admin</prop>
    </util:properties>

    <!-- ORIENT DB config -->
    <bean id="orientConnectionManager" class="persistence.graphdb.graphDBConnectionInit.OrientDatabaseConnectionManager">
        <constructor-arg index="0" value="${orientEmbeddedDbPath}"/>
        <constructor-arg index="1" value="${orientName}"/>
        <constructor-arg index="2" value="${orientPass}"/>
    </bean>

</beans>

2 个答案:

答案 0 :(得分:1)

使用2.x版本时,您应该使用

database.activateOnCurrentThread(); //v 2.1
// for OrientDB v. 2.0.x: database.setCurrentDatabaseInThreadLocal();

请参阅http://orientdb.com/docs/2.1/Java-Multi-Threading.html

答案 1 :(得分:0)

我认为这是orientdb中的一个错误,因为当我在32位Windows 7上尝试它时,它可以工作,但在OS X上我得到了这个错误。

This solution也适用于OSX。