ATLAS Offline Software
Loading...
Searching...
No Matches
MicroSessionManager.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "DatabaseRegistry.h"
7#include "DatabaseHandler.h"
8
12#include "StorageSvc/DbOption.h"
13
14#include "GaudiKernel/StatusCode.h"
15#include <exception>
16
18 long technology ):
19 m_registry( registry ),
20 m_storageSvc( 0 ),
21 m_inSession( false ),
22 m_technology( technology ),
24{
25 m_storageSvc = createStorageSvc("StorageSvc");
26 if ( ! m_storageSvc ) {
27 throw std::runtime_error( "Could not create a StorageSvc object (APR: \" MicroSessionManager::MicroSessionManager \" from \" PersistencySvc \"" );
28 }
29}
30
31
37
38
39bool
40pool::MicroSessionManager::connect( Io::IoFlag mode, int ageLimit )
41{
42 if( !m_inSession ) {
43 m_inSession = m_storageSvc->startSession(mode, m_technology, ageLimit).isSuccess();
44 }
45 return m_inSession;
46}
47
48
51 const std::string& fid,
52 const std::string& pfn )
53{
54 if( mode == Io::INVALID ) return 0;
55 if( m_databaseHandlers.empty() ) {
56 if( !m_inSession ) {
57 if( !m_storageSvc->startSession(mode, m_technology).isSuccess() ) {
58 return nullptr;
59 }
60 m_inSession = true;
61 }
62 }
63
65 try {
68 fid,
69 pfn,
70 mode );
71 m_registry.registerDatabaseHandler( db );
72 m_databaseHandlers.insert( db );
73 } catch( const std::runtime_error& /* error */) {
74 delete db;
75 m_storageSvc->endSession().ignore();
76 m_inSession = false;
77 return nullptr;
78 }
79
80 if( m_databaseHandlers.empty() && m_inSession ) {
81 m_storageSvc->endSession().ignore();
82 m_inSession = false;
83 }
84 return db;
85}
86
87
88void
90{
91 std::set< pool::DatabaseHandler* >::iterator idb = m_databaseHandlers.find( database );
92 if ( idb != m_databaseHandlers.end() ) {
93 m_registry.deregisterDatabaseHandler( *idb );
94 delete *idb;
95 m_databaseHandlers.erase( idb );
96 }
97 if( m_databaseHandlers.empty() && m_inSession ) {
98 m_storageSvc->endSession().ignore();
99 m_inSession = false;
100 }
101}
102
103
104bool
106{
107 bool ret = true;
108 for ( std::set< pool::DatabaseHandler* >::iterator idb = m_databaseHandlers.begin();
109 idb != m_databaseHandlers.end(); ++idb ) {
110 m_registry.deregisterDatabaseHandler( *idb );
111 ret = (*idb)->disconnectTransaction();
112 delete *idb;
113 }
114 m_databaseHandlers.clear();
115
116 if( m_inSession ) {
117 ret = ret and m_storageSvc->endSession().isSuccess();
118 m_inSession = false;
119 }
120 return ret;
121}
122
123std::string
125{
126 if ( m_databaseHandlers.empty() ) {
127 Io::IoFlag mode = Io::READ;
128 if( !m_storageSvc->startSession(mode, m_technology).isSuccess() ) {
129 return "";
130 }
131 }
132
133 std::string fid = "";
134 pool::FileDescriptor fd( pfn, pfn );
135 // this is only a temporary FID so use a special pattern to make that clear
136 fd.setFID( fd.FID().substr(0,24) + "0FF0FF0FF0FF" );
137 if( m_storageSvc->existsConnection(fd).isSuccess() ) {
138 if ( m_storageSvc->connect(Io::READ, fd).isSuccess() ) {
139 DbDatabase dbH( fd.dbc()->handle() );
140 if ( ! dbH.param( "FID", fid ).isSuccess() ) fid = "";
141 m_storageSvc->disconnect( fd ).ignore();
142 }
143 }
144
145 if( m_databaseHandlers.empty() ) {
146 m_storageSvc->endSession().ignore();
147 m_inSession = false;
148 }
149
150 return fid;
151}
152
153bool
154pool::MicroSessionManager::attributeOfType( const std::string& attributeName,
155 void* data,
156 const std::type_info& typeInfo,
157 const std::string& option )
158{
159 if( !m_inSession ) {
160 return false;
161 }
162 pool::DbOption domainOption( attributeName, option );
163 if( !m_storageSvc->getDomainOption(domainOption).isSuccess() ) return false;
164 return domainOption.i_getValue(typeInfo, data).isSuccess();
165}
166
167bool
168pool::MicroSessionManager::setAttributeOfType( const std::string& attributeName,
169 const void* data,
170 const std::type_info& typeInfo,
171 const std::string& option )
172{
173 if( !m_inSession ) {
174 return false;
175 }
176 pool::DbOption domainOption( attributeName, option );
177 if( !domainOption.i_setValue( typeInfo, const_cast<void*>( data ) ).isSuccess() ) return false;
178 return m_storageSvc->setDomainOption(domainOption).isSuccess();
179}
DatabaseHandler is a class taking care of the micro-connections and the micro transactions for a give...
Description: Handle managing a DbDatabaseObj, a generic Database object.
Definition DbDatabase.h:54
StatusCode param(const std::string &nam, std::string &val) const
Retrieve existing parameter by name.
Description: Definition an option to be supplied to database objects.
Definition DbOption.h:36
StatusCode i_setValue(const std::type_info &typ, const void *value)
Set the option value.
StatusCode i_getValue(const std::type_info &typ, void *value) const
Read the option value.
virtual ~MicroSessionManager()
Destructor.
bool connect(Io::IoFlag transType, int ageLimit)
Connects to the storage service.
virtual bool setAttributeOfType(const std::string &attributeName, const void *data, const std::type_info &typeInfo, const std::string &option) override
The actual method setting the attribute data given a name.
void disconnect(DatabaseHandler *database)
Disconnects from a database.
virtual bool attributeOfType(const std::string &attributeName, void *data, const std::type_info &typeInfo, const std::string &option) override
The actual method returning the attribute data given a name.
std::string fidForPfn(const std::string &pfn)
Fetches the FID by trying to temporatily connect to a database.
MicroSessionManager(DatabaseRegistry &registry, long technology)
Constructor.
bool disconnectAll()
Disconnects from all the databases.
std::set< DatabaseHandler * > m_databaseHandlers
IStorageSvc * createStorageSvc(const std::string &componentName)