NCBI C++ Toolkit Cross Reference

  C++/src/dbapi/driver/dblib/context.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
/* $Id: context.cpp 64931 2014-10-23 18:15:34Z ucko $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Vladimir Soussov * * File Description: Driver for DBLib server * */ #include <ncbi_pch.hpp> #include "dblib_utils.hpp" #include <corelib/ncbimtx.hpp> #include <corelib/plugin_manager_impl.hpp> #include <corelib/plugin_manager_store.hpp> #include <corelib/ncbi_safe_static.hpp> // DO NOT DELETE this include !!! #include <dbapi/driver/driver_mgr.hpp> #include <dbapi/driver/dblib/interfaces.hpp> #include <dbapi/driver/dblib/interfaces_p.hpp> #include <dbapi/driver/util/numeric_convert.hpp> #include <dbapi/error_codes.hpp> #if defined(NCBI_OS_MSWIN) # include <winsock2.h> # include "../ncbi_win_hook.hpp" #endif #include <algorithm> #include <stdio.h> #define NCBI_USE_ERRCODE_X Dbapi_Dblib_Context BEGIN_NCBI_SCOPE ///////////////////////////////////////////////////////////////////////////// // // CDblibContextRegistry (Singleton) // class NCBI_DBAPIDRIVER_DBLIB_EXPORT CDblibContextRegistry { public: static CDblibContextRegistry& Instance(void); void Add(CDBLibContext* ctx); void Remove(CDBLibContext* ctx); void ClearAll(void); static void StaticClearAll(void); bool ExitProcessIsPatched(void) const { return m_ExitProcessPatched; } private: CDblibContextRegistry(void); ~CDblibContextRegistry(void) throw(); mutable CMutex m_Mutex; vector<CDBLibContext*> m_Registry; bool m_ExitProcessPatched; friend class CSafeStatic_Allocator<CDblibContextRegistry>; }; ///////////////////////////////////////////////////////////////////////////// CDblibContextRegistry::CDblibContextRegistry(void) : m_ExitProcessPatched(false) { #if defined(NCBI_OS_MSWIN) && defined(NCBI_DLL_BUILD) try { m_ExitProcessPatched = NWinHook::COnExitProcess::Instance().Add(CDblibContextRegistry::StaticClearAll); } catch (const NWinHook::CWinHookException&) { // Just in case ... m_ExitProcessPatched = false; } #endif } CDblibContextRegistry::~CDblibContextRegistry(void) throw() { try { ClearAll(); } NCBI_CATCH_ALL_X( 1, NCBI_CURRENT_FUNCTION ) } CDblibContextRegistry& CDblibContextRegistry::Instance(void) { static CSafeStatic<CDblibContextRegistry> instance; return instance.Get(); } void CDblibContextRegistry::Add(CDBLibContext* ctx) { CMutexGuard mg(m_Mutex); vector<CDBLibContext*>::iterator it = find(m_Registry.begin(), m_Registry.end(), ctx); if (it == m_Registry.end()) { m_Registry.push_back(ctx); } } void CDblibContextRegistry::Remove(CDBLibContext* ctx) { CMutexGuard mg(m_Mutex); vector<CDBLibContext*>::iterator it = find(m_Registry.begin(), m_Registry.end(), ctx); if (it != m_Registry.end()) { m_Registry.erase(it); ctx->x_SetRegistry(NULL); } } void CDblibContextRegistry::ClearAll(void) { if (!m_Registry.empty()) { CMutexGuard mg(m_Mutex); while ( !m_Registry.empty() ) { // x_Close will unregister and remove handler from the registry. m_Registry.back()->x_Close(false); } } } void CDblibContextRegistry::StaticClearAll(void) { CDblibContextRegistry::Instance().ClearAll(); } ///////////////////////////////////////////////////////////////////////////// // // CDBLibContext:: // extern "C" { static int CS_PUBLIC s_DBLIB_err_callback (DBPROCESS* dblink, int severity, int dberr, int oserr, char* dberrstr, char* oserrstr) { return CDBLibContext::DBLIB_dberr_handler (dblink, severity, dberr, oserr, dberrstr? dberrstr : "", oserrstr? oserrstr : ""); } static int CS_PUBLIC s_DBLIB_msg_callback (DBPROCESS* dblink, DBINT msgno, int msgstate, int severity, char* msgtxt, char* srvname, char* procname, int line) { CDBLibContext::DBLIB_dbmsg_handler (dblink, msgno, msgstate, severity, msgtxt? msgtxt : "", srvname? srvname : "", procname? procname : "", line); return 0; } } DEFINE_STATIC_MUTEX(s_CtxMutex); static CDBLibContext* g_pContext = NULL; CDBLibContext::CDBLibContext(DBINT version) : m_PacketSize( 0 ) , m_TDSVersion( version ) , m_BufferSize(1) { SetApplicationName("DBLibDriver"); CMutexGuard mg(s_CtxMutex); CHECK_DRIVER_ERROR( g_pContext != NULL, "You cannot use more than one dblib context " "concurrently", 200000 ); ResetEnvSybase(); char hostname[256]; if(gethostname(hostname, 256) == 0) { hostname[255] = '\0'; SetHostName( hostname ); } if (Check(dbinit()) != SUCCEED) { DATABASE_DRIVER_ERROR( "dbinit failed", 200001 ); } if (Check(dbsetversion(version)) != SUCCEED) { DATABASE_DRIVER_ERROR( "dbsetversion failed", 200001 ); } Check(dberrhandle(s_DBLIB_err_callback)); Check(dbmsghandle(s_DBLIB_msg_callback)); g_pContext = this; m_Registry = &CDblibContextRegistry::Instance(); x_AddToRegistry(); } void CDBLibContext::x_AddToRegistry(void) { if (m_Registry) { m_Registry->Add(this); } } void CDBLibContext::x_RemoveFromRegistry(void) { if (m_Registry) { m_Registry->Remove(this); } } void CDBLibContext::x_SetRegistry(CDblibContextRegistry* registry) { m_Registry = registry; } int CDBLibContext::GetTDSVersion(void) const { return m_TDSVersion; } bool CDBLibContext::SetLoginTimeout(unsigned int nof_secs) { if (impl::CDriverContext::SetLoginTimeout(nof_secs)) { CMutexGuard ctx_mg(s_CtxMutex); return Check(dbsetlogintime(GetLoginTimeout())) == SUCCEED; } return false; } bool CDBLibContext::SetTimeout(unsigned int nof_secs) { if (impl::CDriverContext::SetTimeout(nof_secs)) { CMutexGuard ctx_mg(s_CtxMutex); return Check(dbsettime(GetTimeout())) == SUCCEED; } return false; } bool CDBLibContext::SetMaxTextImageSize(size_t nof_bytes) { impl::CDriverContext::SetMaxTextImageSize(nof_bytes); char s[64]; sprintf(s, "%lu", (unsigned long) GetMaxTextImageSize()); CMutexGuard ctx_mg(s_CtxMutex); return Check(dbsetopt(0, DBTEXTLIMIT, s, -1)) == SUCCEED; } impl::CConnection* CDBLibContext::MakeIConnection(const CDBConnParams& params) { return new CDBL_Connection(*this, params); } SSystemMutex& CDBLibContext::x_GetCtxMtx(void) const { return s_CtxMutex; } bool CDBLibContext::IsAbleTo(ECapability cpb) const { switch(cpb) { case eBcp: case eReturnITDescriptors: case eReturnComputeResults: return true; default: break; } return false; } string CDBLibContext::GetDriverName(void) const { return "dblib"; } CDBLibContext::~CDBLibContext() { try { x_Close(); } NCBI_CATCH_ALL_X( 2, NCBI_CURRENT_FUNCTION ) } void CDBLibContext::x_Close(bool delete_conn) { if (g_pContext) { CMutexGuard ctx_mg(s_CtxMutex); if (g_pContext) { // Unregister itself before any other poeration // for sake of exception safety. g_pContext = NULL; x_RemoveFromRegistry(); if (x_SafeToFinalize()) { // close all connections first try { if (delete_conn) { DeleteAllConn(); } else { CloseAllConn(); } } NCBI_CATCH_ALL_X( 3, NCBI_CURRENT_FUNCTION ) try { // This function can fail if we try to connect to MS SQL server // using Sybase client. dbexit(); CheckFunctCall(); } NCBI_CATCH_ALL_X( 4, "dbexit() call failed. This usually happens when " "Sybase client has been used to connect to a MS SQL Server." ) } } } else { if (delete_conn && x_SafeToFinalize()) { DeleteAllConn(); } } } bool CDBLibContext::x_SafeToFinalize(void) const { if (m_Registry) { #if defined(NCBI_OS_MSWIN) return m_Registry->ExitProcessIsPatched(); #endif } return true; } void CDBLibContext::DBLIB_SetApplicationName(const string& app_name) { SetApplicationName( app_name ); } void CDBLibContext::DBLIB_SetHostName(const string& host_name) { SetHostName( host_name ); } void CDBLibContext::DBLIB_SetPacketSize(int p_size) { m_PacketSize = (short) p_size; } bool CDBLibContext::DBLIB_SetMaxNofConns(int n) { CMutexGuard ctx_mg(s_CtxMutex); return Check(dbsetmaxprocs(n)) == SUCCEED; } static void s_PassException(CDB_Exception& ex, const string& server_name, const string& user_name, CS_INT severity, const CDBParams* params) { ex.SetServerName(server_name); ex.SetUserName(user_name); ex.SetSybaseSeverity(severity); ex.SetParams(params); GetDBLExceptionStorage().Accept(ex); } int CDBLibContext::DBLIB_dberr_handler(DBPROCESS* dblink, int severity, int dberr, int /*oserr*/, const string& dberrstr, const string& /*oserrstr*/) { string server_name; string user_name; const CDBParams* params = NULL; CDB_Exception::SMessageInContext message = dberrstr; CMutexGuard ctx_mg(s_CtxMutex); CDBL_Connection* link = dblink ? reinterpret_cast<CDBL_Connection*> (dbgetuserdata(dblink)) : 0; if ( link ) { server_name = link->ServerName(); user_name = link->UserName(); message.context.Reset(&link->GetDbgInfo()); params = link->GetLastParams(); } switch (dberr) { case SYBETIME: case SYBEFCON: case SYBECONN: { CDB_TimeoutEx ex(DIAG_COMPILE_INFO, 0, message, dberr); s_PassException(ex, server_name, user_name, severity, params); } return INT_TIMEOUT; default: if(dberr == 1205) { CDB_DeadlockEx ex(DIAG_COMPILE_INFO, 0, message); s_PassException(ex, server_name, user_name, severity, params); return INT_CANCEL; } break; } switch (severity) { case EXINFO: case EXUSER: { CDB_ClientEx ex(DIAG_COMPILE_INFO, 0, message, eDiag_Info, dberr); s_PassException(ex, server_name, user_name, severity, params); } break; case EXNONFATAL: case EXCONVERSION: case EXSERVER: case EXPROGRAM: if ( dberr != 20018 ) { CDB_ClientEx ex(DIAG_COMPILE_INFO, 0, message, eDiag_Error, dberr); s_PassException(ex, server_name, user_name, severity, params); } break; case EXTIME: { CDB_TimeoutEx ex(DIAG_COMPILE_INFO, 0, message, dberr); s_PassException(ex, server_name, user_name, severity, params); } return INT_TIMEOUT; default: { CDB_ClientEx ex(DIAG_COMPILE_INFO, 0, message, eDiag_Critical, dberr); s_PassException(ex, server_name, user_name, severity, params); } break; } return INT_CANCEL; } void CDBLibContext::DBLIB_dbmsg_handler(DBPROCESS* dblink, DBINT msgno, int /*msgstate*/, int severity, const string& msgtxt, const string& srvname, const string& procname, int line) { string server_name; string user_name; const CDBParams* params = NULL; if (msgno == 5701 || msgno == 5703 || msgno == 5704) return; CDB_Exception::SMessageInContext message = msgtxt; CMutexGuard ctx_mg(s_CtxMutex); CDBL_Connection* link = dblink ? reinterpret_cast<CDBL_Connection*>(dbgetuserdata(dblink)) : 0; if ( link ) { server_name = link->ServerName(); user_name = link->UserName(); message.context.Reset(&link->GetDbgInfo()); params = link->GetLastParams(); } else { server_name = srvname; } if (msgno == 1205/*DEADLOCK*/) { CDB_DeadlockEx ex(DIAG_COMPILE_INFO, 0, message); s_PassException(ex, server_name, user_name, severity, params); } else if (msgno == 1708 || msgno == 1771) { // "Maximum row size exceeds allowable width. It is being rounded down to 32767 bytes." // "Row size (32767 bytes) could exceed row size limit, which is 1962 bytes." ERR_POST_X(6, Warning << message); } else { EDiagSev sev = severity < 10 ? eDiag_Info : severity == 10 ? eDiag_Warning : severity < 16 ? eDiag_Error : eDiag_Critical; if (!procname.empty()) { CDB_RPCEx ex(DIAG_COMPILE_INFO, 0, message, sev, msgno, procname, line); s_PassException(ex, server_name, user_name, severity, params); } else { CDB_DSEx ex(DIAG_COMPILE_INFO, 0, message, sev, msgno); s_PassException(ex, server_name, user_name, severity, params); } } } void CDBLibContext::CheckFunctCall(void) { _ASSERT(GetExtraMsg().empty()); GetDBLExceptionStorage().Handle(GetCtxHandlerStack(), NULL); } /////////////////////////////////////////////////////////////////////// // Driver manager related functions // /////////////////////////////////////////////////////////////////////////////// class CDbapiDblibCF2 : public CSimpleClassFactoryImpl<I_DriverContext, CDBLibContext> { public: typedef CSimpleClassFactoryImpl<I_DriverContext, CDBLibContext> TParent; public: CDbapiDblibCF2(void); ~CDbapiDblibCF2(void); public: virtual TInterface* CreateInstance( const string& driver = kEmptyStr, CVersionInfo version = NCBI_INTERFACE_VERSION(I_DriverContext), const TPluginManagerParamTree* params = 0) const; }; CDbapiDblibCF2::CDbapiDblibCF2(void) : TParent( "dblib", 0 ) { return ; } CDbapiDblibCF2::~CDbapiDblibCF2(void) { return ; } CDbapiDblibCF2::TInterface* CDbapiDblibCF2::CreateInstance( const string& driver, CVersionInfo version, const TPluginManagerParamTree* params) const { auto_ptr<TImplementation> drv; if ( !driver.empty() && driver != m_DriverName ) { return 0; } if (version.Match(NCBI_INTERFACE_VERSION(I_DriverContext)) != CVersionInfo::eNonCompatible) { // Mandatory parameters .... DBINT db_version = DBVERSION_46; // Optional parameters ... CS_INT page_size = 0; string client_charset; if ( params != NULL ) { typedef TPluginManagerParamTree::TNodeList_CI TCIter; typedef TPluginManagerParamTree::TValueType TValue; // Get parameters ... TCIter cit = params->SubNodeBegin(); TCIter cend = params->SubNodeEnd(); for (; cit != cend; ++cit) { const TValue& v = (*cit)->GetValue(); if ( v.id == "reuse_context" ) { bool reuse_context = (v.value != "false"); CMutexGuard mg(s_CtxMutex); if ( reuse_context && g_pContext ) { return g_pContext; } } else if ( v.id == "version" ) { int value = NStr::StringToInt( v.value ); switch ( value ) { case 46 : db_version = DBVERSION_46; break; case 100 : db_version = DBVERSION_100; } } else if ( v.id == "packet" ) { page_size = NStr::StringToInt( v.value ); } else if ( v.id == "client_charset" ) { client_charset = v.value; } } } // Create a driver ... drv.reset(new CDBLibContext(db_version)); // Set parameters ... if ( page_size ) { drv->DBLIB_SetPacketSize( page_size ); } // Set client's charset ... if ( !client_charset.empty() ) { drv->SetClientCharset( client_charset.c_str() ); } } return drv.release(); } /////////////////////////////////////////////////////////////////////////////// void NCBI_EntryPoint_xdbapi_dblib( CPluginManager<I_DriverContext>::TDriverInfoList& info_list, CPluginManager<I_DriverContext>::EEntryPointRequest method) { CHostEntryPointImpl<CDbapiDblibCF2>::NCBI_EntryPointImpl( info_list, method ); } NCBI_DBAPIDRIVER_DBLIB_EXPORT void DBAPI_RegisterDriver_DBLIB(void) { RegisterEntryPoint<I_DriverContext>( NCBI_EntryPoint_xdbapi_dblib ); } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.