Hazelcast C++ Client
Hazelcast C++ Client Library
transaction_context.h
1 /*
2  * Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "hazelcast/client/transaction_options.h"
19 #include "hazelcast/client/txn/TransactionProxy.h"
20 #include "hazelcast/client/transactional_map.h"
21 #include "hazelcast/client/exception/protocol_exceptions.h"
22 #include "hazelcast/client/transactional_queue.h"
23 #include "hazelcast/client/transactional_multi_map.h"
24 #include "hazelcast/client/transactional_list.h"
25 #include "hazelcast/client/transactional_set.h"
26 
27 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
28 #pragma warning(push)
29 #pragma warning(disable : 4251) // for dll export
30 #endif
31 
32 namespace std {
33 template<>
34 class HAZELCAST_API hash<std::pair<std::string, std::string>>
35 {
36 public:
37  std::size_t operator()(
38  const std::pair<std::string, std::string>& val) const noexcept;
39 };
40 } // namespace std
41 
42 namespace hazelcast {
43 namespace client {
44 namespace spi {
45 namespace impl {
46 class ClientTransactionManagerServiceImpl;
47 }
48 } // namespace spi
49 
50 namespace connection {
51 class ClientConnectionManagerImpl;
52 
53 class Connection;
54 } // namespace connection
55 
63 class HAZELCAST_API transaction_context
64 {
65 public:
71  spi::impl::ClientTransactionManagerServiceImpl& transaction_manager,
72  const transaction_options&);
73 
77  boost::uuids::uuid get_txn_id() const;
78 
84  boost::future<void> begin_transaction();
85 
92  boost::future<void> commit_transaction();
93 
99  boost::future<void> rollback_transaction();
100 
109  std::shared_ptr<transactional_map> get_map(const std::string& name)
110  {
111  return get_transactional_object<transactional_map>(imap::SERVICE_NAME,
112  name);
113  }
114 
122  std::shared_ptr<transactional_queue> get_queue(const std::string& name)
123  {
124  return get_transactional_object<transactional_queue>(
125  iqueue::SERVICE_NAME, name);
126  }
127 
135  std::shared_ptr<transactional_multi_map> get_multi_map(
136  const std::string& name)
137  {
138  return get_transactional_object<transactional_multi_map>(
139  multi_map::SERVICE_NAME, name);
140  }
141 
149  std::shared_ptr<transactional_list> get_list(const std::string& name)
150  {
151  return get_transactional_object<transactional_list>(ilist::SERVICE_NAME,
152  name);
153  }
154 
162  std::shared_ptr<transactional_set> get_set(const std::string& name)
163  {
164  return get_transactional_object<transactional_set>(iset::SERVICE_NAME,
165  name);
166  }
167 
175  template<typename T>
176  std::shared_ptr<T> get_transactional_object(const std::string& service_name,
177  const std::string& name)
178  {
179  if (transaction_.get_state() != txn::TxnState::ACTIVE) {
180  std::string message = "No transaction is found while accessing ";
181  message += "transactional object -> [" + name + "]!";
182  BOOST_THROW_EXCEPTION(exception::illegal_state(
183  "TransactionContext::getMap(const std::string& name)", message));
184  }
185  auto key = std::make_pair(service_name, name);
186  std::shared_ptr<T> obj =
187  std::static_pointer_cast<T>(txn_object_map_.get(key));
188  if (!obj) {
189  obj = std::shared_ptr<T>(new T(name, transaction_));
190  txn_object_map_.put(key, obj);
191  }
192 
193  return obj;
194  }
195 
196 private:
197  transaction_options options_;
198  std::shared_ptr<connection::Connection> txn_connection_;
199  txn::TransactionProxy transaction_;
200  util::SynchronizedMap<std::pair<std::string, std::string>,
201  proxy::TransactionalObject>
202  txn_object_map_;
203 };
204 } // namespace client
205 } // namespace hazelcast
206 
207 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
208 #pragma warning(pop)
209 #endif
Provides a context to do transactional operations; so beginning/committing transactions,...
std::shared_ptr< transactional_set > get_set(const std::string &name)
Returns the transactional set instance with the specified name.
std::shared_ptr< transactional_multi_map > get_multi_map(const std::string &name)
Returns the transactional multimap instance with the specified name.
std::shared_ptr< transactional_map > get_map(const std::string &name)
Returns the transactional distributed map instance with the specified name.
std::shared_ptr< T > get_transactional_object(const std::string &service_name, const std::string &name)
get any transactional object with template T.
std::shared_ptr< transactional_queue > get_queue(const std::string &name)
Returns the transactional queue instance with the specified name.
std::shared_ptr< transactional_list > get_list(const std::string &name)
Returns the transactional list instance with the specified name.
Contains the configuration for a Hazelcast transaction.