Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ add_example(NAME continuousquery
add_example(NAME dataserializable
SOURCE main.cpp Order.cpp Order.hpp)

add_example(NAME function-execution
SOURCE main.cpp)

add_example(NAME pdxserializable
SOURCE main.cpp Order.cpp Order.hpp)

Expand All @@ -45,12 +48,12 @@ add_example(NAME pdxserializer
add_example(NAME put-get-remove
SOURCE main.cpp)

add_example(NAME function-execution
SOURCE main.cpp)

add_example(NAME remotequery
SOURCE main.cpp Order.cpp Order.hpp)

add_example(NAME transaction
SOURCE main.cpp)

install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DESTINATION examples/
PATTERN "./*.in" EXCLUDE
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ project(@PRODUCT_DLL_NAME@.Cpp.Examples LANGUAGES NONE)

add_subdirectory(continuousquery)
add_subdirectory(dataserializable)
add_subdirectory(function-execution)
add_subdirectory(pdxserializable)
add_subdirectory(pdxserializer)
add_subdirectory(put-get-remove)
add_subdirectory(function-execution)
add_subdirectory(remotequery)
add_subdirectory(transaction)
61 changes: 61 additions & 0 deletions examples/cpp/transaction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Transaction example
This is a very simple example showing how to use TransactionManager. This example shows
how to begin a transaction, commit a transaction, and rollback a transaction while showing
exception handling. We commit two keys and rollback adding a third key and destroying an
existing key while showing how to handle exceptions.

## Prerequisites
* An installation of Apache Geode.
* Apache Geode Native, built and installed.
* Apache Geode Native examples, built and installed.
* A `GEODE_HOME` environment variable set to the location of the Apache Geode installation.
* `GEODE_HOME/bin` in the execution path.

## Running
1. Set the current directory to the `transaction` directory in your example workspace.

```
$ cd workspace/examples/cpp/transaction
```

1. Run the `startserver.sh` script to start the Geode server, create a region, and populate the region with sample data.

```
$ sh ./startserver.sh
/Users/user/geode/bin/gfsh

(1) Executing - start locator --name=locator
...
(2) Executing - start server --name=server
...
(3) Executing - create region --name=exampleRegion --type=PARTITION

Member | Status
------ | ----------------------------------------------
server | Region "/exampleRegion" created on "server"
```

1. Execute `transaction`:

```
$ build/transaction
Created cache
Created region 'exampleRegion'
Rolled back transaction - retrying(4)
Rolled back transaction - retrying(3)
Rolled back transaction - retrying(2)
Committed transaction - exiting
```

1. Stop the server

```
$ sh ./stopserver.sh
/Users/user/geode/bin/gfsh
(1) Executing - connect
...
(2) Executing - stop server --name=server
...
(3) Executing - stop locator --name=locator
....
```
89 changes: 89 additions & 0 deletions examples/cpp/transaction/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.
*/

#include <iostream>
#include <random>

#include <geode/CacheFactory.hpp>
#include <geode/CacheTransactionManager.hpp>
#include <geode/PoolManager.hpp>
#include <geode/RegionFactory.hpp>
#include <geode/RegionShortcut.hpp>

using apache::geode::client::Cache;
using apache::geode::client::CacheFactory;
using apache::geode::client::CacheTransactionManager;
using apache::geode::client::RegionShortcut;

auto keys = {
"Key1",
"Key2",
"Key3",
"Key4",
"Key5",
"Key6",
"Key7",
"Key8",
"Key9",
"Key10"
};


int getValueFromExternalSystem() {
static thread_local std::default_random_engine generator(std::random_device{}());
auto value = std::uniform_int_distribution<int32_t>{0, 9}(generator);

if (!value) {
throw "failed to get from external system";
}

return value;
}

int main(int argc, char** argv) {
auto cache = CacheFactory().set("log-level", "none").create();
auto poolFactory = cache.getPoolManager().createFactory();

std::cout << "Created cache" << std::endl;

poolFactory.addLocator("localhost", 10334);
auto pool = poolFactory.create("pool");
auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY);
auto region = regionFactory.setPoolName("pool").create("exampleRegion");

std::cout << "Created region 'exampleRegion'" << std::endl;

auto transactionManager = cache.getCacheTransactionManager();

auto retries = 5;
while (retries--) {
try {
transactionManager->begin();
for (auto& key : keys) {
auto value = getValueFromExternalSystem();
region->put(key, value);
}
transactionManager->commit();
std::cout << "Committed transaction - exiting" << std::endl;
break;
} catch ( ... ) {
transactionManager->rollback();
std::cout << "Rolled back transaction - retrying(" << retries << ")" << std::endl;
}
}
}

31 changes: 31 additions & 0 deletions examples/cpp/transaction/startserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

#!/usr/bin/env bash
GFSH_PATH=""
which gfsh 2> /dev/null

if [ $? -eq 0 ]; then
GFSH_PATH="gfsh"
else
if [ "$GEODE_HOME" == "" ]; then
echo "Could not find gfsh. Please set the GEODE_HOME path."
echo "e.g. export GEODE_HOME=<path to Geode>"
else
GFSH_PATH=$GEODE_HOME/bin/gfsh
fi
fi

$GFSH_PATH -e "start locator --name=locator" -e "start server --name=server" -e "create region --name=exampleRegion --type=PARTITION"
31 changes: 31 additions & 0 deletions examples/cpp/transaction/stopserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

#!/usr/bin/env bash
GFSH_PATH=""
which gfsh 2> /dev/null

if [ $? -eq 0 ]; then
GFSH_PATH="gfsh"
else
if [ "$GEODE_HOME" == "" ]; then
echo "Could not find gfsh. Please set the GEODE_HOME path."
echo "e.g. export GEODE_HOME=<path to Geode>"
else
GFSH_PATH=$GEODE_HOME/bin/gfsh
fi
fi

$GFSH_PATH -e "connect" -e "stop server --name=server" -e "stop locator --name=locator"