动态连接到orientdb

Connect to orientdb dynamically

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

我希望我的应用程序能够在 orientDB 关闭并稍后启动时,我不需要重新启动我的 java 应用程序来获得与 orientDB 的连接。 (数据库可用时应自动刷新连接)

我可能在复制设置中有 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.getGraph(OrientGraphFactory.java:116)
    at com.tinkerpop.blueprints.impls.orient.OrientGraphFactory.getNoTx(OrientGraphFactory.java:240)

将 OStorageException 添加到 catch 块应该就足够了:

  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();
        }

    }