Oracle ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
connection-factory.hxx
Go to the documentation of this file.
1 // file : odb/oracle/connection-factory.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : ODB NCUEL; see accompanying LICENSE file
4 
5 #ifndef ODB_ORACLE_CONNECTION_FACTORY_HXX
6 #define ODB_ORACLE_CONNECTION_FACTORY_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <vector>
11 #include <cstddef> // std::size_t
12 #include <cassert>
13 
14 #include <odb/details/mutex.hxx>
15 #include <odb/details/condition.hxx>
16 #include <odb/details/shared-ptr.hxx>
17 
18 #include <odb/oracle/version.hxx>
19 #include <odb/oracle/forward.hxx>
21 
22 #include <odb/oracle/details/export.hxx>
23 
24 namespace odb
25 {
26  namespace oracle
27  {
28  class LIBODB_ORACLE_EXPORT connection_factory
29  {
30  public:
31  virtual connection_ptr
32  connect () = 0;
33 
34  public:
36 
37  virtual void
38  database (database_type&) = 0;
39 
40  virtual
42  };
43 
44  class LIBODB_ORACLE_EXPORT new_connection_factory: public connection_factory
45  {
46  public:
48  : db_ (0)
49  {
50  }
51 
52  virtual connection_ptr
53  connect ();
54 
55  virtual void
56  database (database_type&);
57 
58  private:
61 
62  private:
63  database_type* db_;
64  };
65 
66  class LIBODB_ORACLE_EXPORT connection_pool_factory:
67  public connection_factory
68  {
69  public:
70  // The max_connections argument specifies the maximum number of
71  // concurrent connections this pool will maintain. If this value
72  // is 0 then the pool will create a new connection every time all
73  // of the existing connections are in use.
74  //
75  // The min_connections argument specifies the minimum number of
76  // connections that should be maintained by the pool. If the
77  // number of connections maintained by the pool exceeds this
78  // number and there are no active waiters for a new connection,
79  // then the pool will release the excess connections. If this
80  // value is 0 then the pool will maintain all the connections
81  // that were ever created.
82  //
83  connection_pool_factory (std::size_t max_connections = 0,
84  std::size_t min_connections = 0)
85  : max_ (max_connections),
86  min_ (min_connections),
87  in_use_ (0),
88  waiters_ (0),
89  db_ (0),
90  cond_ (mutex_)
91  {
92  // max_connections == 0 means unlimited.
93  //
94  assert (max_connections == 0 || max_connections >= min_connections);
95  }
96 
97  virtual connection_ptr
98  connect ();
99 
100  virtual void
101  database (database_type&);
102 
103  virtual
105 
106  private:
109 
110  protected:
112  {
113  public:
116 
117  private:
118  static bool
119  zero_counter (void*);
120 
121  private:
123 
124  shared_base::refcount_callback callback_;
125 
126  // NULL pool value indicates that the connection is not in use.
127  //
129  };
130 
131  friend class pooled_connection;
132 
133  typedef details::shared_ptr<pooled_connection> pooled_connection_ptr;
134  typedef std::vector<pooled_connection_ptr> connections;
135 
136  // This function is called whenever the pool needs to create a new
137  // connection.
138  //
139  virtual pooled_connection_ptr
140  create ();
141 
142  protected:
143  // Return true if the connection should be deleted, false otherwise.
144  //
145  bool
146  release (pooled_connection*);
147 
148  protected:
149  const std::size_t max_;
150  const std::size_t min_;
151 
152  std::size_t in_use_; // Number of connections currently in use.
153  std::size_t waiters_; // Number of threads waiting for a connection.
154 
155  database_type* db_;
157 
158  details::mutex mutex_;
159  details::condition cond_;
160  };
161  }
162 }
163 
164 #include <odb/post.hxx>
165 
166 #endif // ODB_ORACLE_CONNECTION_FACTORY_HXX
Definition: connection.hxx:35
connections connections_
Definition: connection-factory.hxx:156
details::shared_ptr< connection > connection_ptr
Definition: connection.hxx:32
Definition: connection-factory.hxx:44
details::condition cond_
Definition: connection-factory.hxx:159
connection_pool_factory(std::size_t max_connections=0, std::size_t min_connections=0)
Definition: connection-factory.hxx:83
std::size_t waiters_
Definition: connection-factory.hxx:153
struct OCISvcCtx OCISvcCtx
Definition: oracle-fwd.hxx:23
Definition: connection-factory.hxx:28
Definition: connection-factory.hxx:111
oracle::database database_type
Definition: connection-factory.hxx:35
Definition: database.hxx:35
std::size_t in_use_
Definition: connection-factory.hxx:152
database_type * db_
Definition: connection-factory.hxx:155
details::mutex mutex_
Definition: connection-factory.hxx:158
details::shared_ptr< pooled_connection > pooled_connection_ptr
Definition: connection-factory.hxx:133
const std::size_t min_
Definition: connection-factory.hxx:150
Definition: connection-factory.hxx:66
std::vector< pooled_connection_ptr > connections
Definition: connection-factory.hxx:134
new_connection_factory()
Definition: connection-factory.hxx:47
const std::size_t max_
Definition: connection-factory.hxx:149