<!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="Strolch"><linkrel=iconhref=/favicon.icotype=image/ico><title>Transactions - Strolch</title><linkhref=/css/nucleus.css?1626091328rel=stylesheet><linkhref=/css/fontawesome-all.min.css?1626091328rel=stylesheet><linkhref=/css/hybrid.css?1626091328rel=stylesheet><linkhref=/css/featherlight.min.css?1626091328rel=stylesheet><linkhref=/css/perfect-scrollbar.min.css?1626091328rel=stylesheet><linkhref=/css/auto-complete.css?1626091328rel=stylesheet><linkhref=/css/atom-one-dark-reasonable.css?1626091328rel=stylesheet><linkhref=/css/theme.css?1626091328rel=stylesheet><linkhref=/css/hugo-theme.css?1626091328rel=stylesheet><linkhref=/css/theme-green.css?1626091328rel=stylesheet><scriptsrc=/js/jquery-3.3.1.min.js?1626091328></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...>
parent"><ahref=/documentation/>Documentation</a><ul><lidata-nav-id=/documentation/architecture/title=Architectureclass=dd-item><ahref=/documentation/architecture/>Architecture</a></li><lidata-nav-id=/documentation/model/title=Modelclass=dd-item><ahref=/documentation/model/>Model</a></li><lidata-nav-id=/documentation/do-and-donts/title="Do and Don't"class=dd-item><ahref=/documentation/do-and-donts/>Do and Don't</a></li><lidata-nav-id=/documentation/runtime-configuration/title="Runtime Configuration"class=dd-item><ahref=/documentation/runtime-configuration/>Runtime Configuration</a></li><lidata-nav-id=/documentation/realms/title=Realmsclass=dd-item><ahref=/documentation/realms/>Realms</a></li><lidata-nav-id=/documentation/components/title=Componentsclass=dd-item><ahref=/documentation/components/>Components</a></li><lidata-nav-id=/documentation/services-and-commands/title="Services and Commands"class=dd-item><ahref=/documentation/services-and-commands/>Services and Commands</a></li><lidata-nav-id=/documentation/searches/title=Searchesclass=dd-item><ahref=/documentation/searches/>Searches</a></li><lidata-nav-id=/documentation/queries/title=Queriesclass=dd-item><ahref=/documentation/queries/>Queries</a></li><lidata-nav-id=/documentation/transactions/title=Transactionsclass="dd-item active"><ahref=/documentation/transactions/>Transactions</a></li><lidata-nav-id=/documentation/policies/title=Policiesclass=dd-item><ahref=/documentation/policies/>Policies</a></li><lidata-nav-id=/documentation/observers/title=Observersclass=dd-item><ahref=/documentation/observers/>Observers</a></li><lidata-nav-id=/documentation/versioning/title=Versioningclass=dd-item><ahref=/documentation/versioning/>Versioning</a></li><lidata-nav-id=/documentation/reports/title=Reportsclass=dd-item><ahref=/documentation/reports/>Reports</a></li><lidata-nav-id=/documentation/priviles/title=Privilegesclass=dd-item><ahref=/documentation/priviles/>Privileges</a></li></ul></li><lidata-nav-id=/plc/title=PLCclass=dd-item><ahref=/plc/>PLC</a><ul><lidata-nav-id=/plc/architecture/title=Architectureclass=dd-item><ahref=/plc/architecture/>Architecture</a></li><lidata-nav-id=/plc/example-set-up/title="Example Set-Up"class=dd-item><ahref=/plc/example-set-up/>Example Set-Up</a></li></ul></li><lidata-nav-id=/tutorial/title=Tutorialclass=dd-item><ahref=/tutorial/>Tutorial</a><ul><lidata-nav-id=/tutorial/configuration/title=Configurationclass=dd-item><ahref=/tutorial/configuration/>Configuration</a></li><lidata-nav-id=/tutorial/model/title=Modelclass=dd-item><ahref=/tutorial/model/>Model</a></li><lidata-nav-id=/tutorial/crud-book/title="CRUD Book"class=dd-item><ahref=/tutorial/crud-book/>CRUD Book</a></li></ul></li><lidata-nav-id=/download/title=Downloadclass=dd-item><ahref=/download/>Download</a></li><lidata-nav-id=/development/title=Developmentclass=dd-item><ahref=/development/>Development</a></li></ul><sectionid=shortcuts><h3>More</h3><ul><li><aclass=paddinghref=https://strolch.li/tags><iclass="fas fa-tags"></i>Tags</a></li><li><aclass=paddinghref=https://github.com/strolch-li><iclass="fab fa-github"></i>GitHub project</a></li></ul></section><sectionid=footer><p>Built with <ahref=https://github.com/matcornic/hugo-theme-learn><iclass="fas fa-heart"></i></a>from <ahref=https://getgrav.org>Grav</a> and <ahref=https://gohugo.io/>Hugo</a></p></section></div></nav><sectionid=body><divid=overlay></div><divclass="padding highlightable"><div><divid=top-bar><divid=top-github-link><aclass=github-linktitle="Edit this page"href=https://github.com/Pi4J/pi4j.github.io/tree/main/contentdocumentation/transactions.mdtarget=blank><iclass="fas fa-code-branch"></i><spanid=top-github-link-text>Edit this page</span></a></div><divid=breadcrumbsitemscopeitemtype=http://data-vocabulary.org/Breadcrumb><spanid=sidebar-toggle-span><ahref=#id=sidebar-toggledata-sidebar-toggle><iclass="fas fa-bars"></i></a></span><spanid=toc-menu><iclass="fas fa-list-alt"></i></span><spanclass=
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