//create a new retrieval policy:
RetrievalPolicy retrievalPolicy = new RetrievalPolicy();
//use fastest available source of data:
retrievalPolicy.setPolicyValue(RetrievalPolicy.FASTEST);
//configure to acquire new connections always:
retrievalPolicy.getConnectionConfiguration().setConnectionLifetime(
ConnectionConfiguration.ACQUIRE_RELEASE);
//create a QueryConnector for executing queries:
QueryConnector queryConnector = new QueryConnector();
//set the query connector's default retrieval
policy:
queryConnector.setRetrievalPolicy(retrievalPolicy);
//build a query:
BusinessQuery query1 = buildRelationalBusinessQuery();
//execute the query using the default retrieval
policy:
Map results =
queryConnector.retrieveQueryResultMap(Collections.singletonList(query1));
//make a copy of the old policy:
RetrievalPolicy connectionRecyclingPolicy = new RetrievalPolicy(retrievalPolicy);
//make the copied policy re-use connections:
connectionRecyclingPolicy.getConnectionConfiguration().setConnectionLifetime(
ConnectionConfiguration.USER_SESSION);
//execute the query again, using the new retrieval
policy
//(overriding the default for this call only):
Map moreResults =
queryConnector.retrieveQueryResultMap(
Collections.singletonList(query1),
connectionRecyclingPolicy);
//close the result sets just retrieved and the
server connection:
int closeConnectionAndResultSet =
QueryConnector.CloseOptions.ALL_RESULTSET_OPTIONS
| QueryConnector.CloseOptions.ALL_CONNECTION_OPTIONS;
queryConnector.closeResources(getSessionContext(), closeConnectionAndResultSet);
|