动态连接到orientdb

时间:2017-04-24 11:44:26

标签: orientdb orientdb2.2

我有一个java应用程序,它使用orientDB API(tinkerprop包)来创建Class并从OrientDB插入和获取数据。数据库正在另一台服务器上运行。

我希望我的应用程序能够在以下情况下使用:如果orientDB已关闭并且稍后出现,我不需要重新启动我的java应用程序来获取与orientDB的连接。 (当DB可用时,连接应自动刷新)

我可能在复制设置中有2个节点,但是如果两个节点都需要重新启动,我的应用程序也需要重新启动。 (由于仲裁无法访问错误而导致插入未发生的情况,最后需要重新启动两个服务器)

我尝试使用以下代码

while(true)
        {
            try {

            OrientGraphFactory factory = new OrientGraphFactory("remote:localhost/testdb", "root", "root").setupPool(1,50);         
        OrientGraphNoTx graph = factory.getNoTx();
                Thread.sleep(1*30*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

但是当我关闭了orientDB服务器时应用程序终止了。当服务器关闭时,抛出以下异常

Exception in thread "main" com.orientechnologies.orient.core.exception.OStorageException: Cannot create a connection to remote server address(es): [127.0.0.1:2424]
    DB name="testdb"
    at com.orientechnologies.orient.client.remote.OStorageRemote.openRemoteDatabase(OStorageRemote.java:1960)
    at com.orientechnologies.orient.client.remote.OStorageRemote.openRemoteDatabase(OStorageRemote.java:1860)
    at com.orientechnologies.orient.client.remote.OStorageRemote.open(OStorageRemote.java:356)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.open(ODatabaseDocumentTx.java:258)
    at com.orientechnologies.orient.core.db.OPartitionedDatabasePool$DatabaseDocumentTxPooled.internalOpen(OPartitionedDatabasePool.java:447)
    at com.orientechnologies.orient.core.db.OPartitionedDatabasePool.openDatabase(OPartitionedDatabasePool.java:310)
    at com.orientechnologies.orient.core.db.OPartitionedDatabasePool.acquire(OPartitionedDatabasePool.java:268)
    at com.tinkerpop.blueprints.impls.orient.OrientBaseGraph.<init>(OrientBaseGraph.java:143)
    at com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx.<init>(OrientGraphNoTx.java:62)
    at com.tinkerpop.blueprints.impls.orient.OrientGraphFactory$2.getGraph(OrientGraphFactory.java:116)
    at com.tinkerpop.blueprints.impls.orient.OrientGraphFactory.getNoTx(OrientGraphFactory.java:240)

1 个答案:

答案 0 :(得分:1)

向catch块添加OStorageException应该足够了:

  while(true)
    {
        try {

        OrientGraphFactory factory = new OrientGraphFactory("remote:localhost/testdb", "root", "root").setupPool(1,50);         
    OrientGraphNoTx graph = factory.getNoTx();
            Thread.sleep(1*30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (OStorageException e2) {
            e2.printStackTrace();
        } catch(OOfflineNodeException e3){
            e3.printStackTrace();
        }

    }