52 lines
981 B
C++
52 lines
981 B
C++
#include "dbsession.hh"
|
|
#include <zdb.h>
|
|
#include <stdexcept>
|
|
|
|
namespace inventory {
|
|
namespace db {
|
|
|
|
DBSession::DBSession(const char *url)
|
|
: m_url(url), m_pool(NULL) {
|
|
}
|
|
|
|
DBSession::~DBSession() {
|
|
destroy_connpool();
|
|
}
|
|
|
|
void DBSession::reset(const char *url) {
|
|
destroy_connpool();
|
|
m_url = url;
|
|
create_connpool();
|
|
}
|
|
|
|
void DBSession::create_connpool() {
|
|
if (m_pool == NULL) {
|
|
URL_T url = URL_new(m_url.c_str());
|
|
m_pool = ConnectionPool_new(url);
|
|
ConnectionPool_start(m_pool);
|
|
}
|
|
}
|
|
|
|
void DBSession::destroy_connpool() {
|
|
if (m_pool == NULL)
|
|
return;
|
|
|
|
ConnectionPool_stop(m_pool);
|
|
ConnectionPool_free(&m_pool);
|
|
}
|
|
|
|
Connection_T DBSession::get_connection() {
|
|
create_connpool();
|
|
Connection_T conn = ConnectionPool_getConnection(m_pool);
|
|
if (conn == NULL)
|
|
throw std::runtime_error("Couldn't get db connection");
|
|
return conn;
|
|
}
|
|
|
|
void DBSession::return_connection(Connection_T conn) {
|
|
ConnectionPool_returnConnection(m_pool, conn);
|
|
}
|
|
|
|
}
|
|
}
|