Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:19:33

0001 #include "DBInterface.h"
0002 
0003 #include <sphenixodbc/ODBCInterface.h>
0004 
0005 #include <frog/FROG.h>
0006 
0007 #include <fun4all/Fun4AllReturnCodes.h>
0008 #include <fun4all/Fun4AllServer.h>
0009 
0010 #include <phool/phool.h>
0011 
0012 #include <odbc++/resultset.h>
0013 #include <odbc++/statement.h>  // for Statement
0014 
0015 #include <cassert>
0016 #include <chrono>
0017 #include <cmath>
0018 #include <iostream>
0019 #include <random>  // For retrying connections
0020 #include <string>
0021 #include <thread>
0022 
0023 DBInterface *DBInterface::__instance{nullptr};
0024 
0025 DBInterface *DBInterface::instance()
0026 {
0027   if (!__instance)
0028   {
0029     __instance = new DBInterface();
0030   }
0031   return __instance;
0032 }
0033 
0034 DBInterface::~DBInterface()
0035 {
0036   delete m_ODBC;
0037   __instance = nullptr;
0038   return;
0039 }
0040 
0041 DBInterface::DBInterface(const std::string &name)
0042   : SubsysReco(name)
0043   , m_ODBC(ODBCInterface::instance())
0044 {
0045   Fun4AllServer *se = Fun4AllServer::instance();
0046   se->addNewSubsystem(this);
0047 }
0048 
0049 int DBInterface::process_event(PHCompositeNode * /*topNode*/)
0050 {
0051   m_ODBC->Disconnect();
0052   return Fun4AllReturnCodes::EVENT_OK;
0053 }
0054 
0055 odbc::Connection *DBInterface::getDBConnection(const std::string &dbname)
0056 {
0057   return m_ODBC->getDBConnection(dbname);
0058 }
0059 
0060 odbc::Statement *DBInterface::getStatement(const std::string &dbname)
0061 {
0062   return m_ODBC->getStatement(dbname);
0063 }
0064 
0065 int DBInterface::End(PHCompositeNode * /*topNode*/)
0066 {
0067   if (Verbosity() > 0)
0068   {
0069     std::cout << "Number of connection attempts" << std::endl;
0070     for (auto const &iter : m_ODBC->getNumConnection())
0071     {
0072       std::cout << "db: " << iter.first << ", attempts: " << iter.second << std::endl;
0073     }
0074     std::cout << "Total time slept: " << m_ODBC->SleepMS() << " ms" << std::endl;
0075     std::cout << "Total number of connection re-tries: " << m_ODBC->ConnectionTries() << std::endl;
0076   }
0077   return Fun4AllReturnCodes::EVENT_OK;
0078 }
0079 
0080 void DBInterface::Print(const std::string & /*what*/) const
0081 {
0082   std::cout << "Currently open connections: " << m_ODBC->CurrentConnections() << std::endl;
0083   for (auto const &iter : m_ODBC->getOdbcConnectionMap())
0084   {
0085     std::cout << "currently connected to " << iter.first << std::endl;
0086   }
0087   if (m_ODBC->getNumConnection().empty())
0088   {
0089     std::cout << "No ODBC connections cached" << std::endl;
0090   }
0091   else
0092   {
0093     std::cout << "Odbc Connections Stats: " << std::endl;
0094     for (auto const &iter : m_ODBC->getNumConnection())
0095     {
0096       std::cout << "db: " << iter.first << ", opened: " << iter.second << std::endl;
0097     }
0098   }
0099   if (m_ODBC->getNumStatementUse().empty())
0100   {
0101     std::cout << "No odbc::Statements cached" << std::endl;
0102   }
0103   else
0104   {
0105     std::cout << "Odbc Statement use: " << std::endl;
0106     for (auto const &iter : m_ODBC->getNumStatementUse())
0107     {
0108       std::cout << "db: " << iter.first << ", Statement use: " << iter.second << std::endl;
0109     }
0110   }
0111   return;
0112 }
0113 
0114 std::string DBInterface::location(const std::string &logical_name)
0115 {
0116   if (!fr)
0117   {
0118     fr = new FROG();
0119     fr->AutoDisconnect(false);
0120   }
0121   std::string fullfile = fr->location(logical_name);
0122   return fullfile;
0123 }