Example15.java: Oracle NOSQL Database接続プーリング

この例では、Oracle Databaseの接続プーリングを使用します。

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.query.*;
import oracle.rdf.kv.client.jena.*;

public class Example15
{
  
  public static void main(String[] args) throws Exception
  {
    
String szStoreName  = args[0];
String szHostName   = args[1];
String szHostPort   = args[2];
String szModelName  = args[3];
int iPoolSize = Integer.parseInt(args[4]);
    
// 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,
                                               iPoolSize, 
                                               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();
model = OracleModelNoSql.createOracleModelNoSql(szModelName, conn);
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();    
  }
}

この例をコンパイルして実行するコマンドと、想定されるJavaコマンドの出力は次のとおりです。

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example15.java

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example15 <store_name> \
<host_name> <host_port> <graph_name>

Creating OracleNoSQL pool
Done creating OracleNoSql pool
Get a connection from the pool
Clear model
Get a connection from the pool
Execute query select ?x ?y ?z WHERE {?x ?y ?z}
----------------------------------------
| x        | y            | z          |
========================================
| <u:John> | <u:cousinOf> | <u:Jackie> |
----------------------------------------
Close pool, this will close all resources