ODatabaseException:数据库实例未在当前线程中设置
ODatabaseException: Database instance is not set in current thread
我是OrientDB新手,使用orientdb-community-1.7.4版本。我有两个项目。 Project A 用于操作 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");
但是我收到了这个错误。当我尝试在项目 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>
我认为这是 orientdb 中的一个错误,因为当我在 32 位 Windows 7 上尝试它时,它有效,但是在 OS X 上我得到了这个错误。
This solution 也适用于 OSX。
你应该使用 2.x 版本
database.activateOnCurrentThread(); //v 2.1
// for OrientDB v. 2.0.x: database.setCurrentDatabaseInThreadLocal();
我是OrientDB新手,使用orientdb-community-1.7.4版本。我有两个项目。 Project A 用于操作 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");
但是我收到了这个错误。当我尝试在项目 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>
我认为这是 orientdb 中的一个错误,因为当我在 32 位 Windows 7 上尝试它时,它有效,但是在 OS X 上我得到了这个错误。
This solution 也适用于 OSX。
你应该使用 2.x 版本
database.activateOnCurrentThread(); //v 2.1
// for OrientDB v. 2.0.x: database.setCurrentDatabaseInThreadLocal();