Hazelcast C++ Client
 All Classes Functions Variables Enumerations Pages
TransactionContext.h
1 /*
2  * Copyright (c) 2008-2015, 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 //
17 // Created by sancar koyunlu on 8/5/13.
18 
19 
20 
21 
22 
23 #ifndef HAZELCAST_TransactionContext
24 #define HAZELCAST_TransactionContext
25 
26 #include "hazelcast/client/TransactionOptions.h"
27 #include "hazelcast/client/txn/TransactionProxy.h"
28 #include "hazelcast/client/TransactionalMap.h"
29 #include "hazelcast/client/exception/IllegalStateException.h"
30 #include "hazelcast/client/TransactionalQueue.h"
31 #include "hazelcast/client/TransactionalMultiMap.h"
32 #include "hazelcast/client/TransactionalList.h"
33 #include "hazelcast/client/TransactionalSet.h"
34 
35 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
36 #pragma warning(push)
37 #pragma warning(disable: 4251) //for dll export
38 #endif
39 
40 namespace hazelcast {
41  namespace client {
42 
43  namespace spi {
44  class ClientContext;
45  }
46 
47  namespace connection {
48  class ConnectionManager;
49  }
50 
57  class HAZELCAST_API TransactionContext {
58  public:
63  TransactionContext(spi::ClientContext &clientContext, const TransactionOptions &);
67  std::string getTxnId() const;
68 
74  void beginTransaction();
75 
81  void commitTransaction();
82 
88  void rollbackTransaction();
89 
98  template<typename K, typename V>
99  TransactionalMap<K, V> getMap(const std::string &name) {
100  return getTransactionalObject< TransactionalMap<K, V> >(name);
101  }
102 
110  template<typename E>
111  TransactionalQueue< E > getQueue(const std::string &name) {
112  return getTransactionalObject< TransactionalQueue< E > >(name);
113  }
114 
122  template<typename K, typename V>
123  TransactionalMultiMap<K, V> getMultiMap(const std::string &name) {
124  return getTransactionalObject< TransactionalMultiMap<K, V> >(name);
125  }
126 
134  template<typename E>
135  TransactionalList< E > getList(const std::string &name) {
136  return getTransactionalObject< TransactionalList< E > >(name);
137  }
138 
146  template<typename E>
147  TransactionalSet< E > getSet(const std::string &name) {
148  return getTransactionalObject< TransactionalSet< E > >(name);
149  }
150 
158  template<typename T>
159  T getTransactionalObject(const std::string &name) {
160  if (transaction.getState() != txn::TxnState::ACTIVE) {
161  std::string message = "No transaction is found while accessing ";
162  message += "transactional object -> [" + name + "]!";
163  throw exception::IllegalStateException("TransactionContext::getMap(const std::string& name)", message);
164  }
165  T txnObject(name, &transaction);
166  return txnObject;
167  }
168 
169  private :
170  const int CONNECTION_TRY_COUNT;
171  spi::ClientContext &clientContext;
172  TransactionOptions options;
173  boost::shared_ptr<connection::Connection> txnConnection;
174  txn::TransactionProxy transaction;
175 
176  boost::shared_ptr<connection::Connection> connect();
177 
178  };
179 
180  }
181 }
182 
183 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
184 #pragma warning(pop)
185 #endif
186 
187 #endif //HAZELCAST_TransactionContext
188 
Contains the configuration for a Hazelcast transaction.
Definition: TransactionOptions.h:67
TransactionalList< E > getList(const std::string &name)
Returns the transactional list instance with the specified name.
Definition: TransactionContext.h:135
TransactionalMap< K, V > getMap(const std::string &name)
Returns the transactional distributed map instance with the specified name.
Definition: TransactionContext.h:99
T getTransactionalObject(const std::string &name)
get any transactional object with template T.
Definition: TransactionContext.h:159
Transactional implementation of IMap.
Definition: TransactionalMap.h:44
Transactional implementation of MultiMap.
Definition: TransactionalMultiMap.h:37
Transactional implementation of IList.
Definition: TransactionalList.h:35
Transactional implementation of ISet.
Definition: TransactionalSet.h:34
TransactionalSet< E > getSet(const std::string &name)
Returns the transactional set instance with the specified name.
Definition: TransactionContext.h:147
TransactionalMultiMap< K, V > getMultiMap(const std::string &name)
Returns the transactional multimap instance with the specified name.
Definition: TransactionContext.h:123
Raised when method is called in a illegal state.
Definition: IllegalStateException.h:30
TransactionalQueue< E > getQueue(const std::string &name)
Returns the transactional queue instance with the specified name.
Definition: TransactionContext.h:111
Transactional implementation of IQueue.
Definition: TransactionalQueue.h:37
Provides a context to do transactional operations; so beginning/committing transactions, but also retrieving transactional data-structures like the TransactionalMap.
Definition: TransactionContext.h:57