Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. odbcpool.cxx
  5. Abstract:
  6. Provides simple ODBC connection pooling for IDC. The only keys
  7. used for the connection pooling is the datasource name, the
  8. username and password. ODBC options and other connection state
  9. are not taken into consideration.
  10. Author:
  11. John Ludeman (johnl) 01-Apr-1996
  12. Revision History:
  13. --*/
  14. #ifndef _ODBCPOOL_HXX_
  15. #define _ODBCPOOL_HXX_
  16. //
  17. // Globals
  18. //
  19. extern CRITICAL_SECTION g_csPoolLock;
  20. extern LIST_ENTRY g_PoolList;
  21. extern DWORD g_dwTimeoutID;
  22. //
  23. // Various counters
  24. //
  25. extern DWORD g_cFree;
  26. extern DWORD g_cUsed;
  27. //
  28. // ODBC Connection pool item
  29. //
  30. #define ODBC_CONN_POOL_SIGNATURE 'PCDO'
  31. #define ODBC_CONN_POOL_FREE_SIGNATURE 'fCDO'
  32. class ODBC_CONN_POOL
  33. {
  34. public:
  35. ODBC_CONN_POOL()
  36. : m_dwSignature ( ODBC_CONN_POOL_SIGNATURE ),
  37. m_fFree ( TRUE ),
  38. m_TTL ( 2 )
  39. { }
  40. ~ODBC_CONN_POOL()
  41. {
  42. m_odbcconn.Close();
  43. m_dwSignature = ODBC_CONN_POOL_FREE_SIGNATURE;
  44. }
  45. BOOL
  46. CheckSignature(
  47. VOID
  48. ) const
  49. {
  50. return m_dwSignature == ODBC_CONN_POOL_SIGNATURE;
  51. }
  52. HRESULT
  53. Create(
  54. const CHAR * pszDataSource,
  55. const CHAR * pszUsername,
  56. const CHAR * pszPassword,
  57. const CHAR * pszLoggedOnUser
  58. )
  59. {
  60. HRESULT hr;
  61. DBG_ASSERT( CheckSignature() );
  62. hr = m_strDataSource.Copy( pszDataSource );
  63. if( FAILED( hr ) )
  64. {
  65. DBGPRINTF(( DBG_CONTEXT,
  66. "Error copying data source, hr = 0x%x.\n",
  67. hr ));
  68. return hr;
  69. }
  70. hr = m_strUsername.Copy( pszUsername );
  71. if( FAILED( hr ) )
  72. {
  73. DBGPRINTF(( DBG_CONTEXT,
  74. "Error copying user name, hr = 0x%x.\n",
  75. hr ));
  76. return hr;
  77. }
  78. hr = m_strPassword.Copy( pszPassword );
  79. if( FAILED( hr ) )
  80. {
  81. DBGPRINTF(( DBG_CONTEXT,
  82. "Error copying password, hr = 0x%x.\n",
  83. hr ));
  84. return hr;
  85. }
  86. return m_strLogon.Copy( pszLoggedOnUser );
  87. }
  88. HRESULT
  89. Open(
  90. VOID
  91. )
  92. {
  93. DBG_ASSERT( CheckSignature() );
  94. return m_odbcconn.Open( m_strDataSource.QueryStr(),
  95. m_strUsername.QueryStr(),
  96. m_strPassword.QueryStr() );
  97. }
  98. BOOL
  99. IsValid(
  100. VOID
  101. ) const
  102. {
  103. return m_strDataSource.IsValid() &&
  104. m_strUsername.IsValid() &&
  105. m_strPassword.IsValid() &&
  106. m_strLogon.IsValid();
  107. }
  108. BOOL
  109. IsFree(
  110. VOID
  111. ) const
  112. {
  113. return m_fFree;
  114. }
  115. VOID
  116. MarkAsUsed(
  117. VOID
  118. )
  119. {
  120. m_fFree = FALSE;
  121. g_cFree--;
  122. g_cUsed++;
  123. }
  124. VOID
  125. MarkAsFree(
  126. VOID
  127. )
  128. {
  129. m_fFree = TRUE;
  130. g_cUsed--;
  131. g_cFree++;
  132. }
  133. const
  134. CHAR *
  135. QueryDataSource(
  136. VOID
  137. ) const
  138. {
  139. return m_strDataSource.QueryStr();
  140. }
  141. const
  142. CHAR *
  143. QueryUsername(
  144. VOID
  145. ) const
  146. {
  147. return m_strUsername.QueryStr();
  148. }
  149. const
  150. CHAR *
  151. QueryPassword(
  152. VOID
  153. ) const
  154. {
  155. return m_strPassword.QueryStr();
  156. }
  157. const
  158. CHAR *
  159. QueryLoggedOnUser(
  160. VOID
  161. ) const
  162. {
  163. return m_strLogon.QueryStr();
  164. }
  165. ODBC_CONNECTION *
  166. QueryOdbcConnection(
  167. VOID
  168. )
  169. {
  170. return &m_odbcconn;
  171. }
  172. DWORD
  173. DecrementTTL(
  174. VOID
  175. )
  176. {
  177. DBG_ASSERT( CheckSignature() );
  178. if ( m_TTL < IDC_POOL_TIMEOUT )
  179. {
  180. return 0;
  181. }
  182. m_TTL -= IDC_POOL_TIMEOUT;
  183. return m_TTL;
  184. }
  185. VOID
  186. SetTTL(
  187. DWORD csecTimeout
  188. )
  189. {
  190. m_TTL = csecTimeout;
  191. }
  192. VOID *
  193. operator new(
  194. size_t size
  195. )
  196. {
  197. DBG_ASSERT( size == sizeof( ODBC_CONN_POOL ) );
  198. DBG_ASSERT( sm_pachOdbcConnPools != NULL );
  199. return sm_pachOdbcConnPools->Alloc();
  200. }
  201. VOID
  202. operator delete(
  203. VOID * pOdbcConnPool
  204. )
  205. {
  206. DBG_ASSERT( pOdbcConnPool != NULL );
  207. DBG_ASSERT( sm_pachOdbcConnPools != NULL );
  208. DBG_REQUIRE( sm_pachOdbcConnPools->Free( pOdbcConnPool ) );
  209. }
  210. static
  211. HRESULT
  212. Initialize(
  213. VOID
  214. );
  215. static
  216. VOID
  217. Terminate(
  218. VOID
  219. );
  220. LIST_ENTRY m_ListEntry;
  221. private:
  222. //
  223. // Signature of the class
  224. //
  225. DWORD m_dwSignature;
  226. ODBC_CONNECTION m_odbcconn;
  227. STRA m_strDataSource;
  228. STRA m_strUsername;
  229. STRA m_strPassword;
  230. //
  231. // The NT account this request is using
  232. //
  233. STRA m_strLogon;
  234. BOOL m_fFree;
  235. DWORD m_TTL;
  236. //
  237. // Lookaside
  238. //
  239. static ALLOC_CACHE_HANDLER * sm_pachOdbcConnPools;
  240. };
  241. VOID
  242. WINAPI
  243. IDCPoolScavenger(
  244. PVOID pContext
  245. );
  246. #endif //_ODBCPOOL_HXX_