blob: 2bcc3acff314592bf8d8b083daf4e7faa88fc80d [file] [log] [blame]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine or request Chrome Frame -->
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400italic,400,600' rel='stylesheet' type='text/css'>
<!-- Use title if it's in the page YAML frontmatter -->
<title>
Transactions |
Geode Native .NET Docs
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/stylesheets/all.css" rel="stylesheet" media="screen, print" />
<link href="/stylesheets/print.css" rel="stylesheet" media="print" />
<link href='/images/favicon.ico' rel='shortcut icon'>
<script src="/javascripts/all.js"></script>
</head>
<body class="docs docs_geode-native docs_geode-native_dotnet docs_geode-native_dotnet_113 docs_geode-native_dotnet_113_transactions has-subnav">
<div class="viewport">
<div class='wrap'>
<script type="text/javascript">
document.domain = "apache.org";
</script>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<header class="header header-layout">
<h1 class="logo">
<a href="/">
<img src="/images/Apache_Geode_logo_symbol_white.png" style="width:30px;">
Apache Geode Native .NET Documentation
</a>
</h1>
<div class="header-links js-bar-links">
<div class="btn-menu" data-behavior="MenuMobile"></div>
<div class="header-item"><a href="http://geode.apache.org">Back to Product Page</a></div>
<div class="header-item">
<a href="http://geode.apache.org/community" target="_blank">Community</a>
</div>
</div>
</header>
<div class="container">
<!--googleoff: index-->
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div id="sub-nav" class="js-sidenav nav-container" role="navigation">
<a class="sidenav-title" data-behavior="SubMenuMobile">Doc Index</a>
<div class="nav-content">
<ul>
<li>
<a href="/docs/geode-native/dotnet/113/about-client-users-guide.html">Apache Geode Native Documentation</a>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/dotnet/113/getting-started/getting-started-nc-client.html">Getting Started with the Native Library</a>
<ul>
<li>
<a href="/docs/geode-native/dotnet/113/getting-started/getting-started-nc-client.html#set_up_dev_environment">Set Up Your Development Environment</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/getting-started/getting-started-nc-client.html#establish_cluster_access">Establish Access to a Geode Cluster</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/getting-started/getting-started-nc-client.html#app_dev_walkthroughs">Application Development Walkthroughs</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/getting-started/getting-started-nc-client.html#programming_examples">Programming Examples</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/getting-started/put-get-example.html">Put/Get/Remove Examples</a>
</li>
</ul>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/dotnet/113/configuring/configuration.html">Configuring a Client Application</a>
<ul>
<li>
<a href="/docs/geode-native/dotnet/113/configuring/system-level-configuration.html">System Level Configuration</a>
</li>
</ul>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/configuring/config-client-cache.html">Configuring the Client Cache</a>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/dotnet/113/regions/regions.html">Configuring Regions</a>
<ul>
<li>
<a href="/docs/geode-native/dotnet/113/regions/registering-interest-for-entries.html">Registering Interest for Entries</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/regions/region-attributes.html">Region Attributes</a>
</li>
</ul>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/dotnet/113/serialization/data-serialization.html">Serializing Data</a>
<ul>
<li class="has_submenu">
<a href="/docs/geode-native/dotnet/113/serialization/dotnet-serialization/dotnet-pdx-serialization.html">Geode PDX Serialization</a>
<ul>
<li>
<a href="/docs/geode-native/dotnet/113/serialization/dotnet-serialization/dotnet-pdx-autoserializer.html">Using the Geode PDX Autoserializer</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/serialization/dotnet-serialization/serialize-using-ipdxserializable.html">Using the IPdxSerializable Interface</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/serialization/dotnet-serialization/pdx-serializable-examples.html">IPdxSerializable Examples</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/remote-queries.html">Remote Queries</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/continuous-queries.html">Continuous Queries</a>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/dotnet/113/security/security.html">Security: Authentication and Encryption</a>
<ul>
<li>
<a href="/docs/geode-native/dotnet/113/security/authentication.html">Authentication</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/security/sslclientserver.html">TLS/SSL Client-Server Communication Encryption</a>
</li>
</ul>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/function-execution.html">Function Execution</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/transactions.html">Transactions</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/configuring/sysprops.html">System Properties</a>
</li>
<li>
<a href="/docs/geode-native/dotnet/113/client-cache-ref.html">Client Cache XML Reference</a>
</li>
</ul>
</div>
</div>
<!--googleon: index-->
<main class="content content-layout" id="js-content" role="main">
<a id="top"></a>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<span style="font-weight:200;font-size:31px;" style="float:left;">
<img src="/images/Apache_Geode_logo_symbol.png" style="height:26px;">
Apache Geode Native .NET
</span>
<span class="local-header version-info" style="float:right;">
<a href="https://cwiki.apache.org/confluence/display/GEODE/Release+Notes">CHANGELOG</a>
</span>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<h1 class="title-container" >
Transactions
</h1>
<div id="js-quick-links" >
<div class="quick-links"><ul>
<li><a href="#client-transaction-apis">Native Client Transaction APIs</a></li>
<li><a href="#running-nc-transactions">Running Native Client Transactions</a></li>
<li>
<a href="#nc-transaction-examples">Client Transaction Examples</a><ul><li><a href="#dotnet-example">.NET Example</a></li></ul>
</li>
</ul></div>
</div>
<div class="to-top" id="js-to-top">
<a href="#top" title="back to top"></a>
</div>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<p>The Native Client API runs transactions on the server as if they were local to the client application.
Thus, the key to running client transactions lies in making sure the server is properly configured and programmed.
For complete information about how transactions are conducted on the Geode server, see the
<a href="geodeman/developing/transactions/chapter_overview.html">Transactions section of the Geode User Guide</a>.</p>
<h2 id="native-client-transaction-apis"><a id="client-transaction-apis"></a>Native Client Transaction APIs</h2>
<p>The API for distributed transactions has the familiar relational database methods, <code>begin</code>,
<code>commit</code>, and <code>rollback</code>. There are also APIs available to suspend and resume transactions.</p>
<p>The .NET classes for executing transactions are:</p>
<ul>
<li> <code>Apache.Geode.Client.CacheTransactionManager</code></li>
<li> <code>Apache.Geode.Client.TransactionId</code></li>
</ul>
<h2 id="running-native-client-transactions"><a id="running-nc-transactions"></a>Running Native Client Transactions</h2>
<p>The syntax for writing client transactions is the same as with server or peer transactions, but when
a client performs a transaction, the transaction is delegated to a server that brokers the
transaction.</p>
<p>Start each transaction with a <code>begin</code> operation, and end the transaction with a <code>commit</code> or a <code>rollback</code>.</p>
<p>To maintain cache consistency, the local client cache is not used during a transaction.
When the transaction completes or is suspended, local cache usage is reinstated.</p>
<p>If the transaction runs on server regions that are a mix of partitioned and replicated regions,
perform the first transaction operation on a partitioned region. This sets the server data host for
the entire transaction. If you are using PR single-hop, single-hop will be applied as usual to this
first operation.</p>
<p>In addition to the failure conditions common to all transactions, client transactions can also fail
if the transaction delegate fails. If the delegate performing the transaction fails, the transaction
code throws a <code>TransactionException</code>.</p>
<h2 id="client-transaction-examples"><a id="nc-transaction-examples"></a>Client Transaction Examples</h2>
<p>The native client release contains a transaction example in <code>../examples/dotnet/transaction</code>.</p>
<p>The example performs a sequence of operations, displaying simple log entries as they run.</p>
<ul>
<li>To run an example, follow the instructions in the <code>README.md</code> file in the example directory.</li>
<li><p>Review the source code in the example directory to see exactly how it operates.</p></li>
<li><p>You begin by running a script that sets up the server-side environment by invoking <code>gfsh</code> commands to create a region, simply called &ldquo;exampleRegion.&rdquo;</p></li>
<li><p>You run the example client application, which performs the following steps:</p>
<ul>
<li>Connects to the server</li>
<li>Begins a transaction</li>
<li>Performs some <code>put</code> operations</li>
<li>Commits the transaction</li>
</ul></li>
<li><p>For this example, the transaction code has these characteristics:</p>
<ul>
<li>To introduce the possibility of failure, values are randomized from 0 to 9, and the 0 values are
treated as unsuccessful. The transaction is retried until it succeeds.</li>
<li>In case the transaction repeatedly fails, the retry loop uses a counter to set a limit of 5 retries.</li>
</ul></li>
</ul>
<h3 id=".net-example"><a id="dotnet-example"></a>.NET Example</h3>
<p>This section contains code snippets showing highlights of the .NET transaction example. They are not intended for cut-and-paste execution.
For the complete source, see the example source directory.</p>
<p>The .NET example creates a cache, then uses it to create a connection pool.</p>
<pre class="highlight csharp"><code> <span class="kt">var</span> <span class="n">cache</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">CacheFactory</span><span class="p">()</span>
<span class="p">.</span><span class="nf">Set</span><span class="p">(</span><span class="s">"log-level"</span><span class="p">,</span> <span class="s">"none"</span><span class="p">).</span><span class="nf">Create</span><span class="p">();</span>
<span class="kt">var</span> <span class="n">poolFactory</span> <span class="p">=</span> <span class="n">cache</span><span class="p">.</span><span class="nf">GetPoolFactory</span><span class="p">()</span>
<span class="p">.</span><span class="nf">AddLocator</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="m">10334</span><span class="p">);</span>
<span class="n">poolFactory</span><span class="p">.</span><span class="nf">Create</span><span class="p">(</span><span class="s">"pool"</span><span class="p">);</span>
<span class="kt">var</span> <span class="n">regionFactory</span> <span class="p">=</span> <span class="n">cache</span><span class="p">.</span><span class="nf">CreateRegionFactory</span><span class="p">(</span><span class="n">RegionShortcut</span><span class="p">.</span><span class="n">PROXY</span><span class="p">)</span>
<span class="p">.</span><span class="nf">SetPoolName</span><span class="p">(</span><span class="s">"pool"</span><span class="p">);</span>
<span class="kt">var</span> <span class="n">region</span> <span class="p">=</span> <span class="n">regionFactory</span><span class="p">.</span><span class="n">Create</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">,</span> <span class="kt">int</span><span class="p">&gt;(</span><span class="s">"exampleRegion"</span><span class="p">);</span>
</code></pre>
<p>The example application gets a transaction manager from the cache and begins a transaction.</p>
<pre class="highlight csharp"><code> <span class="n">cache</span><span class="p">.</span><span class="n">CacheTransactionManager</span><span class="p">.</span><span class="nf">Begin</span><span class="p">();</span>
</code></pre>
<p>Within the transaction, the client populates data store with 10 values associated with Key1 - Key10.</p>
<pre class="highlight csharp"><code> <span class="k">foreach</span><span class="p">(</span><span class="kt">var</span> <span class="n">key</span> <span class="k">in</span> <span class="n">keys</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">var</span> <span class="k">value</span> <span class="p">=</span> <span class="nf">getValueFromExternalSystem</span><span class="p">();</span>
<span class="n">region</span><span class="p">.</span><span class="nf">Put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="k">value</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
<p>If all <code>put</code> operations succeed, the application commits the transaction. Otherwise, it retries up to 5 times if necessary.</p>
<pre class="highlight csharp"><code> <span class="kt">var</span> <span class="n">retries</span> <span class="p">=</span> <span class="m">5</span><span class="p">;</span>
<span class="k">while</span><span class="p">(</span><span class="n">retries</span><span class="p">--</span> <span class="p">&gt;</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="p">...</span> <span class="c1">// PUT OPERATIONS ...
</span> <span class="n">cache</span><span class="p">.</span><span class="n">CacheTransactionManager</span><span class="p">.</span><span class="nf">Commit</span><span class="p">();</span>
<span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">"Committed transaction - exiting"</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="p">}</span> <span class="k">catch</span>
<span class="p">{</span>
<span class="n">cache</span><span class="p">.</span><span class="n">CacheTransactionManager</span><span class="p">.</span><span class="nf">Rollback</span><span class="p">();</span>
<span class="n">Console</span><span class="p">.</span><span class="nf">WriteLine</span><span class="p">(</span><span class="s">"Rolled back transaction - retrying({0})"</span><span class="p">,</span> <span class="n">retries</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</main>
</div>
</div>
</div>
<div id="scrim"></div>
<div class="container">
<footer class="site-footer-links">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div class="copyright">
<a href='/'>Apache Geode Documentation</a>
&copy; 2020 <a href='http://www.apache.org/'>The Apache Software Foundation</a>.
</div>
<div class="support">
Need help? <a href="http://geode.apache.org/community" target="_blank">Visit the Community</a>
</div>
</footer>
</div><!--end of container-->
</body>
</html>