Guest User

Untitled

a guest
Feb 8th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. class DbPool {
  2.  
  3. pqxx::result runQuery(const string& query) {
  4.  
  5. connection *conn = getCon();
  6. work trans(*conn);
  7. result res = trans.exec(query);
  8. trans.commit();
  9. releaseCon(conn);
  10.  
  11. return res;
  12. }
  13.  
  14. DbPool(uint32_t max_cons) {
  15.  
  16. for (uint32_t i = 0; i < max_cons; i++) {
  17.  
  18. connection* con = createCon();
  19. m_freeCons.push_back(shared_ptr < connection > (con));
  20. }
  21. }
  22.  
  23. connection * createCon() {
  24.  
  25. connection * conn =
  26. new connection(
  27. "user='ak' password='rootpassword' dbname='bips_office' hostaddr='127.0.0.1' port='5432'");
  28. return conn;
  29. }
  30.  
  31. void releaseCon(connection *con) {
  32.  
  33. m_freeCons.push_back(shared_ptr < connection > (con));
  34. }
  35.  
  36. connection* getCon() {
  37.  
  38. shared_ptr < connection > conn = *(m_freeCons.end() - 1);
  39. m_freeCons.pop_back();
  40. return conn.get();
  41. }
  42.  
  43. vector<shared_ptr<connection> > m_freeCons;
  44.  
  45. int main(int argc, char *argv[]) {
  46. DbPool *pool = new DbPool(5);
  47. result res = pool->runQuery("SELECT COUNT (*) from captures");
  48. return 0;
  49. }
  50.  
  51. connection * createCon(){
  52.  
  53. connection * conn = new connection("user='ak' password='rootpassword' dbname='bips_office' hostaddr='127.0.0.1' port='5432'");
  54. return conn;
  55. }
  56.  
  57. connection* con = createCon();
  58. m_freeCons.push_back(shared_ptr<connection>(con));
Add Comment
Please, Sign In to add comment