Connect to the OrientDB server

At first, you have to create a new database. To do so, you have to instantiate the provided class ODatabaseDocumentTx with the path to your local OrientDB directory . Your database has to be located in the /databases/ sub directory!

String creds = "plocal:path/to/directory/databases/yourdatabase";
ODatabaseDocumentTx db = new ODatabaseDocumentTx (creds);

After that, we have to call the ODatabaseDocumentTx.create()-method to finally create a new database in your filesystem.

db.create();

Once you have created the database, or there has already been a usable database, you have to establish a connection to your OrientDB server. In your case, the server is running on your local host. But it's also possible to connect to a remote server. If you are using a remote storage (url starts with "remote:") assure the server is up & running and include the orientdb-client.jar file in your classpath.

To open it, pass your user name and your defined password as arguments. As standard credentials, "admin" with password "admin" are provided.

String creds = "plocal:path/to/directory/databases/yourdatabase";
ODatabaseDocumentTx db = new ODatabaseDocumentTx (creds);    
db.open("username","password")

If one of these steps failed, it could have been caused by exceptions of type OStorageException or OSecurityAccessException.It is always recommended to catch these and other exceptions and evaluate them.

OStorageException: In most cases, there is no database in the provided path. Either you have passed a wrong path, or to a non-existing or empty location. Then, you have to create a database at first.

OSecurityAccessException: This exception is caused by passing wrong (combination of) authentication credentials, e.g. if there's no such a user. To manage the security get the Security Manager and use it to work with users and roles. To change the credentials or to create a new user, first create an instance of the class OSecurity and assign the security definitions of your database to the instance. Finally, drop the user and create a new one with the class OUser.

OSecurity sm = db.getMetadata().getSecurity();
sm.dropUser("admin");
OUser user = sm.createUser(user,password, new String[] { "admin" } );
db.setUser(user);

In order to avoid creating always a new instance of the database by using the ODatabaseDocumentTx.open() method, there is a pool connection handler provided.

This handler manages the connections to your database by returning a reference of type ODatabaseDocumentTx to the database without creating a new instance. To acquire a connection, call

String creds = "plocal:path/to/directory/databases/yourdatabase"+m_path, user, password";
ODatabaseDocumentTx db = ODatabaseDocumentPool.global().acquire(creds);

Once you have finished working on the database, don't forget to release the acquired connection by calling to release resources.

db.close();

You don't have to be confused about calling ODatabaseDocumentTx.close(). The pool connection handler will check the reference of the ODatabaseDocumentTx. If it's a pool connection, the connection will be released. If there's no existing pool connection, the database will be closed.

By default OrientDB provides a global pool declared with maximum 20 instances. To create your own pool, just set it up by calling the setup(min,max)-method.

ODatabaseDocumentPool pool = new ODatabaseDocumentPool();
pool.setup(1,5);
pool.close();

Finally, never forget to call ODatabaseDocumentTx.close() when you are done with editing and working with your database.

You can find the classes used in the upper text online in the OrientDB Java documentation with all available methods for:

Also, for completion, there is a guide provided by OrientDB. You can find it here.

results matching ""

    No results matching ""