2017-04-24 78 views
0

我有一個java應用程序,它使用orientDB API(tinkerprop包)來創建Class並從OrientDB插入和提取數據。數據庫正在另一臺服務器上運行。動態連接到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$2.getGraph(OrientGraphFactory.java:116) 
    at com.tinkerpop.blueprints.impls.orient.OrientGraphFactory.getNoTx(OrientGraphFactory.java:240) 
+0

你就不能檢查服務器的健康和實現循環重新連接以防萬一? –

+0

已經添加了代碼給我的問題,我試圖檢查服務器的健康狀態 – Ayush

回答

1

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

    } 
+0

謝謝@Luigi – Ayush

+0

也com.orientechnologies.common.concur.OOfflineNodeException異常 – Ayush

+0

更新了,謝謝! –