31 January 2014
31 January 2014,
 0

TinkerPop Blueprints standard doesn’t define a proper “Factory” to get graph instances. For this reason OrientDB users that wanted to use a pool of instances had to mix 2 different API: Graph and Document one. Example:
 

ODatabaseDocumentPool pool = new ODatabaseDocumentPool(“plocal:/temp/mydb”);
OrientGraph g = new OrientGraph(pool.acquire());

 
NOTE: You could also use a OGraphDatabase instance in place of ODatabaseDocumentPool, but this API has been deprecated since a long time and will be removed in v1.7.

 
Now everything is simpler, thanks to the new OrientGraphFactory class to manage graphs in easy way (Issue #1971). These the main features:
- by default acts as a factory by creating new database instances every time
- can be configured to work as a pool, by recycling database instances
- if the database doesn’t exist, it’s created automatically (but in “remote” mode)
- returns transactional and non-transactional instances
 
This is the basic way to create the factory, by using the default “admin” user (with “admin” password by default):
 

OrientGraphFactory factory = new OrientGraphFactory(“plocal:/temp/mydb”);

 
But you can also pass user and password:
 

OrientGraphFactory factory = new OrientGraphFactory(“plocal:/temp/mydb”, “jayminer”, “amigarocks”);

 
To work with a recyclable pool of instances with minimum 1, maximum 10 instances:
 

OrientGraphFactory factory = new OrientGraphFactory(“plocal:/temp/mydb”).setupPool(1, 10);

 
Once the factory is configured you can get a Graph instance to start working. OrientGraphFactory has 2 methods to retrieve a Transactional and Non-Transactional instance:
 

OrientGraph txGraph = factory.getTx();
OrientGraphNoTx noTxGraph = factory.getNoTx();

 
Or again you can configure in the factory the instances you want and use the get() method everytime:
 

factory.setTransactional(false);
OrientGraphNoTx noTxGraph = (OrientGraphNoTx) factory.get();

 
Once finished to free all the resources (in case of pool usage), call the close():
 

factory.close();

 



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>