全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 ページ
pooledDatabaseManager.h
1 #pragma once
2 #ifndef BZS_DB_PROTOCOL_TDAP_CLIENT_POOLEDDATABASEMANAGER_H
3 #define BZS_DB_PROTOCOL_TDAP_CLIENT_POOLEDDATABASEMANAGER_H
4 
5 /* =================================================================
6  Copyright (C) 2014 2016 BizStation Corp All rights reserved.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22  ================================================================= */
23 #include "trdboostapi.h"
24 #include "connectionPool.h"
25 namespace bzs
26 {
27 namespace db
28 {
29 namespace protocol
30 {
31 namespace tdap
32 {
33 namespace client
34 {
35 /** @cond INTERNAL */
36 class xaTransaction
37 {
38  std::vector<dbmanager_ptr> m_dbs;
39 
40 public:
41  void add(dbmanager_ptr& db) { m_dbs.push_back(db); }
42 
43  void unUse()
44  {
45  for (int i = (int)m_dbs.size() - 1; i >= 0; --i)
46  {
47  int ref = m_dbs[i].use_count();
48  m_dbs.erase(m_dbs.begin() + i);
49  if (ref == 2)
50  releaseConnection(&cpool);// Notify release
51  }
52  }
53 
54  void beginTrn(short bias)
55  {
56  for (size_t i = 0; i < m_dbs.size(); ++i)
57  {
58  m_dbs[i]->beginTrn(bias);
59  if (m_dbs[i]->stat())
60  {
61  abortTrn();
62  nstable::throwError(m_dbs[i]->uri(), m_dbs[i]->stat());
63  }
64  }
65  }
66 
67  void endTrn()
68  {
69  for (size_t i = 0; i < m_dbs.size(); ++i)
70  m_dbs[i]->endTrn();
71  }
72 
73  void abortTrn()
74  {
75  for (size_t i = 0; i < m_dbs.size(); ++i)
76  m_dbs[i]->abortTrn();
77  }
78 };
79 /** @endcond */
80 
81 /*
82 --------------------------------------
83 pooledDbManager::setMaxConnections(n);
84 
85 pooledDbManager db;
86 db.use(param)
87 activeTable a(db, "user");
88 ....
89 db.unUse();
90 --------------------------------------
91 Thread safe
92 Method : non thread safe.
93 Object : thread safe.
94 
95 */
97 {
98  dbmanager_ptr m_db;
99  bool m_inUse;
100  xaTransaction m_xa;
101  bool m_use_xa;
102 
103 public:
104  inline pooledDbManager() : m_inUse(false),m_use_xa(false){};
105 
106  inline pooledDbManager(const connectParams* param) : m_inUse(false)
107  {
108  use(param);
109  }
110 
112  {
113  if (m_inUse)
114  unUse();
115  }
116 
117  inline bool isUseXa() const {return m_use_xa;}
118 
119  inline void setUseXa(bool v) {m_use_xa = v;}
120 
121  inline void use(const connectParams* param = NULL)
122  {
123  m_db = cpool.get(param);
124  m_inUse = true;
125  m_xa.add(m_db);
126  }
127 
128  inline void unUse()
129  {
130  m_db.reset();
131  m_xa.unUse();
132  m_inUse = false;
133  }
134 
135  inline void reset(int v) { cpool.reset(v); }
136 
137  inline table_ptr table(const _TCHAR* name) { return m_db->table(name); }
138 
139  inline database* db() const { return m_db->db(); }
140 
141  inline const _TCHAR* uri() const { return m_db->uri(); }
142 
143  inline char_td mode() const { return m_db->mode(); }
144 
145  inline bool isOpened() const { return m_db->isOpened(); }
146 
147  inline void setOption(__int64 v) { m_db->setOption(v); }
148 
149  inline __int64 option() { return m_db->option(); }
150 
151  inline void beginTrn(short bias)
152  {
153  (m_use_xa == true) ? m_xa.beginTrn(bias) : m_db->beginTrn(bias);
154  }
155 
156  inline void endTrn()
157  {
158  (m_use_xa == true) ? m_xa.endTrn() : m_db->endTrn();
159  }
160 
161  inline void abortTrn()
162  {
163  (m_use_xa == true) ? m_xa.abortTrn() : m_db->abortTrn();
164  }
165 
166  inline int enableTrn() { return m_db->enableTrn(); }
167 
168  inline void beginSnapshot(short bias = CONSISTENT_READ, binlogPos* bpos = NULL)
169  {
170  m_db->beginSnapshot(bias, bpos);
171  }
172 
173  inline void endSnapshot() { m_db->endSnapshot(); }
174 
175  inline short_td stat() const { return m_db->stat(); }
176 
177  inline uchar_td* clientID() const { return m_db->clientID(); }
178 
179  inline static void setMaxConnections(int maxWorkerNum)
180  {
181  cpool.setMaxConnections(maxWorkerNum);
182  }
183 
184  inline static int maxConnections() { return cpool.maxConnections(); }
185 
186  inline static void reserve(size_t size, const connectParams& param)
187  {
188  cpool.reserve(size, param);
189  }
190 
191  inline int usingCount() const { return cpool.usingCount(); }
192 
193 };
194 
195 } // namespace client
196 } // namespace tdap
197 } // namespace protocol
198 } // namespace db
199 } // namespace bzs
200 
201 #endif // BZS_DB_PROTOCOL_TDAP_CLIENT_POOLEDDATABASEMANAGER_H
char_td mode() const
Definition: pooledDatabaseManager.h:143
uchar_td * clientID() const
Definition: pooledDatabaseManager.h:177
static void throwError(const _TCHAR *caption, short statusCode)
boost::shared_ptr< idatabaseManager > dbmanager_ptr
Definition: trdboostapi.h:1260
void use(const connectParams *param=NULL)
Definition: pooledDatabaseManager.h:121
~pooledDbManager()
Definition: pooledDatabaseManager.h:111
void abortTrn()
Definition: pooledDatabaseManager.h:161
void beginTrn(short bias)
Definition: pooledDatabaseManager.h:151
database * db() const
Definition: pooledDatabaseManager.h:139
boost::shared_ptr< table > table_ptr
Definition: fields.h:251
void endSnapshot()
Definition: pooledDatabaseManager.h:173
int enableTrn()
Definition: pooledDatabaseManager.h:166
int usingCount() const
Definition: pooledDatabaseManager.h:191
void reset(int v)
Definition: pooledDatabaseManager.h:135
table_ptr table(const _TCHAR *name)
Definition: pooledDatabaseManager.h:137
void unUse()
Definition: pooledDatabaseManager.h:128
void endTrn()
Definition: pooledDatabaseManager.h:156
short_td stat() const
Definition: pooledDatabaseManager.h:175
static int maxConnections()
Definition: pooledDatabaseManager.h:184
void releaseConnection(stdDbmCconnectionPool *pool)
__int64 option()
Definition: pooledDatabaseManager.h:149
バイナリーログの情報
Definition: nsDatabase.h:62
データベースとテーブルを管理するインタフェース
Definition: trdboostapi.h:172
pooledDbManager()
Definition: pooledDatabaseManager.h:104
bool isUseXa() const
Definition: pooledDatabaseManager.h:117
void setUseXa(bool v)
Definition: pooledDatabaseManager.h:119
void setOption(__int64 v)
Definition: pooledDatabaseManager.h:147
データベースアクセスクラス
Definition: database.h:59
void beginSnapshot(short bias=CONSISTENT_READ, binlogPos *bpos=NULL)
Definition: pooledDatabaseManager.h:168
const _TCHAR * uri() const
Definition: pooledDatabaseManager.h:141
pooledDbManager(const connectParams *param)
Definition: pooledDatabaseManager.h:106
コネクションプールのデータベースマネージャ
Definition: pooledDatabaseManager.h:96
データベースへの接続文字列生成ヘルパークラス
Definition: trdboostapi.h:72
static void reserve(size_t size, const connectParams &param)
Definition: pooledDatabaseManager.h:186
static void setMaxConnections(int maxWorkerNum)
Definition: pooledDatabaseManager.h:179
bool isOpened() const
Definition: pooledDatabaseManager.h:145

Transactd SDK 2018年07月31日(火) 19時40分22秒 doxygen