NCBI C++ ToolKit
dbapi_impl_context.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef DBAPI_DRIVER_IMPL___DBAPI_IMPL_CONTEXT__HPP
2 #define DBAPI_DRIVER_IMPL___DBAPI_IMPL_CONTEXT__HPP
3 
4 /* $Id: dbapi_impl_context.hpp 97607 2022-08-03 14:42:32Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Sergey Sikorskiy
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbimtx.hpp>
37 
39 
41 
42 
44 
45 class CDB_Connection;
46 class IConnValidator;
47 class CDB_UserHandler;
48 class CDB_Connection;
49 
50 namespace impl
51 {
52 
53 /////////////////////////////////////////////////////////////////////////////
54 // Forward declarations
55 
56 class CConnection;
57 
58 
60 {
61  enum EFlags {
62  fServerSet = 0x0001,
63  fPortSet = 0x0002,
64  fDatabaseSet = 0x0004,
65  fUsernameSet = 0x0008,
66  fPasswordSet = 0x0010,
67  fLoginTimeoutSet = 0x0020,
68  fIOTimeoutSet = 0x0040,
69  fCancelTimeoutSet = 0x0080,
70  fSingleServerSet = 0x0100,
71  fIsPooledSet = 0x0200,
72  fPoolMinSizeSet = 0x0400,
73  fPoolMaxSizeSet = 0x0800,
74  fArgsSet = 0x1000,
75  fPasswordFileSet = 0x2000,
76  fPasswordKeySet = 0x4000,
77  fPoolIdleTimeSet = 0x8000,
78  fPoolWaitTimeSet = 0x10000,
79  fPoolAllowTempSet = 0x20000,
80  fContRaiserrorSet = 0x40000,
81  fPoolMaxConnUseSet = 0x80000,
82  fLogMinorMsgsSet = 0x100000,
83  // , fPoolNameSet = 0x40000
84  };
85  typedef unsigned int TFlags;
86 
88  string server;
89  string port;
90  string database;
91  string username;
92  string password;
93  string password_file; // just used by SDBAPI, at least for now
94  string password_key_id; // just used by SDBAPI, at least for now
95  string login_timeout;
96  string io_timeout;
98  string single_server;
99  string is_pooled;
100  string pool_name;
101  string pool_minsize;
102  string pool_maxsize;
109  string args;
110 
111 
112  bool IsFlagSet(EFlags one_flag) { return (flags & one_flag) != 0; }
113 
114  bool IsServerSet(void) { return IsFlagSet(fServerSet); }
115  bool IsPortSet(void) { return IsFlagSet(fPortSet); }
116  bool IsDatabaseSet(void) { return IsFlagSet(fDatabaseSet); }
117  bool IsUsernameSet(void) { return IsFlagSet(fUsernameSet); }
118  bool IsPasswordSet(void) { return IsFlagSet(fPasswordSet); }
119  bool IsPasswordFileSet(void) { return IsFlagSet(fPasswordFileSet); }
120  bool IsPasswordKeySet(void) { return IsFlagSet(fPasswordKeySet); }
121  bool IsLoginTimeoutSet(void) { return IsFlagSet(fLoginTimeoutSet); }
122  bool IsIOTimeoutSet(void) { return IsFlagSet(fIOTimeoutSet); }
123  bool IsCancelTimeoutSet(void) { return IsFlagSet(fCancelTimeoutSet); }
124  bool IsSingleServerSet(void) { return IsFlagSet(fSingleServerSet); }
125  bool IsPooledSet(void) { return IsFlagSet(fIsPooledSet); }
126  // bool IsPoolNameSet(void) { return IsFlagSet(fPoolNameSet); }
127  bool IsPoolMinSizeSet(void) { return IsFlagSet(fPoolMinSizeSet); }
128  bool IsPoolMaxSizeSet(void) { return IsFlagSet(fPoolMaxSizeSet); }
129  bool IsPoolIdleTimeSet(void) { return IsFlagSet(fPoolIdleTimeSet); }
130  bool IsPoolWaitTimeSet(void) { return IsFlagSet(fPoolWaitTimeSet); }
132  { return IsFlagSet(fPoolAllowTempSet); }
134  { return IsFlagSet(fContRaiserrorSet); }
135  bool IsPoolMaxConnUseSet(void) { return IsFlagSet(fPoolMaxConnUseSet); }
137  { return IsFlagSet(fLogMinorMsgsSet); }
138  bool IsArgsSet(void) { return IsFlagSet(fArgsSet); }
139 
140  void Clear(void);
141 };
142 
143 
144 /////////////////////////////////////////////////////////////////////////////
145 ///
146 /// CDriverContext::
147 ///
148 
150 {
151  friend class impl::CConnection;
152  friend class CDBPoolBalancer;
153 
154 protected:
155  CDriverContext(void);
156 
157 public:
158  virtual ~CDriverContext(void);
159 
160 public:
161  virtual bool SetMaxBlobSize(size_t nof_bytes);
162  bool SetMaxTextImageSize(size_t nof_bytes)
163  { return SetMaxBlobSize(nof_bytes); }
164 
165  virtual bool SetLoginTimeout(unsigned int nof_secs = 0);
166  virtual unsigned int GetLoginTimeout(void) const;
167 
168  virtual bool SetTimeout(unsigned int nof_secs = 0);
169  virtual unsigned int GetTimeout(void) const;
170 
171  virtual bool SetCancelTimeout(unsigned int nof_secs);
172  virtual unsigned int GetCancelTimeout(void) const;
173 
174  virtual void SetApplicationName(const string& app_name);
175  virtual string GetApplicationName(void) const;
176 
177  virtual void SetHostName(const string& host_name);
178  virtual string GetHostName(void) const;
179 
180  virtual CDB_Connection* MakeConnection(const CDBConnParams& params);
181 
182  /// Return number of currently open connections in this context.
183  /// If "srv_name" is not NULL, then return # of conn. open to that server.
184  virtual unsigned int NofConnections(const string& srv_name = "",
185  const string& pool_name = "")
186  const;
187  virtual unsigned int NofConnections(const TSvrRef& svr_ref,
188  const string& pool_name = "") const;
189 
190  /// Add message handler "h" to process 'context-wide' (not bound
191  /// to any particular connection) error messages.
192  virtual void PushCntxMsgHandler(CDB_UserHandler* h,
193  EOwnership ownership = eNoOwnership);
194 
195  /// Remove message handler "h" and all handlers above it in the stack
196  virtual void PopCntxMsgHandler(CDB_UserHandler* h);
197 
198  /// Add `per-connection' err.message handler "h" to the stack of default
199  /// handlers which are inherited by all newly created connections.
200  virtual void PushDefConnMsgHandler(CDB_UserHandler* h,
201  EOwnership ownership = eNoOwnership);
202 
203  /// Remove `per-connection' mess. handler "h" and all above it in the stack.
204  virtual void PopDefConnMsgHandler(CDB_UserHandler* h);
205 
206  /// close reusable deleted connections for specified server and/or pool
207  void CloseUnusedConnections(const string& srv_name = kEmptyStr,
208  const string& pool_name = kEmptyStr,
209  unsigned int max_closings = kMax_UInt);
210 
212  {
213  return m_ConnHandlers;
214  }
216  {
217  return m_CntxHandlers;
218  }
219 
220  virtual void SetClientCharset(const string& charset);
221  const string& GetClientCharset(void) const
222  {
223  return m_ClientCharset;
224  }
226  {
227  return m_ClientEncoding;
228  }
229  bool IsMultibyteClientEncoding(void) const
230  {
231  return (GetClientEncoding() == eEncoding_UTF8);
232  }
233 
234  size_t GetMaxBlobSize(void) const
235  {
236  return m_MaxBlobSize;
237  }
238  size_t GetMaxTextImageSize(void) const { return GetMaxBlobSize(); }
239 
240  static void ResetEnvSybase(void);
241 
242  void ReadDBConfParams (const string& service_name, SDBConfParams* params);
243  bool SatisfyPoolMinimum(const CDBConnParams& params);
244 
245  // Provides the number of the invalidated connections
246  size_t CloseConnsForPool (const string& pool_name, Uint4 keep_host_ip=0,
247  Uint2 keep_port=0);
248  void CloseOldIdleConns (unsigned int max_closings,
249  const string& pool_name = kEmptyStr);
250 
251  typedef map<string, unsigned int> TCounts; // by server name
252  typedef map<string, TCounts> TCountsMap; // by pool or service name
253 
254  void GetCountsForPool(const string& pool_name, TCounts* counts) const
255  {
256  x_GetCounts(m_CountsByPool, pool_name, counts);
257  }
258  void GetCountsForService(const string& service, TCounts* counts) const
259  {
260  x_GetCounts(m_CountsByService, service, counts);
261  }
262 
263 protected:
264  typedef list<CConnection*> TConnPool;
265 
266  virtual CRWLock& x_GetCtxLock(void) const
267  {
268  return m_DefaultCtxLock;
269  }
270 
271  // To allow children of CDriverContext to create CDB_Connection
272  CDB_Connection* MakeCDBConnection(CConnection* connection,
273  int delta /* = 1 */);
274  CDB_Connection* MakePooledConnection(const CDBConnParams& params);
275  virtual CConnection* MakeIConnection(const CDBConnParams& params) = 0;
276  void DestroyConnImpl(CConnection* impl);
277 
278  void CloseAllConn(void);
279  void DeleteAllConn(void);
280 
282  {
283  return m_CntxHandlers;
284  }
285 
286  void UpdateConnMaxBlobSize(void) const;
287  void UpdateConnTimeout(void) const;
288 
289  void SetExtraMsg(const string& msg) const
290  {
291  m_ExtraMsg = msg;
292  }
293  const string& GetExtraMsg(void) const
294  {
295  return m_ExtraMsg;
296  }
297 
298 
299 private:
300  struct SConsumer {
301  SConsumer(CTempString subj, bool is_pool)
302  : conn(nullptr), subject(subj), subject_is_pool(is_pool),
303  selected(false)
304  { }
305 
309  bool selected;
310  };
311 
313  mutable CRWLock m_PoolLock; //< for m_PoolSem*, m_(Not)InUse, and m_Counts*
315  list<SConsumer> m_PoolSemConsumers;
316 
317  unsigned int m_LoginTimeout; //< Login timeout.
318  unsigned int m_Timeout; //< Connection timeout.
319  unsigned int m_CancelTimeout;//< Canceling timeout.
320 
321  string m_AppName; //< Application name.
322  string m_HostName; //< Host name
323 
324  /// Unused(reserve) connections
326  /// Used connections
328 
331 
332  /// Stack of `per-context' err.message handlers
334  /// Stacks of `per-connection' err.message handlers
336 
337  mutable string m_ExtraMsg;
338 
342 
343  /// Return unused connection "conn" to the driver context for future
344  /// reuse (if "conn_reusable" is TRUE) or utilization
345  void x_Recycle(CConnection* conn, bool conn_reusable);
346 
347  void x_AdjustCounts(const CConnection* conn, int delta);
348  void x_GetCounts(const TCountsMap& main_map, const string& name,
349  TCounts* counts) const;
350 };
351 
352 
354 {
355 protected:
356  CWinSock(void);
357  ~CWinSock(void);
358 };
359 
360 } //namespace impl
361 
363 
364 
365 #endif /* DBAPI_DRIVER_IMPL___DBAPI_IMPL_CONTEXT__HPP */
CDBConnParams::
Definition: interfaces.hpp:258
CRWLock –.
Definition: ncbimtx.hpp:953
CSemaphore –.
Definition: ncbimtx.hpp:1375
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CDBHandlerStack m_ConnHandlers
Stacks of `per-connection' err.message handlers.
const string & GetExtraMsg(void) const
list< SConsumer > m_PoolSemConsumers
bool IsMultibyteClientEncoding(void) const
size_t GetMaxBlobSize(void) const
CDBHandlerStack m_CntxHandlers
Stack of `per-context' err.message handlers.
const CDBHandlerStack & GetConnHandlerStack(void) const
void GetCountsForPool(const string &pool_name, TCounts *counts) const
const CDBHandlerStack & GetCtxHandlerStack(void) const
void SetExtraMsg(const string &msg) const
void GetCountsForService(const string &service, TCounts *counts) const
map< string, TCounts > TCountsMap
TConnPool m_NotInUse
Unused(reserve) connections.
bool SetMaxTextImageSize(size_t nof_bytes)
virtual CRWLock & x_GetCtxLock(void) const
virtual CConnection * MakeIConnection(const CDBConnParams &params)=0
const string & GetClientCharset(void) const
size_t GetMaxTextImageSize(void) const
map< string, unsigned int > TCounts
TConnPool m_InUse
Used connections.
CDBHandlerStack & GetCtxHandlerStack(void)
EEncoding GetClientEncoding(void) const
list< CConnection * > TConnPool
Include a standard set of the NCBI C++ Toolkit most basic headers.
static uch flags
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
#define false
Definition: bool.h:36
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
#define kMax_UInt
Definition: ncbi_limits.h:185
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
EEncoding
Definition: ncbistr.hpp:199
#define kEmptyStr
Definition: ncbistr.hpp:123
@ eEncoding_UTF8
Definition: ncbistr.hpp:201
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_DBAPIDRIVER_EXPORT
Definition: ncbi_export.h:392
#define nullptr
Definition: ncbimisc.hpp:45
Multi-threading – mutexes; rw-locks; semaphore.
Int4 delta(size_t dimension_, const Int4 *score_)
static string subject
SConsumer(CTempString subj, bool is_pool)
bool IsFlagSet(EFlags one_flag)
bool IsContinueAfterRaiserrorSet(void)
bool IsPoolAllowTempOverflowSet(void)
Modified on Sat Apr 13 11:43:16 2024 by modify_doxy.py rev. 669887