Library Version 12.1.3.0

Connection Pooling
Prev | Chapter 3. Connect to NoSQL Database | Next

## Connection Pooling

Oracle NoSQL Database Connection Pooling is provided through the RDF Graph feature OraclePoolNoSql class. Once this class is initialized, it can return OracleNoSqlConnection objects out of its pool of available connections. These objects are essentially wrappers to Oracle NoSQL Database connections. After dispose is called on the OracleNoSqlConnection object, instead of being closed the connection is actually returned to the pool. More information about using OraclePoolNoSql can be found in the API reference information (Javadoc).

The following example sets up an OraclePoolNoSql object with three initial connections.

    public static void main(String[] args) throws Exception
    {
    String szStoreName = args[0];
    String szHostName = args[1];
    String szHostPort = args[2];
    String szModelName = args[3];

    // Property of the pool: wait if no connection is available at request.
    boolean bWaitIfBusy = true;

    System.out.println("Creating OracleNoSQL pool");
    OracleNoSqlPool pool =
                     OracleNoSqlPool.createInstance(szStoreName,
                                                    szHostName,
                                                    szHostPort,
                                                    3, // pool size
                                                    bWaitIfBusy,
                                                    true);  // lazyInit
    System.out.println("Done creating OracleNoSql pool");

    // grab an Oracle NoSQL connection and do something
    System.out.println("Get a connection from the pool");
    OracleNoSqlConnection conn = pool.getResource();

    OracleModelNoSql model =
               OracleModelNoSql.createOracleModelNoSql(szModelName, conn);

    System.out.println("Clear model");
    model.removeAll();

    model.getGraph().add(Triple.create(Node.createURI("u:John"),
                                       Node.createURI("u:cousinOf"),
                                       Node.createURI("u:Jackie")));
    model.close();

    //return connection back to the pool
    conn.dispose();

    // grab another Oracle NoSQL connection and do something
    System.out.println("Get a connection from the pool");
    conn = pool.getResource();

    String queryString = "select ?x ?y ?z WHERE {?x ?y ?z}";

    System.out.println("Execute query " + queryString);

    Query query = QueryFactory.create(queryString) ;
    QueryExecution qexec = QueryExecutionFactory.create(query, model);

    try {
          ResultSet results = qexec.execSelect();
          ResultSetFormatter.out(System.out, results, query);
        }

    finally {
          qexec.close();
        }

    model.close();

    //return connection back to the pool
        conn.dispose();

    // Close pool.
    // This will close all resources even if they have not been freed up
       System.out.println("Close pool, this will close all resources");
       pool.close();

    }

Prev | Up | Next
Chapter 3. Connect to NoSQL Database | Home | Chapter 4. Load an RDF Graph