Hazelcast C++ Client
Hazelcast C++ Client Library
Loading...
Searching...
No Matches
transaction_context.h
1/*
2 * Copyright (c) 2008-2025, 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/iqueue.h"
19#include "hazelcast/client/imap.h"
20#include "hazelcast/client/ilist.h"
21#include "hazelcast/client/iset.h"
22#include "hazelcast/client/multi_map.h"
23#include "hazelcast/client/transaction_options.h"
24#include "hazelcast/client/txn/TransactionProxy.h"
25#include "hazelcast/client/transactional_map.h"
26#include "hazelcast/client/exception/protocol_exceptions.h"
27#include "hazelcast/client/transactional_queue.h"
28#include "hazelcast/client/transactional_multi_map.h"
29#include "hazelcast/client/transactional_list.h"
30#include "hazelcast/client/transactional_set.h"
31
32#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
33#pragma warning(push)
34#pragma warning(disable : 4251) // for dll export
35#endif
36
37namespace std {
38template<>
39class HAZELCAST_API hash<std::pair<std::string, std::string>>
40{
41public:
42 std::size_t operator()(
43 const std::pair<std::string, std::string>& val) const noexcept;
44};
45} // namespace std
46
47namespace hazelcast {
48namespace client {
49namespace spi {
50namespace impl {
51class ClientTransactionManagerServiceImpl;
52} // namespace impl
53} // namespace spi
54
55namespace connection {
56class ClientConnectionManagerImpl;
57
58class Connection;
59} // namespace connection
60
68class HAZELCAST_API transaction_context
69{
70public:
76 spi::impl::ClientTransactionManagerServiceImpl& transaction_manager,
77 const transaction_options&);
78
82 boost::uuids::uuid get_txn_id() const;
83
89 boost::future<void> begin_transaction();
90
97 boost::future<void> commit_transaction();
98
104 boost::future<void> rollback_transaction();
105
114 std::shared_ptr<transactional_map> get_map(const std::string& name)
115 {
116 return get_transactional_object<transactional_map>(imap::SERVICE_NAME,
117 name);
118 }
119
127 std::shared_ptr<transactional_queue> get_queue(const std::string& name)
128 {
130 iqueue::SERVICE_NAME, name);
131 }
132
140 std::shared_ptr<transactional_multi_map> get_multi_map(
141 const std::string& name)
142 {
144 multi_map::SERVICE_NAME, name);
145 }
146
154 std::shared_ptr<transactional_list> get_list(const std::string& name)
155 {
156 return get_transactional_object<transactional_list>(ilist::SERVICE_NAME,
157 name);
158 }
159
167 std::shared_ptr<transactional_set> get_set(const std::string& name)
168 {
169 return get_transactional_object<transactional_set>(iset::SERVICE_NAME,
170 name);
171 }
172
180 template<typename T>
181 std::shared_ptr<T> get_transactional_object(const std::string& service_name,
182 const std::string& name)
183 {
184 if (transaction_.get_state() != txn::TxnState::ACTIVE) {
185 std::string message = "No transaction is found while accessing ";
186 message += "transactional object -> [" + name + "]!";
187 BOOST_THROW_EXCEPTION(exception::illegal_state(
188 "TransactionContext::getMap(const std::string& name)", message));
189 }
190 auto key = std::make_pair(service_name, name);
191 std::shared_ptr<T> obj =
192 std::static_pointer_cast<T>(txn_object_map_.get(key));
193 if (!obj) {
194 obj = std::shared_ptr<T>(new T(name, transaction_));
195 txn_object_map_.put(key, obj);
196 }
197
198 return obj;
199 }
200
201private:
202 transaction_options options_;
203 std::shared_ptr<connection::Connection> txn_connection_;
204 txn::TransactionProxy transaction_;
205 util::SynchronizedMap<std::pair<std::string, std::string>,
206 proxy::TransactionalObject>
207 txn_object_map_;
208};
209} // namespace client
210} // namespace hazelcast
211
212#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
213#pragma warning(pop)
214#endif
boost::future< void > begin_transaction()
Begins a transaction.
std::shared_ptr< transactional_multi_map > get_multi_map(const std::string &name)
Returns the transactional multimap instance with the specified name.
boost::future< void > commit_transaction()
Commits a transaction.
std::shared_ptr< transactional_set > get_set(const std::string &name)
Returns the transactional set 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.
boost::future< void > rollback_transaction()
Begins a transaction.
std::shared_ptr< transactional_map > get_map(const std::string &name)
Returns the transactional distributed map 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.
boost::uuids::uuid get_txn_id() const
std::shared_ptr< transactional_queue > get_queue(const std::string &name)
Returns the transactional queue instance with the specified name.
transaction_context(spi::impl::ClientTransactionManagerServiceImpl &transaction_manager, const transaction_options &)
Constructor to be used internally.
Contains the configuration for a Hazelcast transaction.
STL namespace.