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 #ifndef HAZELCAST_TransactionContext
19 #define HAZELCAST_TransactionContext
20 
21 #include "hazelcast/client/TransactionOptions.h"
22 #include "hazelcast/client/txn/TransactionProxy.h"
23 #include "hazelcast/client/TransactionalMap.h"
24 #include "hazelcast/client/exception/IllegalStateException.h"
25 #include "hazelcast/client/TransactionalQueue.h"
26 #include "hazelcast/client/TransactionalMultiMap.h"
27 #include "hazelcast/client/TransactionalList.h"
28 #include "hazelcast/client/TransactionalSet.h"
29 
30 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
31 #pragma warning(push)
32 #pragma warning(disable: 4251) //for dll export
33 #endif
34 
35 namespace hazelcast {
36  namespace client {
37  namespace spi {
38  class ClientContext;
39  }
40 
41  namespace connection {
42  class ConnectionManager;
43  class Connection;
44  }
45 
52  class HAZELCAST_API TransactionContext {
53  public:
58  TransactionContext(spi::ClientContext &clientContext, const TransactionOptions &);
62  std::string getTxnId() const;
63 
69  void beginTransaction();
70 
76  void commitTransaction();
77 
83  void rollbackTransaction();
84 
93  template<typename K, typename V>
94  TransactionalMap<K, V> getMap(const std::string &name) {
95  return getTransactionalObject< TransactionalMap<K, V> >(name);
96  }
97 
105  template<typename E>
106  TransactionalQueue< E > getQueue(const std::string &name) {
107  return getTransactionalObject< TransactionalQueue< E > >(name);
108  }
109 
117  template<typename K, typename V>
118  TransactionalMultiMap<K, V> getMultiMap(const std::string &name) {
119  return getTransactionalObject< TransactionalMultiMap<K, V> >(name);
120  }
121 
129  template<typename E>
130  TransactionalList< E > getList(const std::string &name) {
131  return getTransactionalObject< TransactionalList< E > >(name);
132  }
133 
141  template<typename E>
142  TransactionalSet< E > getSet(const std::string &name) {
143  return getTransactionalObject< TransactionalSet< E > >(name);
144  }
145 
153  template<typename T>
154  T getTransactionalObject(const std::string &name) {
155  if (transaction.getState() != txn::TxnState::ACTIVE) {
156  std::string message = "No transaction is found while accessing ";
157  message += "transactional object -> [" + name + "]!";
158  throw exception::IllegalStateException("TransactionContext::getMap(const std::string& name)", message);
159  }
160  T txnObject(name, &transaction);
161  return txnObject;
162  }
163 
164  private :
165  const int CONNECTION_TRY_COUNT;
166  spi::ClientContext &clientContext;
167  TransactionOptions options;
168  boost::shared_ptr<connection::Connection> txnConnection;
169  txn::TransactionProxy transaction;
170 
171  boost::shared_ptr<connection::Connection> connect();
172 
173  };
174 
175  }
176 }
177 
178 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
179 #pragma warning(pop)
180 #endif
181 
182 #endif //HAZELCAST_TransactionContext
183 
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:130
TransactionalMap< K, V > getMap(const std::string &name)
Returns the transactional distributed map instance with the specified name.
Definition: TransactionContext.h:94
T getTransactionalObject(const std::string &name)
get any transactional object with template T.
Definition: TransactionContext.h:154
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:142
TransactionalMultiMap< K, V > getMultiMap(const std::string &name)
Returns the transactional multimap instance with the specified name.
Definition: TransactionContext.h:118
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:106
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:52