Hazelcast C++ Client
Hazelcast C++ Client Library
transaction_context.h
1 /*
2  * Copyright (c) 2008-2021, 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  public:
36  std::size_t operator()(const std::pair<std::string, std::string> &val) const noexcept;
37  };
38 }
39 
40 namespace hazelcast {
41  namespace client {
42  namespace spi {
43  namespace impl {
44  class ClientTransactionManagerServiceImpl;
45  }
46  }
47 
48  namespace connection {
49  class ClientConnectionManagerImpl;
50 
51  class Connection;
52  }
53 
60  class HAZELCAST_API transaction_context {
61  public:
66  transaction_context(spi::impl::ClientTransactionManagerServiceImpl &transaction_manager,
67  const transaction_options &);
68 
72  boost::uuids::uuid get_txn_id() const;
73 
79  boost::future<void> begin_transaction();
80 
86  boost::future<void> commit_transaction();
87 
93  boost::future<void> rollback_transaction();
94 
102  std::shared_ptr<transactional_map> get_map(const std::string &name) {
103  return get_transactional_object<transactional_map>(imap::SERVICE_NAME, name);
104  }
105 
113  std::shared_ptr<transactional_queue> get_queue(const std::string &name) {
114  return get_transactional_object<transactional_queue>(iqueue::SERVICE_NAME, name);
115  }
116 
124  std::shared_ptr<transactional_multi_map> get_multi_map(const std::string &name) {
125  return get_transactional_object<transactional_multi_map>(multi_map::SERVICE_NAME, name);
126  }
127 
135  std::shared_ptr<transactional_list> get_list(const std::string &name) {
136  return get_transactional_object<transactional_list>(ilist::SERVICE_NAME, name);
137  }
138 
146  std::shared_ptr<transactional_set> get_set(const std::string &name) {
147  return get_transactional_object<transactional_set>(iset::SERVICE_NAME, name);
148  }
149 
157  template<typename T>
158  std::shared_ptr<T> get_transactional_object(const std::string &service_name, const std::string &name) {
159  if (transaction_.get_state() != txn::TxnState::ACTIVE) {
160  std::string message = "No transaction is found while accessing ";
161  message += "transactional object -> [" + name + "]!";
162  BOOST_THROW_EXCEPTION(
163  exception::illegal_state("TransactionContext::getMap(const std::string& name)",
164  message));
165  }
166  auto key = std::make_pair(service_name, name);
167  std::shared_ptr<T> obj = std::static_pointer_cast<T>(txn_object_map_.get(key));
168  if (!obj) {
169  obj = std::shared_ptr<T>(new T(name, transaction_));
170  txn_object_map_.put(key, obj);
171  }
172 
173  return obj;
174  }
175 
176  private :
177  transaction_options options_;
178  std::shared_ptr<connection::Connection> txn_connection_;
179  txn::TransactionProxy transaction_;
180  util::SynchronizedMap<std::pair<std::string, std::string>, proxy::TransactionalObject> txn_object_map_;
181  };
182  }
183 }
184 
185 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
186 #pragma warning(pop)
187 #endif
188 
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.