Package org.exist.storage.txn

Examples of org.exist.storage.txn.TransactionManager


        try {
          pool = startDB();
          assertNotNull(pool);
          broker = pool.get(pool.getSecurityManager().getSystemSubject());
          assertNotNull(broker);           
            TransactionManager mgr = pool.getTransactionManager();
            assertNotNull(mgr);
           
            IndexInfo info = init(broker, mgr);
            assertNotNull(info);
            MutableDocumentSet docs = new DefaultDocumentSet();
            docs.add(info.getDocument());
            XUpdateProcessor proc = new XUpdateProcessor(broker, docs, AccessContext.TEST);
            assertNotNull(proc);
           
            Txn transaction = mgr.beginTransaction();
            assertNotNull(transaction);
            System.out.println("Transaction started ...");
           
            String xupdate;
            Modification modifications[];
           
            // append some new element to records
            for (int i = 1; i <= 200; i++) {
                xupdate =
                    "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" +
                    "   <xu:append select=\"/products\">" +
                    "       <product>" +
                    "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" +
                    "           <description>Product " + i + "</description>" +
                    "           <price>" + (i * 2.5) + "</price>" +
                    "           <stock>" + (i * 10) + "</stock>" +
                    "       </product>" +
                    "   </xu:append>" +
                    "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
           
            mgr.commit(transaction);
            System.out.println("Transaction commited ...");
           
            // the following transaction will not be committed and thus undone during recovery
            transaction = mgr.beginTransaction();
            assertNotNull(transaction);
            System.out.println("Transaction started ...");
           
            // update attributes
            for (int i = 1; i <= 200; i++) {
View Full Code Here


    private void store() {
        BrokerPool.FORCE_CORRUPTION = true;
        BrokerPool pool = startDB();
        DBBroker broker = null;
        Txn transaction = null;
        TransactionManager transact = null;
        try {
            broker = pool.get(pool.getSecurityManager().getSystemSubject());           
            transact = pool.getTransactionManager();
           
            transaction = transact.beginTransaction();           
            System.out.println("Transaction started ...");
           
            Collection root = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI);
            broker.saveCollection(transaction, root);
           
            Collection test = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI.append("test2"));
            broker.saveCollection(transaction, test);
           
            transact.commit(transaction);
            System.out.println("Transaction commited ...");
        } catch (Exception e) {
          transact.abort(transaction);
            fail(e.getMessage());             
        } finally {
            pool.release(broker);
        }
    }
View Full Code Here

      System.out.println("Add some random data and force db corruption ...\n");
      broker = pool.get(pool.getSecurityManager().getSystemSubject());
      assertNotNull(broker);
      //TODO : is this necessary ?
            broker.flush();
      TransactionManager mgr = pool.getTransactionManager();
      assertNotNull(mgr);
      Txn txn = mgr.beginTransaction();
      assertNotNull(txn);
      System.out.println("Transaction started ...");
           
            DOMFile domDb = ((NativeBroker) broker).getDOMFile();
            assertNotNull(domDb);
            domDb.setOwnerObject(this);
           
            // put 1000 values into the btree
            long firstToRemove = -1;
            for (int i = 1; i <= 10000; i++) {
                byte[] data = ("Value" + i).getBytes();
                NodeId id = idFact.createInstance(i);
                long addr = domDb.put(txn, new NativeBroker.NodeRef(500, id), data);
//              TODO : test addr ?
                if (i == 1)
                    firstToRemove = addr;
            }

            domDb.closeDocument();
           
            // remove all
            NativeBroker.NodeRef ref = new NativeBroker.NodeRef(500);
            assertNotNull(ref);
            IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);
            assertNotNull(idx);
            domDb.remove(txn, idx, null);
            domDb.removeAll(txn, firstToRemove);
           
            // put some more
            for (int i = 1; i <= 10000; i++) {
                byte[] data = ("Value" + i).getBytes();
                @SuppressWarnings("unused")
        long addr = domDb.put(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)), data);
//              TODO : test addr ?
            }
           
            domDb.closeDocument();
            mgr.commit(txn);
            System.out.println("Transaction commited ...");
           
            txn = mgr.beginTransaction();
            System.out.println("Transaction started ...");
           
            // put 1000 new values into the btree
            for (int i = 1; i <= 1000; i++) {
                byte[] data = ("Value" + i).getBytes();
                long addr = domDb.put(txn, new NativeBroker.NodeRef(501, idFact.createInstance(i)), data);
//              TODO : test addr ?               
                if (i == 1)
                    firstToRemove = addr;
            }           
           
            domDb.closeDocument();
            mgr.commit(txn);
            System.out.println("Transaction commited ...");
           
            // the following transaction is not committed and will be rolled back during recovery
            txn = mgr.beginTransaction();
            System.out.println("Transaction started ...");
           
            for (int i = 1; i <= 200; i++) {
                domDb.remove(txn, new NativeBroker.NodeRef(500, idFact.createInstance(i)));
            }

            idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, new NativeBroker.NodeRef(501));
            domDb.remove(txn, idx, null);
            domDb.removeAll(txn, firstToRemove);
           
//          Don't commit...
            mgr.commit(txn);
            mgr.getJournal().flushToLog(true);
            System.out.println("Transaction interrupted ...");
           
            Writer writer = new StringWriter();
            domDb.dump(writer);
            System.out.println(writer.toString());
View Full Code Here

        DBBroker broker = null;
        try {
          System.out.println("Recover and read the data ...\n");         
            broker = pool.get(pool.getSecurityManager().getSystemSubject());
            assertNotNull(broker);
            TransactionManager mgr = pool.getTransactionManager();
            assertNotNull(mgr);
           
            DOMFile domDb = ((NativeBroker) broker).getDOMFile();
            assertNotNull(domDb);
            domDb.setOwnerObject(this);
View Full Code Here

    private BinaryDocument storeBinary(String name,  String data, String mimeType) throws EXistException {
      BinaryDocument binaryDoc = null;
        Database pool = BrokerPool.getInstance();;

        DBBroker broker = null;
        TransactionManager transact = null;
        Txn transaction = null;
        try {
            broker = pool.get(pool.getSecurityManager().getSystemSubject());
            assertNotNull(broker);
            transact = pool.getTransactionManager();
            assertNotNull(transact);
            transaction = transact.beginTransaction();
            assertNotNull(transaction);

            Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        broker.saveCollection(transaction, root);
            assertNotNull(root);

            binaryDoc = root.addBinaryResource(transaction, broker, XmldbURI.create(name), data.getBytes(), mimeType);

            if(transact != null) {
                transact.commit(transaction);
            }
        } catch (Exception e) {
            if (transact != null)
                transact.abort(transaction);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            pool.release(broker);
        }
View Full Code Here

        try {
          pool = startDB();
          assertNotNull(pool);
            broker = pool.get(pool.getSecurityManager().getSystemSubject());
            assertNotNull(broker);           
            TransactionManager transact = pool.getTransactionManager();
            assertNotNull(transact);
            Txn transaction = transact.beginTransaction();
            assertNotNull(transaction);           
            System.out.println("Transaction started ...");
           
            Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(root);
            broker.saveCollection(transaction, root);
           
            Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
            broker.saveCollection(transaction, test2);
           
           
            File files[] = dir.listFiles();
            assertNotNull(files);
           
            File f;
            IndexInfo info;
           
            BinaryDocument doc = test2.addBinaryResource(transaction, broker, TestConstants.TEST_BINARY_URI, "Some text data".getBytes(), null);
            assertNotNull(doc);
           
            // store some documents. Will be replaced below
            for (int i = 0; i < files.length; i++) {
                f = files[i];
                try {
                    info = test2.validateXMLResource(transaction, broker, XmldbURI.create(f.getName()), new InputSource(f.toURI().toASCIIString()));
                    assertNotNull(info);
                    test2.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false);
                } catch (SAXException e) {
//                  TODO : why pass invalid couments ?
                    System.err.println("Error found while parsing document: " + f.getName() + ": " + e.getMessage());
                }
            }
           
            // replace some documents
            for (int i = 0; i < files.length; i++) {
                f = files[i];
                try {
                    info = test2.validateXMLResource(transaction, broker, XmldbURI.create(f.getName()), new InputSource(f.toURI().toASCIIString()));
                    assertNotNull(info);
                    test2.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false);
                } catch (SAXException e) {
//                  TODO : why pass invalid couments ?
                    System.err.println("Error found while parsing document: " + f.getName() + ": " + e.getMessage());
                }
            }
   
            info = test2.validateXMLResource(transaction, broker, XmldbURI.create("test_string.xml"), TEST_XML);
            assertNotNull(info);
            //TODO : unlock the collection here ?
           
            test2.store(transaction, broker, info, TEST_XML, false);           
            // remove last document
            test2.removeXMLResource(transaction, broker, XmldbURI.create(files[files.length - 1].getName()));           
           
            transact.commit(transaction);
            System.out.println("Transaction commited ...");
           
            // the following transaction will not be committed. It will thus be rolled back by recovery
            transaction = transact.beginTransaction();
            System.out.println("Transaction started ...");
           
            test2.removeXMLResource(transaction, broker, XmldbURI.create(files[0].getName()));           
            test2.removeBinaryResource(transaction, broker, doc);
           
//          Don't commit...           
            transact.getJournal().flushToLog(true);
            System.out.println("Transaction interrupted ...");
           
            //DOMFile domDb = ((NativeBroker)broker).getDOMFile();
            //assertNotNull(domDb);
            //Writer writer = new StringWriter();
View Full Code Here

    private void read() {
        BrokerPool.FORCE_CORRUPTION = false;
        BrokerPool pool = null;
        DBBroker broker = null;          
        TransactionManager transact = null;
        Txn transaction = null;
       
        try {
          System.out.println("testRead() ...\n");
          pool = startDB();
          assertNotNull(pool);
            broker = pool.get(pool.getSecurityManager().getSystemSubject());
            Serializer serializer = broker.getSerializer();
            serializer.reset();
           
            DocumentImpl doc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), Lock.READ_LOCK);
            assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", doc);
            String data = serializer.serialize(doc);
            assertNotNull(data);
            //System.out.println(data);
            doc.getUpdateLock().release(Lock.READ_LOCK);
           
            doc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), Lock.READ_LOCK);
            assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", doc);
            data = serializer.serialize(doc);
            assertNotNull(data);
            //System.out.println(data);
            doc.getUpdateLock().release(Lock.READ_LOCK);
           
            File files[] = dir.listFiles();
            assertNotNull(files);
           
            doc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(files[files.length - 1].getName()), Lock.READ_LOCK);
            assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + files[files.length - 1].getName() + " should not exist anymore", doc);
           
            XQuery xquery = broker.getXQueryService();
            assertNotNull(xquery);
            Sequence seq = xquery.execute("//SPEECH[ft:query(LINE, 'king')]", null, AccessContext.TEST);
            assertNotNull(seq);
            System.out.println("Found: " + seq.getItemCount());
            for (SequenceIterator i = seq.iterate(); i.hasNext(); ) {
                Item next = i.nextItem();
                String value = serializer.serialize((NodeValue) next);
                //System.out.println(value);
            }
           
            BinaryDocument binDoc = (BinaryDocument) broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), Lock.READ_LOCK);
            assertNotNull("Binary document is null", binDoc);
            InputStream is = broker.getBinaryResource(binDoc);
            byte [] bdata = new byte[(int)broker.getBinaryResourceSize(binDoc)];
            is.read(bdata);
            is.close();
            data = new String(bdata);
            assertNotNull(data);
            System.out.println(data);
           
            DOMFile domDb = ((NativeBroker)broker).getDOMFile();
            assertNotNull(domDb);
            Writer writer = new StringWriter();
            domDb.dump(writer);
            //System.out.println(writer.toString());
           
            transact = pool.getTransactionManager();
            assertNotNull(transact);
            transaction = transact.beginTransaction();
            assertNotNull(transaction);
            System.out.println("Transaction started ...");
           
            Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, Lock.WRITE_LOCK);
            assertNotNull(root);
            transaction.registerLock(root.getLock(), Lock.WRITE_LOCK);           
            broker.removeCollection(transaction, root);
           
            transact.commit(transaction);
            System.out.println("Transaction commited ...");
      } catch (Exception e) {        
               if (transact!=null) {
        transact.abort(transaction);
               }
          fail(e.getMessage());
          e.printStackTrace();
        } finally {
          if (pool != null) pool.release(broker);
View Full Code Here

    try {
      pool = startDB();
      assertNotNull(pool);
      broker = pool.get(pool.getSecurityManager().getSystemSubject());
      assertNotNull(broker);
      TransactionManager transact = pool.getTransactionManager();
      assertNotNull(transact);
      Txn transaction = transact.beginTransaction();
      assertNotNull(transaction);
      System.out.println("Transaction started ...");

      Collection root = broker.getOrCreateCollection(transaction,  TestConstants.TEST_COLLECTION_URI);
      assertNotNull(root);
      broker.saveCollection(transaction, root);

      Collection test2 = broker.getOrCreateCollection(transaction,TestConstants.TEST_COLLECTION_URI2);
      assertNotNull(test2);
      broker.saveCollection(transaction, test2);

            String existHome = System.getProperty("exist.home");
            File existDir = existHome==null ? new File(".") : new File(existHome);
      File f = new File(existDir,"samples/shakespeare/r_and_j.xml");
      assertNotNull(f);
      IndexInfo info = test2.validateXMLResource(transaction, broker, TestConstants.TEST_XML_URI, new InputSource(f.toURI().toASCIIString()));
      assertNotNull(info);
      test2.store(transaction, broker, info, new InputSource(f.toURI().toASCIIString()), false);

            System.out.println("Moving document test.xml to new_test.xml ...");
      broker.moveResource(transaction, info.getDocument(), root, XmldbURI.create("new_test.xml"));
      broker.saveCollection(transaction, root);

      transact.commit(transaction);
      System.out.println("Transaction commited ...");
    } catch (Exception e) {
            e.printStackTrace();
          fail(e.getMessage());
    } finally {
View Full Code Here

  private void doRead() {
      BrokerPool.FORCE_CORRUPTION = false;
      BrokerPool pool = null;
      DBBroker broker = null;
        TransactionManager transact = null;
        Txn transaction = null;
      try {
        System.out.println("testRead() ...\n");
        pool = startDB();
        assertNotNull(pool);
          broker = pool.get(pool.getSecurityManager().getSystemSubject());
          assertNotNull(broker);
          Serializer serializer = broker.getSerializer();
          serializer.reset();

          DocumentImpl doc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/new_test.xml"), Lock.READ_LOCK);
          assertNotNull("Document should not be null", doc);
          String data = serializer.serialize(doc);
          assertNotNull(data);
//          System.out.println(data);
          doc.getUpdateLock().release(Lock.READ_LOCK);

            transact = pool.getTransactionManager();
            assertNotNull(transact);
            transaction = transact.beginTransaction();
            assertNotNull(transaction);
            System.out.println("Transaction started ...");

            Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, Lock.WRITE_LOCK);
            assertNotNull(root);
            transaction.registerLock(root.getLock(), Lock.WRITE_LOCK);
            broker.removeCollection(transaction, root);

            transact.commit(transaction);
            System.out.println("Transaction commited ...");
      } catch (Exception e) {
            transact.abort(transaction);
            e.printStackTrace();
          fail(e.getMessage());
      } finally {
          pool.release(broker);
      }
View Full Code Here

    DBBroker broker = null;
    try {
      broker = pool.get(pool.getSecurityManager().getSystemSubject());

      TransactionManager transact = pool.getTransactionManager();
      Txn transaction = transact.beginTransaction();

      System.out.println("Transaction started ...");

      Collection root = broker.getOrCreateCollection(transaction,  TestConstants.TEST_COLLECTION_URI);
      assertNotNull(root);
      broker.saveCollection(transaction, root);

      Collection test2 = broker.getOrCreateCollection(transaction,TestConstants.TEST_COLLECTION_URI2);
      assertNotNull(test2);
      broker.saveCollection(transaction, test2);

            String existHome = System.getProperty("exist.home");
            File existDir = existHome==null ? new File(".") : new File(existHome);
      File f = new File(existDir,"samples/shakespeare/r_and_j.xml");
      assertNotNull(f);
            IndexInfo info = test2.validateXMLResource(transaction, broker, XmldbURI.create("new_test2.xml"),
          new InputSource(f.toURI().toASCIIString()));
      test2.store(transaction, broker, info, new InputSource(f.toURI()
          .toASCIIString()), false);

      transact.commit(transaction);

      transaction = transact.beginTransaction();

      broker.moveResource(transaction, info.getDocument(), root,
          XmldbURI.create("new_test2.xml"));
      broker.saveCollection(transaction, root);
View Full Code Here

TOP

Related Classes of org.exist.storage.txn.TransactionManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.