Ayush
Ayush

Reputation: 161

Connect to orientdb dynamically

I have a java application, which uses orientDB API (tinkerprop package) for creating Class and inserting and fetching the data from OrientDB. The DB is running on another server.

I am looking to have my application such that in case the orientDB is down and later comes up, I don't need to restart my java application to get the connection with orientDB. (connection should automatically be refreshed when DB is available)

I may have 2 nodes in the replication setup, but in the case of both nodes need to restart, my application need to restart too. ( have faced scenario when the insert was not happening due to quorum not reachable error, and finally, need to restart both servers)

I tried with below code

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

But the application terminated when I shut down the orientDB server. The below exception is thrown, when server is shutdown

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)

Upvotes: 0

Views: 312

Answers (1)

Luigi Dell&#39;Aquila
Luigi Dell&#39;Aquila

Reputation: 2814

Adding OStorageException to the catch block should be enough:

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

    }

Upvotes: 1

Related Questions