15.07.2016 Views

MARKLOGIC SERVER

Inside-MarkLogic-Server

Inside-MarkLogic-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

timestamp to make the fragment live. Because it's an update, it has to mark the old<br />

fragment as deleted. It does that by setting the deletion timestamp of the original<br />

fragment to the transaction timestamp. This combination effectively replaces the old<br />

fragment with the new. When the request concludes, it releases its locks. Our document<br />

is now deleted, replaced by a new, shinier model.<br />

It still exists on disk, of course. In fact, any query that was already in progress before the<br />

update incremented the timestamp, or any point-in-time query with an old timestamp,<br />

can still see it. Eventually, the on-disk stand holding the fragment will be merged again,<br />

and that will be the end of the line for this document. It won't be written into the new<br />

on-disk stand that is, unless the administration "merge timestamp" was set to preserve<br />

it. In that case, it will live on, sticking around for use with any subsequent point-intime<br />

queries until finally a merge happens at a timestamp that is later than its deleted<br />

timestamp and it gets merged out.<br />

MULTI-STATEMENT TRANSACTIONS<br />

As mentioned above, each statement by default acts as its own auto-commit transaction.<br />

Sometimes it's useful to extend transactions across statement boundaries. Multistatement<br />

transactions let you, for example, load a document and then process it,<br />

making it so that a failure in the processing will roll back the transaction and keep<br />

the failed document out of the database. Or, you can use a Java program to perform<br />

multiple sequential database updates, having the actions tied together as a singular unit.<br />

Multi-statement transactions aren't only for updates. A multi-statement read-only query<br />

transaction is an easy way to group multiple reads together at the same timestamp.<br />

To execute a multi-statement transaction from Java, you call<br />

session.setTransactionMode(Session.TransactionMode.UPDATE) or<br />

session.setTransactionMode(Session.TransactionMode.QUERY). The<br />

first puts you in read-write update mode (using locks) and the second puts you in<br />

read-only query mode (using a fixed timestamp). Call session.commit() or<br />

xdmp:rollback() to end the transaction. In XQuery, you control the transaction<br />

mode using the special xdmp:set-transaction-mode prolog or by passing<br />

options to xdmp:eval(), xdmp:invoke(), or xdmp:spawn(), and end it with<br />

xdmp:commit() or xdmp:rollback().<br />

An important aspect of multi-statement transactions is that each statement in the<br />

series sees the results of the previous statements, even though those changes aren't<br />

fully committed to the database and aren't visible to any other transaction contexts.<br />

MarkLogic manages this trick by keeping a set of "added" and "deleted" fragment<br />

IDs associated with each update transaction context. These act as overrides on the<br />

normal view of the database. When a statement in a multi-statement transaction<br />

adds a document, the document is placed in the database as a nascent fragment<br />

(having creation and deletion timestamps of infinity). Normal transactions won't see<br />

43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!