fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. class DBConn {
  8. private:
  9. int id;
  10. DBConn(int conn_id) : id(conn_id) {
  11. cout << "Connection " << id << " established.\n";
  12. }
  13. ~DBConn() {
  14. cout << "Connection " << id << " closed.\n";
  15. }
  16.  
  17. public:
  18. void query(const string& sql) {
  19. cout << "Executing on Connection " << id << ": " << sql << endl;
  20. }
  21. friend class ConnManager; // Only ConnManager can create/destroy
  22. };
  23.  
  24. class ConnManager {
  25. private:
  26. static vector<DBConn*> connections;
  27. static int connCounter;
  28. public:
  29. // Get a new database connection
  30. static DBConn* getConn() {
  31. DBConn* conn = new DBConn(++connCounter);
  32. connections.push_back(conn);
  33. return conn;
  34. }
  35. // Release a specific connection
  36. static void releaseConn(DBConn* conn) {
  37. auto it = find(connections.begin(), connections.end(), conn);
  38. if (it != connections.end()) {
  39. delete *it;
  40. connections.erase(it);
  41. cout << "Connection released.\n";
  42. }
  43. else
  44. cout << "Invalid connection!\n";
  45. }
  46. // Close all connections
  47. static void shutdownAll() {
  48. cout << "Shutting down all connections\n";
  49. for (DBConn* conn : connections) {
  50. delete conn;
  51. }
  52. connections.clear();
  53. cout << "All connections closed.\n";
  54. }
  55. };
  56. // Static variable initialization
  57. vector<DBConn*> ConnManager::connections;
  58. int ConnManager::connCounter = 0;
  59. // Driver Code
  60. int main() {
  61. cout << "\n--- Creating Database Connections ---" << endl;
  62. DBConn* db1 = ConnManager::getConn();
  63. DBConn* db2 = ConnManager::getConn();
  64. db1->query("SELECT * FROM users");
  65. db2->query("UPDATE users SET active = 1");
  66. cout << "\n--- Releasing One Connection ---" << endl;
  67. ConnManager::releaseConn(db1);
  68. cout << "\n--- Shutting Down All Connections ---" << endl;
  69. ConnManager::shutdownAll();
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0s 5296KB
stdin
100
88 102
372 461581
427480 427487
747466 846825
840 111021
325 257248
453 705313
218065 218074
512 880446
350 657250
248 480874
667832 884126
453 821647
102831 303586
777630 814525
7 526182
678383 678388
211166 410071
810178 810188
437 643143
220133 220141
7044 205276
263 606124
118 630123
21736 21746
136871 136875
235 284036
876 166267
410584 713330
256530 526265
254201 318223
8205 8211
367 403628
775 415545
587 333017
62171 286453
183011 183021
16 26
503 602407
361 873266
142 776661
848 760565
455 342446
546 351181
512 464303
26 243615
11 652217
272622 604005
38 547136
212455 618287
161155 161164
3673 3676
16 218776
363586 531764
104 143032
451 127680
543 400777
618646 618654
61325 110578
806447 806451
538 715082
114 154028
77776 77777
175 832667
60577 60581
35 656647
1 2
125035 125041
52676 81372
764 665688
510 748112
672637 672640
584 180368
175 734041
242 232618
411446 411447
1 1000000
157777 807542
164648 721412
647452 647456
236 667408
712816 785842
724 287221
230 135188
580 866011
2 3
586064 586066
433 625461
643 626773
743 768201
372 263516
628 347574
285 315232
523 751618
867 273842
274 518172
86113 86117
272 223700
340411 523315
213 813047
stdout
--- Creating Database Connections ---
Connection 1 established.
Connection 2 established.
Executing on Connection 1: SELECT * FROM users
Executing on Connection 2: UPDATE users SET active = 1

--- Releasing One Connection ---
Connection 1 closed.
Connection released.

--- Shutting Down All Connections ---
Shutting down all connections
Connection 2 closed.
All connections closed.