Concept A managed TSQLServer connection- dropped when idle. More...
#include <TDbiConnection.hxx>
Public Member Functions | |
TDbiConnection (const std::string &url="", const std::string &user="", const std::string &password="", int maxConnects=20) | |
virtual | ~TDbiConnection () |
const std::string & | GetDbName () const |
const std::string & | GetPassword () const |
const std::string & | GetUrl () const |
const std::string & | GetUser () const |
Bool_t | IsClosed () const |
Bool_t | IsTemporary () const |
Bool_t | TableExists (const std::string &tableName) const |
const TDbiExceptionLog & | GetExceptionLog () const |
void | ClearExceptionLog () |
Bool_t | PrintExceptionLog (Int_t level=3) const |
Print exceptions at level of above and return true if any. | |
void | RecordException () |
void | SetTableExists (const std::string &tableName="") |
void | Connect () |
Methods used when "borrowing" the server (use same as for Statement). | |
void | DisConnect () |
void | ConnectStatement () |
Increment number of statements relying on this connection. | |
void | DisConnectStatement () |
Decrement number of statements relying on this connection and close if idle. | |
void | SetPermanent (Bool_t permanent=true) |
Connection is permanent, don't close even when idle. | |
Bool_t | Close (Bool_t force=false) |
Bool_t | Open () |
TSQLServer * | GetServer () |
TSQLStatement * | CreatePreparedStatement (const std::string &sql) |
Private Member Functions | |
void | CloseIdleConnection () |
Private Attributes | |
std::string | fDbName |
Database Name. | |
std::string | fExistingTableList |
A comma separate list of existing tables each in single quotes : 'table1','table2',... | |
TUrl | fUrl |
TSQLServer URL. | |
std::string | fUser |
Username. | |
std::string | fPassword |
Password. | |
Bool_t | fUrlValidated |
True if URL works. | |
int | fMaxConnectionAttempts |
Maximum number of times to try making a connection. | |
Int_t | fNumConnectedStatements |
Number of connected statements. | |
Bool_t | fIsTemporary |
Connection closes after each I/O (no connections left). | |
TSQLServer * | fServer |
TSQLServer or 0 if closed. | |
TDbiExceptionLog | fExceptionLog |
Concept A managed TSQLServer connection- dropped when idle.
Purpose To minimise connections. Contact: A.Finch@lancaster.ac.uk
Definition at line 42 of file TDbiConnection.hxx.
TDbiConnection::TDbiConnection | ( | const std::string & | url = "" , |
|
const std::string & | user = "" , |
|||
const std::string & | password = "" , |
|||
int | maxConnects = 20 | |||
) |
Passed in url - address of sql server user - username to use password - password to use macConnects = maximum number of connections to attempt before giving up, default to 20 throws EBadConnection() if can not make connection
TDbiConnection::~TDbiConnection | ( | ) | [virtual] |
Definition at line 117 of file TDbiConnection.cxx.
References Close(), and SK_DBI_Trace.
00117 { 00118 // 00119 // 00120 // Purpose: Destructor 00121 00122 00123 SK_DBI_Trace( "Destroying TDbiConnection" << " "); 00124 this->Close(true); 00125 00126 }
void TDbiConnection::ClearExceptionLog | ( | ) | [inline] |
Definition at line 76 of file TDbiConnection.hxx.
References TDbiExceptionLog::Clear(), and fExceptionLog.
Referenced by Close(), and Open().
00076 { fExceptionLog.Clear(); }
Bool_t TDbiConnection::Close | ( | Bool_t | force = false |
) |
Purpose: Close server connection unless active (or always if forced) . Return: true if connection now closed.
Definition at line 134 of file TDbiConnection.cxx.
References ClearExceptionLog(), fNumConnectedStatements, fServer, GetUrl(), IsClosed(), SK_DBI_Debug, and SK_DBI_Info.
Referenced by CloseIdleConnection(), and ~TDbiConnection().
00134 { 00135 00136 this->ClearExceptionLog(); 00137 if ( this->IsClosed() ) return true; 00138 00139 if ( fNumConnectedStatements ) { 00140 if ( ! force ) { 00141 SK_DBI_Info( "Unable to close connection: " << this->GetUrl() 00142 << "; it still has " 00143 << fNumConnectedStatements << "active statements. " << " "); 00144 return false; 00145 } 00146 SK_DBI_Info( "Closing connection: " << this->GetUrl() 00147 << "; even though it still has " 00148 << fNumConnectedStatements << " active statements. " << " "); 00149 } 00150 00151 delete fServer; 00152 fServer = 0; 00153 SK_DBI_Debug( "Closed connection: " << this->GetUrl() << " "); 00154 return true; 00155 00156 }
void TDbiConnection::CloseIdleConnection | ( | ) | [private] |
Purpose: Close idle connection. Idle means there are no active connections to this database.
Definition at line 163 of file TDbiConnection.cxx.
References Close(), fIsTemporary, and fNumConnectedStatements.
Referenced by DisConnectStatement().
00163 { 00164 00165 if ( fIsTemporary && fNumConnectedStatements == 0 ) this->Close(); 00166 00167 }
void TDbiConnection::Connect | ( | ) | [inline] |
Methods used when "borrowing" the server (use same as for Statement).
Definition at line 92 of file TDbiConnection.hxx.
References ConnectStatement().
Referenced by Open(), and TDbiDBProxy::StoreMetaData().
00092 { this->ConnectStatement(); }
void TDbiConnection::ConnectStatement | ( | ) | [inline] |
Increment number of statements relying on this connection.
Definition at line 96 of file TDbiConnection.hxx.
References fNumConnectedStatements.
Referenced by Connect().
00096 { ++fNumConnectedStatements; }
TSQLStatement * TDbiConnection::CreatePreparedStatement | ( | const std::string & | sql | ) |
Get statement, opening if necessary. Caller must take ownership.
Purpose: Open if necessary and get a prepared statment. Return: Statement - Caller must take ownership. will be 0 if failure.
Definition at line 178 of file TDbiConnection.cxx.
References TDbiExceptionLog::AddEntry(), fExceptionLog, fServer, and Open().
Referenced by TDbiStatement::CreateProcessedStatement().
00178 { 00179 00180 TSQLStatement* stmt = 0; 00181 if ( ! this->Open() ) return stmt; 00182 stmt = fServer->Statement(sql.c_str()); 00183 if ( ! stmt ) { 00184 fExceptionLog.AddEntry(*fServer); 00185 } 00186 else stmt->EnableErrorOutput(false); 00187 00188 return stmt; 00189 }
void TDbiConnection::DisConnect | ( | ) | [inline] |
Definition at line 93 of file TDbiConnection.hxx.
References DisConnectStatement().
Referenced by TDbiDBProxy::StoreMetaData().
00093 { this->DisConnectStatement(); }
void TDbiConnection::DisConnectStatement | ( | ) | [inline] |
Decrement number of statements relying on this connection and close if idle.
Definition at line 98 of file TDbiConnection.hxx.
References CloseIdleConnection(), and fNumConnectedStatements.
Referenced by DisConnect(), and TDbiStatement::~TDbiStatement().
00098 { 00099 --fNumConnectedStatements; 00100 if ( ! fNumConnectedStatements ) this->CloseIdleConnection(); }
const std::string& TDbiConnection::GetDbName | ( | ) | const [inline] |
Definition at line 65 of file TDbiConnection.hxx.
References fDbName.
Referenced by TDbiStatement::ExecuteQuery(), and TDbiStatement::ExecuteUpdate().
00065 { return fDbName; }
const TDbiExceptionLog& TDbiConnection::GetExceptionLog | ( | ) | const [inline] |
Definition at line 75 of file TDbiConnection.hxx.
References fExceptionLog.
Referenced by TDbiStatement::AppendExceptionLog().
00075 { return fExceptionLog; }
const std::string& TDbiConnection::GetPassword | ( | ) | const [inline] |
TSQLServer * TDbiConnection::GetServer | ( | ) |
Get server, opening if necessary TDbiConnection retains ownership
Purpose: Open if necessary and get a TSQLServer. Return: Server ( = 0 if connection not open). WARNING: The server returned remains is being borrowed from the TDbiConnection and remains under its ownership and must not be deleted. However the caller must invoke the Connect() method on this TDbiConnection before borrowing it and must invoke the DisConnect() when it has finished using it to ensure the TDbiConnection does not close it prematurely i.e.:- void Demo(TDbiConnection* con) { con->Connect(); TSQLServer* server = con->GetServer(); // Do stuff con->DisConnect(); }
Definition at line 212 of file TDbiConnection.cxx.
References fServer, and Open().
Referenced by TDbiStatement::ExecuteUpdate(), and TDbiDBProxy::StoreMetaData().
const std::string & TDbiConnection::GetUrl | ( | ) | const |
Don't ask me why TUrl::GetUrl() is non-const, just accept that it is! Note: This function returns a reference to a shared string; use the value or make a copy of it before any subsequent call to this function.
Definition at line 227 of file TDbiConnection.cxx.
References fUrl.
Referenced by Close(), and TDbiCascader::CreateTemporaryTable().
00227 { 00228 00229 00230 00231 static std::string url; 00232 url = const_cast<TDbiConnection*>(this)->fUrl.GetUrl(); 00233 return url; 00234 00235 }
const std::string& TDbiConnection::GetUser | ( | ) | const [inline] |
Bool_t TDbiConnection::IsClosed | ( | ) | const [inline] |
Bool_t TDbiConnection::IsTemporary | ( | ) | const [inline] |
Definition at line 70 of file TDbiConnection.hxx.
References fIsTemporary.
Referenced by TDbiCascader::CreateTemporaryTable().
00070 { return fIsTemporary; }
Bool_t TDbiConnection::Open | ( | ) |
Purpose: Open connection if necessary.
Definition at line 242 of file TDbiConnection.cxx.
References TDbiExceptionLog::AddEntry(), TDbiServices::AsciiDBConectionsTemporary(), ClearExceptionLog(), Connect(), fExceptionLog, fIsTemporary, fMaxConnectionAttempts, fPassword, fServer, fUrl, fUrlValidated, fUser, TDbiAsciiDbImporter::GetExceptionLog(), TDbiAsciiDbImporter::GetImportedTableNames(), IsClosed(), SetTableExists(), SK_DBI_Debug, SK_DBI_Log, SK_DBI_Severe, and SK_DBI_Warn.
Referenced by ClassImp(), CreatePreparedStatement(), and GetServer().
00242 { 00243 00244 00245 this->ClearExceptionLog(); 00246 if ( ! this->IsClosed() ) return true; 00247 00248 if ( ! fUrl.IsValid() ) { 00249 00250 ostringstream oss; 00251 oss << "Unable to open connection: URL '" << fUrl.GetUrl() << "' is invalid"; 00252 SK_DBI_Severe( oss.str() << " "); 00253 fExceptionLog.AddEntry(oss.str()); 00254 return false; 00255 } 00256 00257 // Make several attempts (or more if URL is known to be O.K.) to open connection. 00258 int maxAttempt = fUrlValidated ? 100: fMaxConnectionAttempts ; 00259 for (int attempt = 1; attempt <= maxAttempt; attempt++) { 00260 fServer = TSQLServer::Connect(fUrl.GetUrl(),fUser.c_str(),fPassword.c_str()); 00261 00262 if ( ! fServer ) { 00263 ostringstream oss; 00264 oss << "Failing to open: " << fUrl.GetUrl() << " for user " << fUser 00265 << " and password " << fPassword << " (attempt " << attempt << ")"; 00266 fExceptionLog.AddEntry(oss.str()); 00267 if ( fMaxConnectionAttempts > attempt ){ 00268 00269 if ( attempt == 1 ) { 00270 SK_DBI_Severe( " retrying ... " << " "); 00271 } 00272 SK_DBI_Log(" Waiting "<<attempt<<" seconds before trying again"); 00273 gSystem->Sleep(attempt*1000); 00274 } 00275 } 00276 00277 else { 00278 fServer->EnableErrorOutput(false); 00279 if ( attempt > 1 ) SK_DBI_Warn( "... Connection opened on attempt " << attempt << " "); 00280 SK_DBI_Debug( "Successfully opened connection to: " << fUrl.GetUrl() << " "); 00281 00282 // If this is an ASCII database, populate it and make the connection permanent 00283 // unless even ASCII DB connections are temporary. 00284 00285 TString ascii_file = fUrl.GetAnchor(); 00286 if ( ascii_file.IsNull() ) return true; 00287 gSystem->Setenv("DBI_CATALOGUE_PATH",gSystem->DirName(fUrl.GetAnchor())); 00288 TDbiAsciiDbImporter importer(ascii_file,fServer); 00289 const TDbiExceptionLog& el(importer.GetExceptionLog()); 00290 if ( ! el.IsEmpty() ) { 00291 SK_DBI_Severe( "Failed to populate ASCII database from " << fUrl.GetUrl() << "\n" 00292 << el << " "); 00293 delete fServer; 00294 fServer = 0; 00295 return false; 00296 } 00297 fIsTemporary = TDbiServices::AsciiDBConectionsTemporary(); 00298 // Add imported tables names. 00299 const std::list<std::string> tableNames(importer.GetImportedTableNames()); 00300 std::list<std::string>::const_iterator itr(tableNames.begin()), itrEnd(tableNames.end()); 00301 while ( itr != itrEnd ) { 00302 this->SetTableExists(*itr); 00303 ++itr; 00304 } 00305 return true; 00306 00307 } 00308 } 00309 SK_DBI_Severe( "... Failed to open a connection to: " << fUrl.GetUrl() 00310 << " for user " << fUser << " and pwd " << fPassword << " "); 00311 00312 return false; 00313 00314 }//
Bool_t TDbiConnection::PrintExceptionLog | ( | Int_t | level = 3 |
) | const |
Print exceptions at level of above and return true if any.
Purpose: Print all warning at supplied Msg log level. Return: kTRUE if warnings have occurred
Definition at line 325 of file TDbiConnection.cxx.
References fExceptionLog, and TDbiExceptionLog::Size().
00325 { 00326 00327 00328 return fExceptionLog.Size() != 0; 00329 00330 }
void TDbiConnection::RecordException | ( | ) |
Purpose: Record an exception that has occurred while a client was using its TSQLServer.
Definition at line 336 of file TDbiConnection.cxx.
References TDbiExceptionLog::AddEntry(), fExceptionLog, and fServer.
Referenced by TDbiStatement::ExecuteUpdate().
00336 { 00337 00338 fExceptionLog.AddEntry(*fServer); 00339 00340 }
void TDbiConnection::SetPermanent | ( | Bool_t | permanent = true |
) | [inline] |
Connection is permanent, don't close even when idle.
Definition at line 102 of file TDbiConnection.hxx.
References fIsTemporary.
Referenced by TDbiCascader::CreateTemporaryTable().
00102 { fIsTemporary = ! permanent; }
void TDbiConnection::SetTableExists | ( | const std::string & | tableName = "" |
) |
Add name to list of existing tables (necessary when creating tables) Default name = "", reread all tables from database.
Referenced by TDbiCascader::CreateTemporaryTable(), and Open().
Bool_t TDbiConnection::TableExists | ( | const std::string & | tableName | ) | const |
std::string TDbiConnection::fDbName [private] |
Log of exceptions generated. Cleared by Open Close and (implicitly) by CreatePreparedStatement, GetServer
Definition at line 155 of file TDbiConnection.hxx.
Referenced by ClearExceptionLog(), CreatePreparedStatement(), GetExceptionLog(), Open(), PrintExceptionLog(), and RecordException().
std::string TDbiConnection::fExistingTableList [private] |
A comma separate list of existing tables each in single quotes : 'table1','table2',...
Definition at line 127 of file TDbiConnection.hxx.
Bool_t TDbiConnection::fIsTemporary [private] |
Connection closes after each I/O (no connections left).
Definition at line 148 of file TDbiConnection.hxx.
Referenced by CloseIdleConnection(), IsTemporary(), Open(), and SetPermanent().
int TDbiConnection::fMaxConnectionAttempts [private] |
Maximum number of times to try making a connection.
Definition at line 142 of file TDbiConnection.hxx.
Referenced by Open().
Int_t TDbiConnection::fNumConnectedStatements [private] |
Number of connected statements.
Definition at line 145 of file TDbiConnection.hxx.
Referenced by Close(), CloseIdleConnection(), ConnectStatement(), and DisConnectStatement().
std::string TDbiConnection::fPassword [private] |
Password.
Definition at line 136 of file TDbiConnection.hxx.
Referenced by GetPassword(), and Open().
TSQLServer* TDbiConnection::fServer [private] |
TSQLServer or 0 if closed.
Definition at line 151 of file TDbiConnection.hxx.
Referenced by Close(), CreatePreparedStatement(), GetServer(), IsClosed(), Open(), and RecordException().
TUrl TDbiConnection::fUrl [private] |
TSQLServer URL.
Definition at line 130 of file TDbiConnection.hxx.
Bool_t TDbiConnection::fUrlValidated [private] |
std::string TDbiConnection::fUser [private] |