C++DB is the database layer that was designed to work with C++CMS. This customized version is used within Ye Ol' Pi Shack.
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.
 
 
 
 
 

117 lines
3.0 KiB

  1. /*! \page pool Using Connection Pool
  2. \section pool_using Using Connection Pooling
  3. CppDB provides two ways to use connection pooling:
  4. -# Transparent - by setting a connection string option "@pool_size=N" where N is positive integer.
  5. -# Direct - by using cppdb::pool object directly.
  6. The first is the most simple case, it works fully transparently and only thing that should
  7. be changed is a connection string. Such that
  8. \code
  9. cppdb::session sql("sqlite3:db=test.db;@pool_size=10");
  10. \endcode
  11. Would automatically use connections pool and not require using any additional effort.
  12. On the other hand it has its own downsides - use of global singleton instance of the connections.
  13. It may give a chance that unrelated components of same software that use same connection
  14. string may interfere each other, set different connection options, etc.
  15. So libraries should prefer to use explicit pool object.
  16. \code
  17. // Create the pool
  18. cppdb::pool::pointer my_pool = cppdb::pool::create("sqlite3:db=test.db");
  19. // Use the pool
  20. cppdb::session sql(my_pool->open());
  21. // When sql is destroyed the
  22. // connection is returned to the pool
  23. \endcode
  24. This allows to use pool outside the global \ref cppdb::connections_manager.
  25. \section pool_conn_opt Configuring a Connection
  26. It is useful to be able to setup some generic session options that are usually
  27. set only once and kept during all the session. It may be things like
  28. transaction isolation level, durability options and more.
  29. When you use connections pool it would be very useful to distinguish between
  30. the newly created connection and other, recycled from the pool.
  31. It can be done using \ref cppdb::session::once_called() member function
  32. or simply by using \ref cppdb::session::once() function with a callback
  33. that is promised to be called only once per new connection.
  34. For example:
  35. \code
  36. void config(cppdb::session &sql)
  37. {
  38. sql << "PRAGMA read_uncommited=1" << cppdb::exec;
  39. }
  40. ...
  41. cppdb::session sql(conn_str);
  42. sql.once(config); // called for new connections only
  43. sql << ... ;
  44. \endcode
  45. Please note that any function like object that receive as
  46. a parameter the sql sesion can be used as callback:
  47. \code
  48. struct config {
  49. int uncommited;
  50. void operator()(cppdb::session &sql) const // operator ()
  51. {
  52. sql << "PRAGMA read_uncommited=?" << uncommited << cppdb::exec;
  53. }
  54. config(int val ) : uncommited(val)
  55. {
  56. }
  57. };
  58. ...
  59. cppdb::session sql(conn_str);
  60. sql.once(config(commit_mode)); // called for new connections only
  61. sql << ... ;
  62. \endcode
  63. \section pool_conn_specific Connection Specific Data
  64. If more complex configuration of the session is required it is possible to
  65. associate any user object with a connection using \ref cppdb::connection_specific_data.
  66. User can derive his own classes from it and store it withing a connection object. For example
  67. \code
  68. struct my_data : public cppdb::connection_specific_data {
  69. int foo;
  70. std::string bar
  71. };
  72. ...
  73. my_data *p=sql.get_specific<my_data>();
  74. // check if it exists
  75. if(!p) {
  76. // if not create one and assign one
  77. p = new my_data();
  78. sql.reset_specific(p);
  79. }
  80. p->foo++;
  81. ...
  82. \endcode
  83. */