Changes

Jump to navigation Jump to search
208 bytes added ,  19:09, 20 August 2018
By default, access to external resources (files on hard disk, HTTP requests, ...) is not controlled by the transaction monitor of BaseX. You can use custom XQuery locks to do so:
===Query OptionsOption Declarations, Pragmas, Function Annotations===
{{Mark|Updated Introduced with Version 9.1}}: options were moved from {{Code|query}} to {{Code|basex}} namespacelocks via pragmas and function annotations.
* You can declare custom locks via the {{Code|basex:read-lock}} and {{Code|basex:write-lock}} options in the query prolog, pragmas or function annotations.* The value of the option contains the lock string, may contain one or multiple ones lock keys (separated with commas).
* Similar to the internal database locks, write locks block all other operations while read locks allow parallel access.
* The internal locks and XQuery locks can co-exist (there will be no conflicts, even if your lock string equals the name of a database that will be locked by the transaction manager).
Before {{Version|9.1}}, lock string were declared in the {{Code|query}} namespace. {{Code|basex:read-lock}} and {{Code|basex:write-lock}} options in the query prolog  In the following two example modulesmodule, locks have been added lock annotations are used to prevent concurrent write operations on the same file:
<pre class="brush:xquery">
module namespace read config = 'read'; (:~ Read lock on CONFIG key. :)declare option basex:read-lock 'CONFIGconfig';
declare %basex:read-lock('CONFIG') function config:read:config() {
file:read-text('config.txt')
};
</pre>
 
<pre class="brush:xquery">
module namespace write = 'write';
 
(:~ Write lock on CONFIG key. :)
declare option basex:write-lock 'CONFIG';
declare %basex:write-lock('CONFIG') function config:write:file($data) {
file:write-text('config.txt', $data)
};
Some explanations:
* If a query is parsed that is going to call calls the <code>config:read:file</code> function, a read lock will be acquired for the user-defined {{Code|CONFIG}} lock string before query evaluation.* If <code>config:write:file</code> is referenced called by a query, a write lock on this lock string will be set for this query.* If a query references calls <code>config:write:file</code>, it will be queued until there is no running query left that has {{Code|filesCONFIG}} locked.* If the writing query will be evaluated, all other queries that will set a {{Code|filesCONFIG}} lock (reading or writing) will have to wait. Local locks can also be declared via pragmas: <pre class="brush:xquery">(# basex:write-lock CONFIGLOCK #) { file:write('config.xml', <config/>)}</pre>
In practiceLocks for the functions of a module can be assigned via option declarations: <pre class="brush:xquery">declare option basex:write-lock 'HTTPLOCK';file:write('config.xml', it’s often sufficient to only work with (exclusive<config/>) write locks.</pre>
===Java Modules===
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu