Search in sources :

Example 76 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class CustomIndexTest method dropIndex.

@Test
public void dropIndex() throws EXistException, PermissionDeniedException, XPathException, LockException, TriggerException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        checkIndex(broker, docs, "cha", 1);
        checkIndex(broker, docs, "le8", 1);
        try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
            assertNotNull(root);
            root.removeXMLResource(transaction, broker, XmldbURI.create("test_string.xml"));
        }
        checkIndex(broker, docs, "cha", 0);
        seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        transact.commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) XQuery(org.exist.xquery.XQuery) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 77 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class CustomIndexTest method tearDown.

@After
public void tearDown() throws EXistException, PermissionDeniedException, IOException, TriggerException {
    final BrokerPool pool = BrokerPool.getInstance();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        broker.removeCollection(transaction, root);
        Collection config = broker.getOrCreateCollection(transaction, XmldbURI.create(CollectionConfigurationManager.CONFIG_COLLECTION + "/db"));
        assertNotNull(config);
        broker.removeCollection(transaction, config);
        transact.commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 78 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class IndexerTest3 method store_suppress_type.

private void store_suppress_type(final String propValue, final String xml) throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    pool.getConfiguration().setProperty(Indexer.PROPERTY_SUPPRESS_WHITESPACE, propValue);
    // Make sure to keep preserve whitespace mixed content stable even if default changes. fixme! - should test both. /ljo
    boolean propWSMValue = false;
    pool.getConfiguration().setProperty(Indexer.PROPERTY_PRESERVE_WS_MIXED_CONTENT, propWSMValue);
    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_URI, new StringInputSource(xml), MimeType.XML_TYPE, collection);
            broker.flush();
            broker.saveCollection(txn, collection);
        }
        txnMgr.commit(txn);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 79 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class DeadlockTest method startDB.

@BeforeClass
public static void startDB() throws DatabaseConfigurationException, EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, LockException, ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final Collection root = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI);
        assertNotNull(root);
        broker.saveCollection(transaction, root);
        final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(test);
        broker.saveCollection(transaction, test);
        transact.commit(transaction);
        // initialize XML:DB driver
        final Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
        final Database database = (Database) cl.newInstance();
        DatabaseManager.registerDatabase(database);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Database(org.xmldb.api.base.Database) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 80 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class ExistDocument method lock.

/**
 * Lock document.
 *
 * @param inputToken Lock token.
 * @return Input lock token.
 * @throws PermissionDeniedException Permission denied
 * @throws DocumentAlreadyLockedException Document is already locked
 * @throws EXistException Generic existdb exception
 */
public LockToken lock(LockToken inputToken) throws PermissionDeniedException, DocumentAlreadyLockedException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("create lock {}", xmldbUri);
    }
    // Try to get document
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
        final DocumentImpl document = lockedDocument.getDocument();
        if (document == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No resource found for path: {}", xmldbUri);
            }
            // return null; // throw exception?
            throw new EXistException("No resource found.");
        }
        // Get current userlock
        Account userLock = document.getUserLock();
        // Check if Resource is already locked. @@ToDo
        if (userLock != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource was already locked, ignored.");
            }
        }
        if (userLock != null && userLock.getName() != null && !userLock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource is locked by user {}.", userLock.getName());
            }
            throw new PermissionDeniedException(userLock.getName());
        }
        // Check for request for shared lock. @@TODO
        if (inputToken.getScope() == LockToken.LockScope.SHARED) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Shared locks are not implemented.");
            }
            throw new EXistException("Shared locks are not implemented.");
        }
        // Update locktoken
        inputToken.setOwner(subject.getName());
        inputToken.createOpaqueLockToken();
        // inputToken.setTimeOut(inputToken.getTimeOut());
        inputToken.setTimeOut(LockToken.LOCK_TIMEOUT_INFINITE);
        // Update document
        document.setLockToken(inputToken);
        document.setUserLock(subject);
        // Make token persistant
        final TransactionManager txnManager = brokerPool.getTransactionManager();
        try (final Txn txn = txnManager.beginTransaction()) {
            broker.storeMetadata(txn, document);
            txnManager.commit(txn);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Successfully retrieved token");
        }
        return inputToken;
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } catch (TriggerException e) {
        LOG.error(e);
        throw new EXistException(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished create lock");
        }
    }
}
Also used : Account(org.exist.security.Account) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) Txn(org.exist.storage.txn.Txn) TriggerException(org.exist.collections.triggers.TriggerException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

TransactionManager (org.exist.storage.txn.TransactionManager)131 Txn (org.exist.storage.txn.Txn)128 Collection (org.exist.collections.Collection)86 DBBroker (org.exist.storage.DBBroker)70 BrokerPool (org.exist.storage.BrokerPool)61 StringInputSource (org.exist.util.StringInputSource)27 EXistException (org.exist.EXistException)21 XmldbURI (org.exist.xmldb.XmldbURI)20 Sequence (org.exist.xquery.value.Sequence)20 XQuery (org.exist.xquery.XQuery)18 InputSource (org.xml.sax.InputSource)18 DocumentImpl (org.exist.dom.persistent.DocumentImpl)17 Test (org.junit.Test)16 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 InputStream (java.io.InputStream)14 StringReader (java.io.StringReader)13 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)13 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 IOException (java.io.IOException)12 Modification (org.exist.xupdate.Modification)12