===XQuery===
With {{Announce|Version Since BaseX 10}}, the [[BaseX 10#Compilation|lock detection was has been fundamentally improved]] , by splitting compilation into multiple steps.
Local locks can be applied if it is possible at after compile time to associate names of databases with all database operationswith static databases names:
{| class="wikitable"
|-valign="top"
! Query
! Description
|-valign="top"| {{Code|<code>//item}}</code>
| Read lock of the currently opened database
|-valign="top"| {{Code|<code>doc('factbook')}}</code>
| Read lock of the {{Code|factbook}} database
|-valign="top"| {{Code|<code>collection('documents/path/to/docs')}}</code>
| Read lock of the {{Code|documents}} database
|-valign="top"| {{Code|<code>delete nodes db:get('test')//*[string-length(local-name(.)) @type = 'misc']</code> 5]}}
| Write lock of the {{Code|test}} database
|-valign="top"| {{Code|fn:sum(1 to 100)}}| ''no lock''|-| {{Code|for $db in ('db1', 'db2') return db:get($db)}}| Read lock of {{Code|db1}} and {{Code|db2}}, as query is [[XQuery Optimization#Loop Unrolling|unrolled at compile time]].|-| {{Code|<code>declare variables $db external; <br/>db:get($db)}}</code>
| Read lock of the database externally bound to {{Code|$db}}.
|- valign="top"
| <code>for $db in ('db1', 'db2')<br/>return db:get($db)</code>
| Read lock of {{Code|db1}} and {{Code|db2}}, as the query is [[XQuery Optimizations#Loop Unrolling|unrolled at compile time]].
|- valign="top"
| <code>let $db := 'test'<br/>return insert nodes <test/> into db:get($db)</code>
| Read lock of {{Code|test}}, as the [[XQuery Optimizations#Variable_Inlining|variable is inlined]] at compile time.
|- valign="top"
| <code>sum(1 to 100)</code>
| No lock required
|- valign="top"
| <code>declare variable $SIMULATE := true();<br/>if($SIMULATE) then <doc/> else db:get('doc')</code>
| No lock required, as the query is simplified to {{Code|<doc/>}} at compile time.
|}
A global lock will be assigned if the name of the database is not a static stringdetection fails:
* {{Code|docclass="wikitable"|- valign="top"! Query! Description|- valign="top"| <code>db:get(doc('test')/reference/text())}}</code>| The name of the database to be opened will only be known at evaluation time.|- valign="top"* | <code>let $(1 to 100) ! db := get(concat('testdb' return insert nodes , .))<test/code> into | The {{Option|UNROLLLIMIT}} can be increased to generate 100 {{Code|db:get}} function calls and corresponding locks.|} The functions {{Code|fn:doc}} and {{Code|fn:collection}} can be used for both accessing databases resources and fetching resources at the specified URI ($dbsee [[Databases#Access Resources|Access Resources]] for more details)</code>. There are two ways to reduce the number of locks:
The functions {{Code|fn:doc}} and {{Code|fn:collection}} can also be used to address that are not stored in a database. However, this may lead to unwanted locks, and you have two options to reduce # Turn off the number of locks: No database lookups will take place if {{Option|WITHDB}} option is disabled, to prevent the functions from accessing databases; or if # use {{Function|Fetch|fetch:doc}} is used instead of for fetching resources from URIs, and use {{CodeFunction|fnDatabase|db:docget}}for accessing databases.
You can consult the query info output (which you find in via the [[GUI#Visualizations|Info View]] of the GUI or which you can turn on by setting , via {{OptionCode|QUERYINFO-V}} to on [[Command-Line]] or via turning on the {{CodeOption|trueQUERYINFO}}option) to find out which databases have been are locked by a query, and if local locks or a global lock is applied.
=XQuery Locks=
By default, access to external resources (files on hard disk, HTTP requests, ...…) is not controlled by the transaction monitor of BaseX. Custom locks can be assigned via annotations, pragmas or options:
* A lock string may consist of a single key or multiple keys separated with commas.
In the following module, lock annotations are used to prevent concurrent write operations on the same file:
<syntaxhighlight pre lang="'xquery"'>
module namespace config = 'config';
file:write-text('config.txt', $data)
};
</syntaxhighlightpre>
Some explanations:
* If a query calls the a reading <code>configbasex:readlock</code> function, a read lock will be acquired for the user-defined {{Code|CONFIG}} lock string before query evaluation.* If an updating <code>configbasex:writelock</code> is called by a query, a write lock will be applied.* If another query calls a <code>configbasex:writelock</code>function, it will be queued until the first query is evaluated.
==Pragmas==
Locks can also be declared via pragmas:
<syntaxhighlight pre lang="'xquery"'>
update:output((# basex:lock CONFIG #) {
file:write('config.xml', <config/>)
})
</syntaxhighlightpre>
The write locks is enforced via the {{Code|Update|update:output}}.
Locks for the functions of a module can also be assigned via option declarations:
<syntaxhighlight pre lang="'xquery"'>
declare option basex:lock 'CONFIG';
update:output(file:write('config.xml', <config/>))
</syntaxhighlightpre>
Once again, a write lock is enforced.