NCBI C++ ToolKit
nst_dbconnection_thread.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: nst_dbconnection_thread.cpp 73408 2016-07-06 14:56:12Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Sergey Satskiy
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 #include <dbapi/simple/sdbapi.hpp>
32 #include <corelib/ncbi_system.hpp>
34 
36 #include "nst_precise_time.hpp"
37 #include "nst_database.hpp"
38 
39 
41 
42 
45 
46 
48  CDatabase * & db,
49  CNSTDatabase * db_wrapper,
50  CFastMutex & db_lock) :
51  m_Connected(connected), m_Database(db),
52  m_DBWrapper(db_wrapper),
53  m_DbLock(db_lock),
54  m_StopSignal(0, 10000000)
55 {}
56 
57 
59 {}
60 
61 
63 {
64  m_StopFlag.Add(1);
65  Wakeup();
66 }
67 
68 
70 {
72 }
73 
74 
76 {
77  SetCurrentThreadName("netstoraged_dbc");
78  for (;;) {
79  try {
80  if (m_StopFlag.Get() != 0)
81  break;
83 
84  // There are two cases:
85  // - could not decrypt the DB password
86  // - connection is lost
87  if (m_Database == NULL)
90  } catch (const exception & ex) {
91  ERR_POST("Unexpected exception in the DB restore "
92  "connection thread Main() function: " << ex <<
93  " Ignore and continue.");
94  } catch (...) {
95  ERR_POST("Unexpected unknown exception in the DB restore "
96  "connection thread Main() function. Ignore and continue.");
97  }
98  }
99  return NULL;
100 }
101 
102 
104 {
105  m_DbLock.Lock();
106  if (m_Database == NULL) {
107  m_DbLock.Unlock();
108  return;
109  }
110  m_DbLock.Unlock();
111 
112  while (m_StopFlag.Get() == 0 && m_Connected == false) {
114 
115  try {
116  {
117  CFastMutexGuard guard(m_DbLock);
118  m_Database->Close();
119  m_Database->Connect();
120  m_Connected = true;
121  }
122  LOG_POST(Note << "Database connection has been restored");
123  return;
124  } catch (const exception & ex) {
125  ERR_POST("Exception while restoring DB connection: " << ex <<
126  " Ignore and continue.");
127  } catch (...) {
128  ERR_POST("Unknown exception while restoring DB connection. "
129  "Ignore and continue.");
130  }
131 
132  CNSTPreciseTime loop_time = CNSTPreciseTime::Current() - start;
133  if (loop_time < gs_MinRetryToConnectTimeout)
134  m_StopSignal.TryWait(1, 0);
135  }
136 }
137 
138 
139 // The function serves the case when there was a problem of decrypting the
140 // database password parameter. The server should try to decrypt the password
141 // every 100 seconds.
143 {
144  if (m_Database != NULL)
145  return;
146 
147  while (m_StopFlag.Get() == 0) {
149 
150  try {
151  // Reset the key file cache
153 
154  // false -> this not initialization; this is a periodic try to
155  // decrypt the database password
157  if (m_Database != NULL) {
158  LOG_POST(Note << "Encrypted database password "
159  "has been decrypted");
160  return;
161  }
162  } catch (const exception & ex) {
163  ERR_POST("Exception while decrypting database password: " << ex <<
164  " Ignore and continue.");
165  } catch (...) {
166  ERR_POST("Unknown exception while decrypting database password. "
167  "Ignore and continue.");
168  }
169 
170  CNSTPreciseTime loop_time = CNSTPreciseTime::Current() - start;
171  if (loop_time < gs_MinRetryToCreateTimeout)
172  m_StopSignal.TryWait(100, 0);
173  }
174 }
175 
177 
178 
Database connection object.
Definition: sdbapi.hpp:1137
void Close(void)
Close database object.
Definition: sdbapi.cpp:2028
void Connect(void)
Explicitly (re)connect to the database server.
Definition: sdbapi.cpp:2016
CFastMutex –.
Definition: ncbimtx.hpp:667
CAtomicCounter_WithAutoInit m_StopFlag
virtual void * Main(void)
Derived (user-created) class must provide a real thread function.
CNSTDBConnectionThread(bool &connected, CDatabase *&db, CNSTDatabase *db_wrapper, CFastMutex &db_lock)
void x_CreateDatabase(bool is_initialization)
static CNSTPreciseTime Current(void)
static void Reload(void)
Re-read key file locations and domain paths, reload encryption keys.
#define NULL
Definition: ncbistd.hpp:225
TValue Add(int delta) THROWS_NONE
Atomically add value (=delta), and return new counter value.
Definition: ncbicntr.hpp:278
TValue Get(void) const THROWS_NONE
Get atomic counter value.
Definition: ncbicntr.hpp:168
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
void Lock(void)
Acquire mutex for the current thread with no nesting checks.
void Wait(void)
Wait on semaphore.
Definition: ncbimtx.cpp:1787
static void SetCurrentThreadName(const CTempString &)
Set name for the current thread.
Definition: ncbithr.cpp:958
bool TryWait(unsigned int timeout_sec=0, unsigned int timeout_nsec=0)
Timed wait.
Definition: ncbimtx.cpp:1844
void Post(unsigned int count=1)
Increment the semaphore by "count".
Definition: ncbimtx.cpp:1971
void Unlock(void)
Release mutex with no owner or nesting checks.
where both of them are integers Note
static const CNSTPreciseTime gs_MinRetryToConnectTimeout(1.0)
static const CNSTPreciseTime gs_MinRetryToCreateTimeout(100.0)
Defines NCBI C++ secure resources API.
Modified on Mon May 27 04:38:10 2024 by modify_doxy.py rev. 669887