Multiple connections

37 views
Skip to first unread message

Johan Sjöberg

unread,
Apr 29, 2015, 10:27:59 AM4/29/15
to orient-...@googlegroups.com
Hi

I'm trying to connect to two different databases at the same time. My goal is to copy records (both vertices and edges) from one database (db1) and insert it into the other database (db2). I will only be reading from db1 and only writing to db2.
The db1 is running on a remote server which I'm sshing into, thus port 2425. The db2 is running locally on my machine

ODatabaseDocumentTx db1 = new ODatabaseDocumentTx("remote:localhost:2425/db1).open("login", "cred");
ODatabaseDocumentTx db2 = new ODatabaseDocumentTx("remote:localhost:2424/db2).open("login", "cred");

I do various queries to the dbs, I'm only using SQL:

db1 -> List<ODocument> oDocs = db1.command(new OSQLSynchQuery(q_read)).execute();
db2 -> db2.command(new OCommandSQL(q_insert)).execute();

For most parts this works fine but for some records it seems like they are read from db2 instead of db1. So my theory is that somehow the databases are not safe to use in a scenario like this. 
But I'm afraid I'm very new to threads and the wiki doesn't really seem to cover my usage.

So my questions are:
1. Are the databases safe to use in a scenario like mine?
2. If not, could they cause problems like records not being read from the correct db?
3. How do I solve a problem like this?

Colin

unread,
Apr 29, 2015, 3:36:56 PM4/29/15
to orient-...@googlegroups.com
Hi Johan,

You don't mention if you're executing all this in a single thread or multiple, but here's the basic idea:

Call ODatabaseRecordThreadLocal.INSTANCE.set(db1);

Then perform your reads/write/queries involving db1.

When you need to access db2, call ODatabaseRecordThreadLocal.INSTANCE.set(db2);

Then perform your operations.

So, any time you switch between the two databases, just call the ODatabaseRecordThreadLocal.INSTANCE.set() method again with the corresponding db instance.

Hope that helps.

-Colin

Orient Technologies

The Company behind OrientDB

Reply all
Reply to author
Forward
0 new messages