12

I want to use OrientJS with Express.js. How do I configure a connection pool before any http request is made, acquire and release a connection from the pool during the request/response cycle, and finish the pool when I shutdown the app?

André
  • 12,497
  • 6
  • 42
  • 44
  • [OrientJS features](https://github.com/orientechnologies/orientjs#features) seems to already contain a **Connection Pooling** support. Have you tried it? Otherwise, you may handle clients with a generic resource pooling module like [this one](https://github.com/coopernurse/node-pool). – xmikex83 Aug 04 '15 at 20:08
  • No, I haven't, because I don't know how to use it. The module you've mentioned looks interesting, though. – André Aug 05 '15 at 15:54

1 Answers1

4

I've looked a bit into OrientJS source and actually found a way to use the built-in ConnectionPool.

You don't need any generic resource pooling module (as I mentioned in my comment above). Basically, it's very straightforward. All you need to do is:

var OrientDB = require('orientjs');

var server = OrientDB({
  host: 'localhost',
  port: 2424,
  username: 'root',
  password: 'yourpassword',
  pool: {
    max: 10
  }
});

Now your server object is using the built in ConnectionPool, and max allowed connections are 10.
If you check server.transport.pool, you'll see the internal pool object.

To actually check how many connections are made (or in use), you can check the length of server.transport.pool.connections (which is an array).

Another way to watch connections' use is a simple bash command:

$ watch -n 0.1 'netstat -p tcp -an | grep 2424'

And you'll see the connections.

From this point, you can start querying right away and the connection pool will be used automatically.

xmikex83
  • 945
  • 5
  • 14
  • 1
    This is cool. But now we need to figure out how to manage the connections within the http lifecycle. I think your fragment must be used when I bootstrap express js, right? Do you know how to release the connection after I build the response, and how to shutdown the pool when my server is finished? – André Aug 07 '15 at 01:45
  • 1
    I have all this care because I want to gracefully close everything and finish the pool properly, to shutdown orient db server when necessary, handling any error that may occur. It looks like orientdb sometimes corrupt files when you don't close things accordingly... – André Aug 07 '15 at 01:50
  • 1
    IMHO you have to create a module which exports the `server` variable, then require it whenever you need it in your application. Connection's acquire/release process is handled automatically by OrientJS, so you don't need to do anything, just use the server object to query your db(s). To close all the connections gracefully, simply call `server.close()`. Remember that if you want to query the db after that, you have to do again `var server = OrientDB( ... )`. – xmikex83 Aug 07 '15 at 07:36
  • I will test your solution and then I'll post here. The bounty goes to you. Thank you for your help. – André Aug 07 '15 at 08:38
  • Glad if it could help you. Thank you! – xmikex83 Aug 07 '15 at 10:40
  • This was a great help for pooling connections and still works in 2018, thank you! – mitchken Jan 05 '18 at 11:00