46 #if defined(NCBI_OS_MSWIN)
47 # include <winsock2.h>
53 #define NCBI_USE_ERRCODE_X Dbapi_DrvrContext
64 const string& server_name)
66 if (pool_name.empty()) {
69 return conn->ServerName() == server_name
70 ||
conn->GetRequestedServer() == server_name;
72 return conn->PoolName() == pool_name;
233 con->PopMsgHandler(h);
238 con->PopMsgHandler(h);
253 if (
env.Get(
"LC_ALL") ==
"POSIX") {
255 env.Set(
"LC_ALL",
"C");
257 if (!
env.Get(
"SYBASE").empty()) {
258 string reset =
env.Get(
"RESET_SYBASE");
299 if ( !consumer.selected
300 && (consumer.subject_is_pool
301 ?
conn->PoolName() == consumer.subject
303 consumer.selected =
true;
304 recipient = &consumer.conn;
314 if (conn_reusable &&
conn->IsOpeningFinished() &&
conn->IsValid()) {
321 if (
conn->m_Pool.empty()) {
326 if (
n >=
conn->m_PoolMaxSize) {
331 if (keep &&
conn->m_ReuseCount + 1 >
conn->m_PoolMaxConnUse) {
340 conn->m_CleanupTime = now +
conn->m_PoolIdleTimeParam;
344 if (recipient !=
nullptr) {
361 string server_name =
conn->GetServerName();
362 if (
conn->Host() != 0 && server_name.find(
'@') ==
NPOS) {
364 if (
conn->Port() != 0) {
372 _TRACE(server_name <<
" count += " <<
delta <<
" for pool "
373 <<
conn->PoolName() <<
" (" << pool_count <<
") and service "
374 <<
conn->GetRequestedServer() <<
" (" << service_count <<
')');
380 const string& name,
TCounts* counts)
const
383 auto it = main_map.
find(name);
384 if (it == main_map.
end()) {
387 *counts = it->second;
393 const string& pool_name,
394 unsigned int max_closings)
404 if ( !srv_name.empty() && srv_name != con->ServerName()
405 && srv_name != con->GetRequestedServer() )
407 if((!pool_name.empty()) && pool_name.compare(con->PoolName()))
continue;
412 if (--max_closings == 0) {
419 const string& pool_name)
const
423 if ((!svr_ref || !svr_ref->
IsValid()) && pool_name.empty()) {
442 if(!server.empty()) {
443 if (server != con->ServerName()
444 && server != con->GetRequestedServer())
447 else if (host != 0) {
448 if (host != con->Host() || port != con->Port())
451 if((!pool_name.empty()) && pool_name.compare(con->PoolName()))
continue;
460 const string& pool_name)
const
480 if (params.
GetParam(
"is_pooled") ==
"true") {
483 string pool_name (params.
GetParam(
"pool_name")),
488 if (
s_Matches(t_con, pool_name, server_name)) {
512 string pool_max_str(params.
GetParam(
"pool_maxsize"));
513 if (!pool_max_str.empty() && pool_max_str !=
"default") {
519 if (
s_Matches(t_con, pool_name, server_name)) {
523 if (total_cnt >= pool_max) {
525 string timeout_str(params.
GetParam(
"pool_wait_time"));
526 double timeout_val = 0.0;
527 if ( !timeout_str.empty() && timeout_str !=
"default") {
532 if (pool_name.empty()) {
541 bool timed_out =
true;
542 bool relocked =
false;
546 if (target->selected) {
549 if (&it != &*target && it.selected) {
564 if (*it == target->conn) {
565 t_con = target->conn;
582 if (params.
GetParam(
"pool_allow_temp_overflow")
584 string msg =
FORMAT(
"Connection pool full (size "
585 << pool_max <<
") for "
586 << (pool_name.empty() ? server_name
595 if (params.
GetParam(
"do_not_connect") ==
"true") {
600 if (!TDbapi_CanUseKerberos::GetDefault()
604 string err_msg(
"Insufficient credentials to connect.");
607 err_msg +=
" User name has not been set.";
610 err_msg +=
" Password has not been set.";
749 const string& dn,
const string& pass)
769 string& db_name,
string& password)
771 if (!TDbapi_ConnUseEncryptData::GetDefault())
780 string res_name = app_name;
781 if (!user_name.empty()) {
783 res_name += user_name;
785 if (!server_name.empty()) {
787 res_name += server_name;
789 if (!db_name.empty()) {
798 password =
info.GetValue();
800 typedef TExtraMap::const_iterator TExtraMapIt;
801 const TExtraMap& extra =
info.GetExtraValues().GetPairs();
803 TExtraMapIt it = extra.find(
"server");
804 if (it != extra.end())
805 server_name = it->second;
806 it = extra.find(
"username");
807 if (it != extra.end())
808 user_name = it->second;
809 it = extra.find(
"database");
810 if (it != extra.end())
811 db_name = it->second;
814 SLoginData(server_name, user_name, db_name, password)).second)
817 "Circular dependency inside resources info file.", 100012);
850 if (service_name.empty())
858 string section_name(service_name);
859 section_name.append(1,
'.');
860 section_name.append(
"dbservice");
866 params->
server = reg.
Get(section_name,
"service");
870 params->
port = reg.
Get(section_name,
"port");
874 params->
database = reg.
Get(section_name,
"database");
878 params->
username = reg.
Get(section_name,
"username");
886 params->
password = reg.
Get(section_name,
"password");
890 }
NCBI_CATCH(
"Password decryption for " + service_name);
893 <<
"Using unencrypted password for " + service_name);
896 if (reg.
HasEntry(section_name,
"password_file",
922 params->
is_pooled = reg.
Get(section_name,
"use_conn_pool");
929 params->
pool_name = reg.
Get(section_name,
"conn_pool_name");
939 if (reg.
HasEntry(section_name,
"conn_pool_idle_time",
944 if (reg.
HasEntry(section_name,
"conn_pool_wait_time",
949 if (reg.
HasEntry(section_name,
"conn_pool_allow_temp_overflow",
953 = reg.
Get(section_name,
"conn_pool_allow_temp_overflow");
955 if (reg.
HasEntry(section_name,
"continue_after_raiserror",
959 = reg.
Get(section_name,
"continue_after_raiserror");
961 if (reg.
HasEntry(section_name,
"conn_pool_max_conn_use",
965 = reg.
Get(section_name,
"conn_pool_max_conn_use");
967 if (reg.
HasEntry(section_name,
"log_minor_messages",
971 = reg.
Get(section_name,
"log_minor_messages");
975 params->
args = reg.
Get(section_name,
"args");
984 string pool_min_str = params.
GetParam(
"pool_minsize");
985 if (pool_min_str.empty() || pool_min_str ==
"default")
991 string pool_name = params.
GetParam(
"pool_name"),
1011 vector< AutoPtr<CDB_Connection> > conns(pool_min);
1012 for (
int i = total_cnt;
i < pool_min; ++
i) {
1017 ERR_POST_X(1,
"Error filling connection pool: " << ex);
1030 conf_params.
Clear();
1031 if (params.
GetParam(
"do_not_read_conf") !=
"true") {
1035 unique_ptr<CDB_Connection> t_con;
1050 unsigned int timeout;
1051 if (cfactory !=
nullptr) {
1061 if (
value ==
"default") {
1063 }
else if (!
value.empty()) {
1069 if (timeout == 0 && cfactory !=
nullptr) {
1082 if (
value ==
"default") {
1084 }
else if (!
value.empty()) {
1090 if (cfactory !=
nullptr) {
1091 auto validation_timeout = cfactory->GetConnectionTimeout();
1092 if (validation_timeout == 0) {
1093 validation_timeout = timeout ? timeout : 30;
1110 if (
value ==
"default") {
1113 else if (!
value.empty()) {
1119 act_params.
SetParam(
"single_server",
"true");
1122 act_params.
SetParam(
"single_server",
1127 else if (params.
GetParam(
"single_server") ==
"default") {
1128 act_params.
SetParam(
"single_server",
"true");
1132 act_params.
SetParam(
"is_pooled",
"false");
1141 else if (params.
GetParam(
"is_pooled") ==
"default") {
1142 act_params.
SetParam(
"is_pooled",
"false");
1146 else if (params.
GetParam(
"pool_minsize") ==
"default") {
1147 act_params.
SetParam(
"pool_minsize",
"0");
1151 else if (params.
GetParam(
"pool_maxsize") ==
"default") {
1152 act_params.
SetParam(
"pool_maxsize",
"");
1156 else if (params.
GetParam(
"pool_idle_time") ==
"default") {
1157 act_params.
SetParam(
"pool_idle_time",
"");
1161 else if (params.
GetParam(
"pool_wait_time") ==
"default") {
1162 act_params.
SetParam(
"pool_wait_time",
"0");
1166 act_params.
SetParam(
"pool_allow_temp_overflow",
"false");
1170 (
"pool_allow_temp_overflow",
1176 else if (params.
GetParam(
"pool_allow_temp_overflow") ==
"default") {
1177 act_params.
SetParam(
"pool_allow_temp_overflow",
"false");
1180 act_params.
SetParam(
"pool_max_conn_use",
1182 else if (params.
GetParam(
"pool_max_conn_use") ==
"default") {
1183 act_params.
SetParam(
"pool_max_conn_use",
"0");
1187 act_params.
SetParam(
"continue_after_raiserror",
"false");
1191 (
"continue_after_raiserror",
1197 else if (params.
GetParam(
"continue_after_raiserror") ==
"default") {
1198 act_params.
SetParam(
"continue_after_raiserror",
"false");
1202 act_params.
SetParam(
"log_minor_messages",
"false");
1206 (
"log_minor_messages",
1212 else if (params.
GetParam(
"log_minor_messages") ==
"default") {
1213 act_params.
SetParam(
"log_minor_messages",
"false");
1225 if (t_con.get() ==
NULL) {
1226 if (act_params.
GetParam(
"do_not_connect") ==
"true") {
1231 err +=
"Cannot connect to the server '" + act_params.
GetServerName();
1232 err +=
"' as user '" + act_params.
GetUserName() +
"'";
1238 ex.AddPrevious(*it);
1243 t_con->SetTimeout(timeout);
1249 return t_con.release();
1255 size_t invalidated_count = 0;
1261 if (t_con->
Host() != keep_host_ip || t_con->
Port() != keep_port) {
1263 ++invalidated_count;
1270 if (t_con->
Host() != keep_host_ip || t_con->
Port() != keep_port) {
1277 return invalidated_count;
1282 const string& pool_name)
1285 if (max_closings == 0) {
1292 const string& pool_name_2 = (*it)->PoolName();
1294 = pool_name_2.
empty() ? at_min_by_server : at_min_by_pool;
1295 if (pool_name_2.empty()) {
1296 if ( !pool_name.empty()
1297 || at_min.
find((*it)->GetRequestedServer()) != at_min.
end()) {
1300 }
else if (( !pool_name.empty() && pool_name != pool_name_2)
1301 || at_min.
find(pool_name_2) != at_min.
end()) {
1304 if ((*it)->m_CleanupTime.IsEmpty() || (*it)->m_CleanupTime > now) {
1308 if (pool_name_2.empty()) {
1313 if (
n > (*it)->m_PoolMinSize) {
1317 if (--max_closings == 0) {
1321 at_min.
insert(pool_name_2);
1330 impl->ReleaseInterface();
1363 if (!t_con)
continue;
1370 if (!t_con)
continue;
1383 if (!t_con)
continue;
1390 if (!t_con)
continue;
1400 #if defined(NCBI_OS_MSWIN)
1402 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
1411 #if defined(NCBI_OS_MSWIN)
unsigned int GetLoginTimeout(void) const
Uint2 GetPort(void) const
const string & GetName(void) const
Uint4 GetHost(void) const
CRef< IDBConnectionFactory > GetConnectionFactory(void) const
Retrieve a connection factory.
static CDbapiConnMgr & Instance(void)
Get access to the class instance.
void Guard(resource_type &resource)
Manually force the guard to protect some other resource.
void Release()
Manually force the resource to be released.
static CNcbiApplication * Instance(void)
Singleton method.
static bool IsEncrypted(const string &data)
Check if the string contains valid encrypted data.
static string Decrypt(const string &encrypted_string)
Decrypt a string using the matching key found in the NCBI keys files.
Class for handling resource info files.
static string GetDefaultFileName(void)
Get default resource info file location (/etc/ncbi/.info).
const CNcbiResourceInfo & GetResourceInfo(const string &res_name, const string &pwd) const
Get read-only resource info for the given name.
Class for storing encrypted resource information.
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
CTimeout – Timeout interval.
virtual CDB_Connection * MakeDBConnection(I_DriverContext &ctx, const CDBConnParams ¶ms, CDB_UserHandler::TExceptions &exceptions)=0
Create new connection object for the given context and connection attributes.
virtual void SetTimeout(size_t nof_secs)=0
virtual void SetBlobSize(size_t nof_bytes)
bool IsReusable(void) const
Check if this connection is a reusable one.
virtual bool IsAlive(void)=0
Check out if connection is alive (this function doesn't ping the server, it just checks the status of...
const string & PoolName(void) const
Find out which connection pool this connection belongs to.
impl::CDBConnParamsBase::
virtual string GetPassword(void) const
virtual string GetDatabaseName(void) const
virtual string GetUserName(void) const
void SetUserName(const string &name)
virtual string GetParam(const string &key) const
Parameters, which are not listed above explicitly, should be retrieved via SetParam() method.
void SetParam(const string &key, const string &value)
void SetServerName(const string &name)
void SetPassword(const string &passwd)
virtual string GetServerName(void) const
void SetDatabaseName(const string &name)
void Pop(CDB_UserHandler *h, bool last=true)
void Push(CDB_UserHandler *h, EOwnership ownership=eNoOwnership)
virtual void PopDefConnMsgHandler(CDB_UserHandler *h)
Remove `per-connection' mess. handler "h" and all above it in the stack.
virtual void PushDefConnMsgHandler(CDB_UserHandler *h, EOwnership ownership=eNoOwnership)
Add `per-connection' err.message handler "h" to the stack of default handlers which are inherited by ...
CDBHandlerStack m_ConnHandlers
Stacks of `per-connection' err.message handlers.
void x_AdjustCounts(const CConnection *conn, int delta)
virtual CDB_Connection * MakeConnection(const CDBConnParams ¶ms)
Create connection object using Load Balancer / connection factory.
size_t CloseConnsForPool(const string &pool_name, Uint4 keep_host_ip=0, Uint2 keep_port=0)
virtual void SetHostName(const string &host_name)
Set host name.
virtual void PushCntxMsgHandler(CDB_UserHandler *h, EOwnership ownership=eNoOwnership)
Add message handler "h" to process 'context-wide' (not bound to any particular connection) error mess...
list< SConsumer > m_PoolSemConsumers
void ReadDBConfParams(const string &service_name, SDBConfParams *params)
size_t GetMaxBlobSize(void) const
CDBHandlerStack m_CntxHandlers
Stack of `per-context' err.message handlers.
virtual string GetHostName(void) const
Get host name.
bool SatisfyPoolMinimum(const CDBConnParams ¶ms)
void x_Recycle(CConnection *conn, bool conn_reusable)
Return unused connection "conn" to the driver context for future reuse (if "conn_reusable" is TRUE) o...
virtual unsigned int GetCancelTimeout(void) const
unsigned int m_LoginTimeout
unsigned int m_CancelTimeout
virtual unsigned int NofConnections(const string &srv_name="", const string &pool_name="") const
Return number of currently open connections in this context.
TCountsMap m_CountsByPool
virtual bool SetTimeout(unsigned int nof_secs=0)
Set connection timeout.
static void ResetEnvSybase(void)
virtual ~CDriverContext(void)
void UpdateConnMaxBlobSize(void) const
TConnPool m_NotInUse
Unused(reserve) connections.
virtual string GetApplicationName(void) const
Return application name.
virtual void PopCntxMsgHandler(CDB_UserHandler *h)
Remove message handler "h" and all handlers above it in the stack.
CDB_Connection * MakePooledConnection(const CDBConnParams ¶ms)
Create connection object WITHOUT using of Load Balancer / connection factory.
virtual bool SetMaxBlobSize(size_t nof_bytes)
Set maximal size for BLOB data.
virtual void SetClientCharset(const string &charset)
virtual CRWLock & x_GetCtxLock(void) const
void CloseOldIdleConns(unsigned int max_closings, const string &pool_name=kEmptyStr)
virtual unsigned int GetLoginTimeout(void) const
Get login timeout.
virtual CConnection * MakeIConnection(const CDBConnParams ¶ms)=0
virtual void SetApplicationName(const string &app_name)
Set application name.
virtual bool SetLoginTimeout(unsigned int nof_secs=0)
Set login timeout.
CDB_Connection * MakeCDBConnection(CConnection *connection, int delta)
TConnPool m_InUse
Used connections.
EEncoding m_ClientEncoding
virtual unsigned int GetTimeout(void) const
Get connection timeout.
TCountsMap m_CountsByService
void UpdateConnTimeout(void) const
void x_GetCounts(const TCountsMap &main_map, const string &name, TCounts *counts) const
void CloseUnusedConnections(const string &srv_name=kEmptyStr, const string &pool_name=kEmptyStr, unsigned int max_closings=kMax_UInt)
close reusable deleted connections for specified server and/or pool
void DestroyConnImpl(CConnection *impl)
list< CConnection * > TConnPool
virtual bool SetCancelTimeout(unsigned int nof_secs)
virtual string GetUserName(void) const
virtual Uint4 GetProtocolVersion(void) const
virtual CRef< IConnValidator > GetConnValidator(void) const
~CMakeConnActualParams(void)
virtual string GetDatabaseName(void) const
const CDBConnParams & m_Other
virtual string GetPassword(void) const
virtual Uint2 GetPort(void) const
void SetUserName(const string &name)
virtual EEncoding GetEncoding(void) const
virtual string GetParam(const string &key) const
Parameters, which are not listed above explicitly, should be retrieved via SetParam() method.
void SetParam(const string &key, const string &value)
virtual string GetServerName(void) const
virtual Uint4 GetHost(void) const
virtual EServerType GetServerType(void) const
void SetServerName(const string &name)
CMakeConnActualParams(const CDBConnParams &other)
void SetPassword(const string &passwd)
void SetDatabaseName(const string &name)
const_iterator end() const
const_iterator find(const key_type &key) const
iterator_bool insert(const value_type &val)
const_iterator find(const key_type &key) const
const_iterator end() const
static CS_CONNECTION * conn
NCBI_PARAM_DEF_EX(bool, dbapi, conn_use_encrypt_data, false, eParam_NoThread, NULL)
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
constexpr size_t ArraySize(const Element(&)[Size])
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
#define ERASE_ITERATE(Type, Var, Cont)
Non-constant version with ability to erase current element, if container permits.
const string & GetProgramDisplayName(void) const
Get the application's "display" name.
#define NON_CONST_REVERSE_ITERATE(Type, Var, Cont)
Non constant version of REVERSE_ITERATE macro.
@ eRetriable_No
It makes no sense to retry the action.
@ eNegative
Value is negative.
@ eTakeOwnership
An object can take ownership of another.
void SetDatabaseName(const string &d)
#define DATABASE_DRIVER_ERROR(message, err_code)
static CDB_UserHandler & GetDefault(void)
deque< CDB_Exception * > TExceptions
Exception container type.
virtual EEncoding GetEncoding(void) const =0
virtual EServerType GetServerType(void) const =0
virtual string GetPassword(void) const =0
virtual Uint4 GetProtocolVersion(void) const =0
virtual Uint4 GetHost(void) const =0
virtual string GetServerName(void) const =0
virtual Uint2 GetPort(void) const =0
virtual string GetDatabaseName(void) const =0
virtual CRef< IConnValidator > GetConnValidator(void) const =0
virtual string GetUserName(void) const =0
virtual string GetParam(const string &key) const =0
Parameters, which are not listed above explicitly, should be retrieved via SetParam() method.
virtual bool Refresh()
Reset the connection to the "ready" state (cancel all active commands)
#define DIAG_COMPILE_INFO
Make compile time diagnostic information object to use in CNcbiDiag and CException.
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
@ eDiag_Error
Error message.
#define NCBI_CATCH(message)
Catch CExceptions as well This macro is deprecated - use *_X or *_XX variant instead of it.
#define STD_CATCH_ALL(message)
Standard handling of "exception"-derived exceptions; catches non-standard exceptions and generates "u...
void Warning(CExceptionArgs_Base &args)
#define FORMAT(message)
Format message using iostreams library.
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
@ eParam_NoThread
Do not use per-thread values.
uint32_t Uint4
4-byte (32-bit) unsigned integer
uint16_t Uint2
2-byte (16-bit) unsigned integer
virtual const string & Get(const string §ion, const string &name, TFlags flags=0) const
Get the parameter value.
virtual bool HasEntry(const string §ion, const string &name=kEmptyStr, TFlags flags=0) const
@ fCountCleared
Let explicitly cleared entries stand.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
static bool StringToBool(const CTempString str)
Convert string to bool.
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
static double StringToDouble(const CTempStringEx str, TStringToNumFlags flags=0)
Convert string to double.
static TNumeric StringToNumeric(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to a numeric value.
static const string BoolToString(bool value)
Convert bool to string.
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Test for equality of a substring with another string.
@ eEncoding_ISO8859_1
Note: From the point of view of the C++.
#define DEFINE_STATIC_MUTEX(id)
Define static mutex and initialize it.
bool TryWait(unsigned int timeout_sec=0, unsigned int timeout_nsec=0)
Timed wait.
void Post(unsigned int count=1)
Increment the semaphore by "count".
CTime & Clear(void)
Make the time "empty",.
@ eCurrent
Use current time. See also CCurrentTime.
enum ENcbiOwnership EOwnership
Ownership relations between objects.
Definition of all error codes used in dbapi libraries (dbapi_driver.lib and others).
string ConvertN2A(Uint4 host)
static bool s_Matches(CConnection *conn, const string &pool_name, const string &server_name)
static void s_TransformLoginData(string &server_name, string &user_name, string &db_name, string &password)
double value_type
The numeric datatype used by the parser.
const struct ncbi::grid::netcache::search::fields::KEY key
CRef< CDBServer > TSvrRef
const char * NCBI_GetDefaultSybasePath(void)
Get default Sybase client installation path.
const char * NCBI_GetSybasePath(void)
Get Sybase client installation path.
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
Int4 delta(size_t dimension_, const Int4 *score_)
Defines NCBI C++ secure resources API.
bool IsPoolIdleTimeSet(void)
string log_minor_messages
bool IsPoolMinSizeSet(void)
bool IsSingleServerSet(void)
bool IsPoolWaitTimeSet(void)
bool IsPoolMaxConnUseSet(void)
bool IsIOTimeoutSet(void)
bool IsPoolMaxSizeSet(void)
bool IsLogMinorMessagesSet(void)
string pool_allow_temp_overflow
bool IsContinueAfterRaiserrorSet(void)
bool IsPoolAllowTempOverflowSet(void)
bool IsCancelTimeoutSet(void)
string continue_after_raiserror
bool IsLoginTimeoutSet(void)
bool operator<(const SLoginData &right) const
SLoginData(const string &sn, const string &un, const string &dn, const string &pass)