<!doctype html><htmllang=enclass="js csstransforms3d"><head><metacharset=utf-8><metaname=viewportcontent="width=device-width,initial-scale=1"><metaname=generatorcontent="Hugo 0.80.0"><metaname=descriptioncontent="Strolch is a parameterized framework for use on servers and IoT"><metaname=authorcontent="Robert von Burg"><linkrel=iconhref=/favicon.icotype=image/ico><title>Transactions - Strolch</title><linkhref=/css/nucleus.css?1678977092rel=stylesheet><linkhref=/css/fontawesome-all.min.css?1678977092rel=stylesheet><linkhref=/css/hybrid.css?1678977092rel=stylesheet><linkhref=/css/featherlight.min.css?1678977092rel=stylesheet><linkhref=/css/perfect-scrollbar.min.css?1678977092rel=stylesheet><linkhref=/css/auto-complete.css?1678977092rel=stylesheet><linkhref=/css/atom-one-dark-reasonable.css?1678977092rel=stylesheet><linkhref=/css/theme.css?1678977092rel=stylesheet><linkhref=/css/hugo-theme.css?1678977092rel=stylesheet><linkhref=/css/theme-green.css?1678977092rel=stylesheet><scriptsrc=/js/jquery-3.3.1.min.js?1678977092></script><style>:root#header+#content>#left>#rlblock_left{display:none!important}</style></head><bodydata-url=/documentation/transactions/><navid=sidebar><divid=header-wrapper><divid=header><aid=logohref=/><imgsrc=/logo.png></a></div><divclass=searchbox><labelfor=search-by><iclass="fas fa-search"></i></label><inputdata-search-inputid=search-bytype=searchplaceholder=Search...>
opened for a realm, and grants access to the model of the agent. Transactions
are implemented as a Java <code>try-with-resources</code> by implementing
the <code>AutoCloseable</code>
interface. This makes it trivial to understand the scope of a transaction.</p><p>Transactions handle the following:</p><ul><li>Opening and closing database connections</li><li>Releasing locks to strolch elements, if <code>tx.lock(StrolchRootElement)</code> or
<code>tx.lock(Locator)</code> was called</li><li>Performing Commands by executing them in the added order, and validating them
first.</li><li>Exception handling</li><li>Auditing</li><li>Updating observers</li></ul><p>When a transaction is opened, it is by default read-only, i.e. does not perform
any commands when it is closed. Should the TX perform commands, then it is
important to call <code>tx.commitOnClose()</code>, but only at the end of the work, so that
exception handling can properly work if something goes wrong.</p><p><code>StrolchTransaction</code> offers a myriad of methods:</p><ul><li>find element by its <code>Locator</code></li><li>get methods for elements by type and id, or using a <code>StringParameter</code> or
<code>StringListParameter</code> references</li><li>methods to add, update or remove elements</li><li>assert privilege access</li><li>get a new element by its template</li><li>check if an element exists by type and id</li><li>get streams for elements</li><li>add commands for execution</li></ul><p>Transactions are opened by accessing the realm, but there are convenience
methods depending on the use-case:</p><ul><li>In Services: by calling one of the <code>openTx()</code>-methods</li><li>In Commands: Transactions are already open, use method <code>tx()</code> to get instance.</li><li>REST API: <code>RestfulStrolchComponent.openTx()</code></li></ul><divclass="notices warning"><p>Note: don’t open a new TX inside a TX for the same realm!</p></div><p>Important is to always open the transaction as a <code>try-with-resource</code> block and to
define if the TX should commit, or not:</p><divclass=highlight><prestyle=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><codeclass=language-javadata-lang=java><spanstyle=color:#66d9ef>try</span><spanstyle=color:#f92672>(</span>StrolchTransaction tx <spanstyle=color:#f92672>=</span> openTx<spanstyle=color:#f92672>(...))</span><spanstyle=color:#f92672>{</span>
</span><spanstyle=color:#75715e></span> Resource ferrari <spanstyle=color:#f92672>=</span> tx<spanstyle=color:#f92672>.</span><spanstyle=color:#a6e22e>findElement</span><spanstyle=color:#f92672>(</span>ferrariLoc<spanstyle=color:#f92672>);</span>
<spanstyle=color:#75715e>// get a car by ID
</span><spanstyle=color:#75715e></span> Resource opel <spanstyle=color:#f92672>=</span> tx<spanstyle=color:#f92672>.</span><spanstyle=color:#a6e22e>getResourceBy</span><spanstyle=color:#f92672>(</span><spanstyle=color:#e6db74>"Car"</span><spanstyle=color:#f92672>,</span><spanstyle=color:#e6db74>"opel"</span><spanstyle=color:#f92672>,</span><spanstyle=color:#66d9ef>true</span><spanstyle=color:#f92672>);</span>
Resource audi <spanstyle=color:#f92672>=</span> tx<spanstyle=color:#f92672>.</span><spanstyle=color:#a6e22e>getResourceBy</span><spanstyle=color:#f92672>(</span><spanstyle=color:#e6db74>"Car"</span><spanstyle=color:#f92672>,</span><spanstyle=color:#e6db74>"audi"</span><spanstyle=color:#f92672>,</span><spanstyle=color:#66d9ef>true</span><spanstyle=color:#f92672>);</span>
<spanstyle=color:#75715e>// assert has privilege to remove a car