我是 OrientDB 的新手并使用 orientdb-community-1.7.4 版本。我有两个项目。项目 用于操作 orientdb 数据库(CRUD 操作等)。项目 B 是一个 maven 项目(Liferay portlet),其中包括项目 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.x 版本,您应该使用
database.activateOnCurrentThread(); //v 2.1
// for OrientDB v. 2.0.x: database.setCurrentDatabaseInThreadLocal();
见 http://orientdb.com/docs/2.1/Java-Multi-Threading.html
关于database-connection - ODatabaseException : Database instance is not set in current thread,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29473050/