use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ConnectionIT method getConnectionCanBeExplicitlyClosed.
@Test
public void getConnectionCanBeExplicitlyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
final String query = "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "let $conn := sql:get-connection(\"" + h2Database.getDriverClass().getName() + "\", \"" + h2Database.getUrl() + "\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")\n" + "return sql:close-connection($conn)";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.getBroker();
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// execute query
final Tuple2<XQueryContext, Boolean> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result.itemAt(0).toJavaObject(boolean.class));
});
// check that the handle for the sql connection was closed
assertTrue(contextAndResult._2);
// check the connections were closed
final int connectionsCount = ModuleUtils.readContextMap(contextAndResult._1, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, connectionsCount);
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImplicitConnectionCloseIT method getJndiConnectionIsAutomaticallyClosed.
@Test
public void getJndiConnectionIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
final String mainQuery = "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "sql:get-jndi-connection(\"" + JNDI_DS_NAME + "\", \"" + STUB_JDBC_USER + "\", \"" + STUB_JDBC_PASSWORD + "\")";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source mainQuerySource = new StringSource(mainQuery);
try (final DBBroker broker = pool.getBroker();
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final XQueryContext escapedMainQueryContext = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
// execute the query
final Sequence result = executeQuery(broker, mainCompiledQuery);
// check that the handle for the sql connection that was created was valid
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof IntegerValue);
assertEquals(Type.LONG, result.itemAt(0).getType());
final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
assertFalse(connectionHandle == 0);
return mainQueryContext;
});
// check the connections map is empty
final int connectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, connectionsCount);
// check the connections from our StubDataSource, they should all be closed
final Deque<StubDataSource> createdDataSources = StubDataSourceFactory.CREATED_DATA_SOURCES;
assertEquals(1, createdDataSources.size());
final StubDataSource stubDataSource = createdDataSources.peek();
final Deque<StubConnection> createdConnections = stubDataSource.createdConnections;
assertEquals(1, createdConnections.size());
final StubConnection stubConnection = createdConnections.peek();
assertTrue(stubConnection.isClosed());
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class LocalCollectionManagementService method createCollection.
@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
final XmldbURI collName = resolve(name);
withDb((broker, transaction) -> {
try {
final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName, Optional.ofNullable(created).map(c -> new Tuple2<>(null, c.getTime())));
try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collName)) {
broker.saveCollection(transaction, coll);
}
return null;
} catch (final LockException | TriggerException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
return new LocalCollection(user, brokerPool, collection, collName);
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class IndexerTest2 method executeQuery.
private String executeQuery() throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final StringWriter out = new StringWriter()) {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final Sequence result = xquery.execute(broker, XQUERY, null);
final Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "yes");
final SAXSerializer serializer = new SAXSerializer(out, props);
serializer.startDocument();
for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
next.toSAX(broker, serializer, props);
}
serializer.endDocument();
return out.toString();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class IndexerTest2 method storeDoc.
private static void storeDoc() throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager txnMgr = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().authenticate("admin", "")));
final Txn txn = txnMgr.beginTransaction()) {
try (final Collection collection = broker.getOrCreateCollection(txn, TestConstants.TEST_COLLECTION_URI)) {
broker.storeDocument(txn, TestConstants.TEST_XML_URI2, new StringInputSource(XML), MimeType.XML_TYPE, collection);
broker.flush();
broker.saveCollection(txn, collection);
}
txnMgr.commit(txn);
}
}
Aggregations