1 |
// Connector |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <hop/bind.hpp> |
10 |
#include <xml/document.hpp> |
11 |
#include <xml/nodeset.hpp> |
12 |
#include <xml/parse.hpp> |
13 |
|
14 |
#include "Connector.hpp" |
15 |
|
16 |
Connector::Connector(const cse::String& configuration) : running(true), cleanup(hop::BindAll(&Connector::Cleanup, this)) |
17 |
{ |
18 |
_R<xml::Document> document(xml::Parse(configuration)); |
19 |
_R<xml::Node> configuration_(*document/"configuration"); |
20 |
|
21 |
driver = dbi::GetDriver(*configuration_/"driver"); |
22 |
host = *configuration_/"host"; |
23 |
user = *configuration_/"user"; |
24 |
password = *configuration_/"password"; |
25 |
database = *configuration_/"database"; |
26 |
} |
27 |
|
28 |
_R<dbi::Connection> Connector::Connect() |
29 |
{ |
30 |
_R<dbi::Connection> connection; |
31 |
|
32 |
_synchronized (connectionsLock) |
33 |
if (!connections.Pop(connection)) |
34 |
connection = driver->Connect(host, user, password, database); |
35 |
|
36 |
return connection; |
37 |
} |
38 |
|
39 |
void Connector::Release(const _R<dbi::Connection>& connection) |
40 |
{ |
41 |
_synchronized (connectionsLock) |
42 |
connections.Push(connection); |
43 |
} |
44 |
|
45 |
int Connector::Cleanup() |
46 |
{ |
47 |
while (running) |
48 |
{ |
49 |
::timespec sleep = { 60, 0 }, remaining; |
50 |
|
51 |
while (running && ::nanosleep(&sleep, &remaining) == -1) |
52 |
sleep = remaining; |
53 |
|
54 |
_synchronized (connectionsLock) |
55 |
connections.Clear(); |
56 |
} |
57 |
|
58 |
return 0; |
59 |
} |