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
16#include <exception>
17
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 {
66 db = new pool::DatabaseHandler( *m_storageSvc, m_technology, fid, pfn, mode );
67 m_registry.registerDatabaseHandler( db );
68 m_databaseHandlers.insert( db );
69 } catch( const std::runtime_error& /* error */) {
70 delete db;
71 m_storageSvc->endSession().ignore();
72 m_inSession = false;
73 return nullptr;
74 }
75
76 if( m_databaseHandlers.empty() && m_inSession ) {
77 m_storageSvc->endSession().ignore();
78 m_inSession = false;
79 }
80 return db;
81}
82
83
84void
86{
87 std::set< pool::DatabaseHandler* >::iterator idb = m_databaseHandlers.find( database );
88 if ( idb != m_databaseHandlers.end() ) {
89 m_registry.deregisterDatabaseHandler( *idb );
90 delete *idb;
91 m_databaseHandlers.erase( idb );
92 }
93 if( m_databaseHandlers.empty() && m_inSession ) {
94 m_storageSvc->endSession().ignore();
95 m_inSession = false;
96 }
97}
98
99
100bool
102{
103 bool ret = true;
104 for ( std::set< pool::DatabaseHandler* >::iterator idb = m_databaseHandlers.begin();
105 idb != m_databaseHandlers.end(); ++idb ) {
106 m_registry.deregisterDatabaseHandler( *idb );
107 ret = (*idb)->disconnectTransaction();
108 delete *idb;
109 }
110 m_databaseHandlers.clear();
111
112 if( m_inSession ) {
113 ret = ret and m_storageSvc->endSession().isSuccess();
114 m_inSession = false;
115 }
116 return ret;
117}
118
119std::string
121{
122 if ( m_databaseHandlers.empty() ) {
123 Io::IoFlag mode = Io::READ;
124 if( !m_storageSvc->startSession(mode, m_technology).isSuccess() ) {
125 return "";
126 }
127 }
128
129 std::string fid = "";
130 pool::FileDescriptor fd( pfn, pfn );
131 // this is only a temporary FID so use a special pattern to make that clear
132 fd.setFID( fd.FID().substr(0,24) + "0FF0FF0FF0FF" );
133 if( m_storageSvc->existsConnection(fd).isSuccess() ) {
134 if ( m_storageSvc->connect(Io::READ, fd).isSuccess() ) {
135 DbDatabase dbH( fd.dbc()->handle() );
136 if ( ! dbH.param( "FID", fid ).isSuccess() ) fid = "";
137 m_storageSvc->disconnect( fd ).ignore();
138 }
139 }
140
141 if( m_databaseHandlers.empty() ) {
142 m_storageSvc->endSession().ignore();
143 m_inSession = false;
144 }
145
146 return fid;
147}
148
149bool
150pool::MicroSessionManager::attributeOfType( const std::string& attributeName,
151 void* data,
152 const std::type_info& typeInfo,
153 const std::string& option )
154{
155 if( !m_inSession ) {
156 return false;
157 }
158 pool::DbOption domainOption( attributeName, option );
159 if( !m_storageSvc->getDomainOption(domainOption).isSuccess() ) return false;
160 return domainOption.i_getValue(typeInfo, data).isSuccess();
161}
162
163bool
164pool::MicroSessionManager::setAttributeOfType( const std::string& attributeName,
165 const void* data,
166 const std::type_info& typeInfo,
167 const std::string& option )
168{
169 if( !m_inSession ) {
170 return false;
171 }
172 pool::DbOption domainOption( attributeName, option );
173 if( !domainOption.i_setValue( typeInfo, const_cast<void*>( data ) ).isSuccess() ) return false;
174 return m_storageSvc->setDomainOption(domainOption).isSuccess();
175}
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 attributeOfType(const std::string &attributeName, void *data, const std::type_info &typeInfo, const std::string &option)
void disconnect(DatabaseHandler *database)
Disconnects from a database.
std::string fidForPfn(const std::string &pfn)
Fetches the FID by trying to temporatily connect to a database.
virtual bool setAttributeOfType(const std::string &attributeName, const void *data, const std::type_info &typeInfo, const std::string &option)
MicroSessionManager(DatabaseRegistry &registry, long technology)
Constructor.
bool disconnectAll()
Disconnects from all the databases.
std::set< DatabaseHandler * > m_databaseHandlers
IStorageSvc * createStorageSvc(const std::string &componentName)