StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
DBSessionManager.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <string>
00007 #include <sstream>
00008 // SOCI
00009 #if defined(SOCI_HEADERS_BURIED)
00010 #include <soci/core/soci.h>
00011 #include <soci/backends/mysql/soci-mysql.h>
00012 #else // SOCI_HEADERS_BURIED
00013 #include <soci.h>
00014 #include <mysql/soci-mysql.h>
00015 #endif // SOCI_HEADERS_BURIED
00016 // StdAir
00017 #include <stdair/stdair_exceptions.hpp>
00018 #include <stdair/basic/BasDBParams.hpp>
00019 #include <stdair/service/DBSessionManager.hpp>
00020 #include <stdair/service/Logger.hpp>
00021 
00022 namespace stdair {
00023   
00024   // //////////////////////////////////////////////////////////////////////
00025   DBSessionManager::DBSessionManager () : _dbSession (NULL) {
00026   }
00027 
00028   // //////////////////////////////////////////////////////////////////////
00029   DBSessionManager::DBSessionManager (const DBSessionManager&)
00030     : _dbSession (NULL) {
00031     assert (false);
00032   }
00033 
00034   // //////////////////////////////////////////////////////////////////////
00035   DBSessionManager::~DBSessionManager () {
00036     // std::cout << "In DBSessionManager destructor" << std::endl;
00037     dbFinalise();
00038   }
00039 
00040   // //////////////////////////////////////////////////////////////////////
00041   void DBSessionManager::dbInit (const BasDBParams& iDBParams) {
00042 
00043     // Database parameters
00044     std::ostringstream oStr;
00045     oStr << "db=" << iDBParams.getDBName() << " user=" << iDBParams.getUser()
00046          << " password=" << iDBParams.getPassword()
00047          << " port=" << iDBParams.getPort() << " host=" << iDBParams.getHost();
00048     const std::string lDBSessionConnectionString (oStr.str());
00049 
00050     // Instanciate the database session: nothing else is performed at that stage
00051     _dbSession = new DBSession_T;
00052     
00053     try {
00054       // Open the connection to the database
00055       _dbSession->open (soci::mysql, lDBSessionConnectionString);
00056       
00057     } catch (std::exception const& lException) {
00058       std::ostringstream oMessage;
00059       oMessage <<"Error while opening a connection to database: "
00060                << lException.what() << std::endl
00061                << "Database parameters used:"
00062                << " db=" << iDBParams.getDBName()
00063                << " user=" << iDBParams.getUser()
00064                << " port=" << iDBParams.getPort()
00065                << " host=" << iDBParams.getHost();
00066       throw SQLDatabaseConnectionImpossibleException (oMessage.str());
00067     }
00068   }
00069     
00070   // //////////////////////////////////////////////////////////////////////
00071   void DBSessionManager::dbFinalise () {
00072     delete _dbSession; _dbSession = NULL;
00073   }
00074   
00075   // //////////////////////////////////////////////////////////////////////
00076   void DBSessionManager::init (const BasDBParams& iDBParams) {
00077     DBSessionManager& lInstance = instance();
00078     lInstance.dbInit (iDBParams);
00079   }
00080   
00081   // //////////////////////////////////////////////////////////////////////
00082   DBSessionManager& DBSessionManager::instance() {
00083     static DBSessionManager _instance;
00084     return _instance;
00085   }
00086   
00087   // //////////////////////////////////////////////////////////////////////
00088   void DBSessionManager::clean() {
00089   }
00090   
00091   // //////////////////////////////////////////////////////////////////////
00092   DBSession_T& DBSessionManager::getDBSession() const {
00093     if (_dbSession == NULL) {
00094       throw NonInitialisedDBSessionManagerException ("");
00095     }
00096     assert (_dbSession != NULL);
00097     return *_dbSession;
00098   }
00099   
00100 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines