lyfztestodbc.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // lyfztestodbc.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "lyfztestodbc.h"
  5. #include <process.h>
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // 唯一的应用程序对象
  10. CWinApp theApp;
  11. using namespace std;
  12. volatile LONG nThreadCount = 0;
  13. unsigned __stdcall ThreadWork( LPVOID lpInput )
  14. //DWORD WINAPI ThreadWork( LPVOID lpInput )
  15. {
  16. IODBCPoolInterface *pInstance = (IODBCPoolInterface *)lpInput;
  17. DWORD dwThreadId = GetCurrentThreadId();
  18. CArray<CStringArray,CStringArray> Ary;
  19. //pInstance->GetOrderInfo( &Ary , 5000);
  20. pInstance->GetEnterprisInfo(_T("多多福"), Ary);
  21. //Sleep(10);
  22. InterlockedIncrement(&nThreadCount);
  23. // printf("Thread Finish Count = %d\n", nThreadCount);
  24. printf("log[%d]:\t %d\n",dwThreadId,nThreadCount);
  25. //
  26. for ( int i = 0; i < Ary.GetSize(); i++ )
  27. {
  28. CStringArray &strAry = Ary.ElementAt(i);
  29. for ( int n = 0; n < strAry.GetSize(); n++ )
  30. {
  31. printf("log[%d]: %s\n",dwThreadId, strAry.ElementAt(n));
  32. }
  33. }
  34. return 0;
  35. }
  36. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  37. {
  38. int nRetCode = 0;
  39. // 初始化 MFC 并在失败时显示错误
  40. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  41. {
  42. // TODO: 更改错误代码以符合您的需要
  43. _tprintf(_T("错误: MFC 初始化失败\n"));
  44. nRetCode = 1;
  45. }
  46. else
  47. {
  48. // TODO: 在此处为应用程序的行为编写代码。
  49. //////////////////////////////////////////////////////////////////////////
  50. // 测试用例;
  51. IODBCPoolInterface *pInstance = lyfzCreatePoolInstance();
  52. if ( pInstance == NULL )
  53. {
  54. printf("log:打开数据库连接池失败\n");
  55. return -1;
  56. }
  57. /*
  58. DWORD dwCount = pInstance->InitializePool(_T("IT-SERVER\\SQLEXPRESS"),
  59. 0,
  60. _T("sa"),
  61. _T("ly1234"),
  62. _T("219"),
  63. 5,
  64. 20);
  65. */
  66. DWORD dwCount = pInstance->InitializePool(_T("192.168.1.223"),
  67. 1011,
  68. _T("sa"),
  69. _T("ly1234"),
  70. _T("dbmsg"),
  71. 5,
  72. 20);
  73. if ( dwCount == 0 )
  74. {
  75. printf("log:创建连接对象0个,退出\n");
  76. return -1;
  77. }
  78. int ncount = 0;
  79. for ( int i = 0; i < 100; i++)
  80. {
  81. Sleep(50);// 注释这个,会内存泄漏;
  82. unsigned int nThreadID = 0;
  83. //HANDLE hThread = CreateThread(NULL, 0, ThreadWork, pInstance, 0, NULL);
  84. HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadWork, pInstance, 0, &nThreadID);
  85. if (hThread)
  86. {
  87. ncount++;
  88. CString strNum = _T("");
  89. strNum.Format(_T("----创建线程成功: %d--------\n"), ncount);
  90. OutputDebugString(strNum);
  91. CloseHandle(hThread);
  92. //_endthreadex(hThread);
  93. }
  94. }
  95. system("pause");
  96. printf("\t\t %d \n\n\n",nThreadCount);
  97. pInstance->Release(); // 必须保证线程全部执行完成后才可以结束!
  98. system("pause");
  99. }
  100. return nRetCode;
  101. }