Debian Codegraph @

docs/pool.txt

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