Ignore:
Timestamp:
03/06/12 20:29:12 (8 years ago)
Author:
mnagni
Message:

Great improvement on how SA alchemy session is managed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/MolesManager/trunk/src/libs/migration/MigrationEPB.py

    r8323 r8325  
    88from sqlalchemy.sql.expression import asc 
    99from libs.epb import EPB 
    10 from libs.migration.exception.exceptions import NoDBManager 
    11 from libs.migration.processor.commons import stringToTimestamp 
    1210from sqlalchemy.orm.collections import InstrumentedList 
    13 from sqlalchemy.orm.util import identity_key 
    1411 
    1512 
    16 class MigrationEPB(EPB): 
     13class MigrationEPBFactory(EPB): 
    1714     
    18     _migrationDB = None 
    19      
    20     @classmethod 
    21     def overrrideDBManager(self, dbManager): 
    22         """ 
    23             Sets the MigrationEPB libs.commons_db.DbManager 
    24         """ 
    25         MigrationEPB._migrationDB = dbManager          
     15    def __init__(self, dbManager): 
     16        self._dbManager = dbManager 
     17 
     18    def _getSession(self): 
     19        if self._dbManager is not None: 
     20            return self._dbManager.createDbSession()                 
     21        return None 
    2622         
    27     @classmethod 
    28     def persistInstance(self, instance, session = None): 
     23    def createMigrationEPB(self): 
     24        return MigrationEPB(self._getSession()) 
     25         
     26         
     27class MigrationEPB(object): 
     28             
     29    def __init__(self, session): 
     30        self._session = session             
     31 
     32    def close(self): 
     33        return self._session.close() 
     34             
     35    def persistInstance(self, instance): 
    2936        """ 
    3037            Adds a new migration object. 
     
    3340            @return an updated, session independent, object instance reflecting the new persisted object  
    3441        """       
    35         intSession = MigrationEPB._getSession(session) 
    36         EPB.persistInstance(instance, intSession) 
    37         if session is None: 
    38             intSession.close() 
     42        EPB.persistInstance(instance, self._session) 
    3943        #return ret 
    4044 
    41     @classmethod 
    4245    def getAllDeploymentsMigrationByDataEntitySortedByDate(self, dataEntity, deploymentNames): 
    43         intSession = MigrationEPB._getSession(None) 
    44         res = EPB.getAllObjects(DeploymentsMigration, intSession).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")).all() 
    45         intSession.close() 
    46         return res 
     46        return EPB.getAllObjects(DeploymentsMigration, self._session).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")).all() 
    4747        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation"))         
    4848        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_status', dataEntity.doc_status)]).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")) 
    4949 
    50     @classmethod 
    51     def _getMigrationObject(self, mo_type, doc_status, doc_owner, doc_name, session = None): 
    52         intSession = MigrationEPB._getSession(session) 
    53         return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() 
     50    def _getMigrationObject(self, mo_type, doc_status, doc_owner, doc_name): 
     51        return self._session.query(mo_type).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() 
    5452        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first()         
    5553        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_status', doc_status)]).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() 
    5654 
    57     @classmethod 
    58     def _getMigrationObjectByName(self, mo_type, migrationObject, doc_name, session = None): 
    59         return self._getMigrationObject(mo_type, migrationObject.doc_status, migrationObject.doc_owner, doc_name, session)              
     55    def _getMigrationObjectByName(self, mo_type, migrationObject, doc_name): 
     56        return self._getMigrationObject(mo_type, migrationObject.doc_status, migrationObject.doc_owner, doc_name)              
    6057 
    61     @classmethod 
    62     def getDeploymentsMigrationByName(self, migrationObject, doc_name, session = None): 
     58    def getDeploymentsMigrationByName(self, migrationObject, doc_name): 
    6359        if migrationObject is None: 
    6460            raise Exception("migrationObject is None") 
    65         return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name, session)         
     61        return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name)         
    6662 
    67     @classmethod 
    68     def getDeploymentDataMigrationByName(self, migrationObject, doc_name, session = None): 
     63    def getDeploymentDataMigrationByName(self, migrationObject, doc_name): 
    6964        if migrationObject is None: 
    7065            raise Exception("migrationObject is None") 
    71         return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name, session) 
     66        return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name) 
    7267 
    73     @classmethod 
    74     def getDataEntityMigrationbyPath(self, migrationObject, session = None): 
     68    def getDataEntityMigrationbyPath(self, migrationObject): 
    7569        """ 
    7670            Returns the DataEntityMigration associated with the given path 
    7771            @param migrationObject: the migration object to look for. If None returns all the DataEntityMigration items 
    7872        """ 
    79         intSession = MigrationEPB._getSession(session) 
    8073        if migrationObject: 
    81             ret = intSession.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first()                
     74            ret = self._session.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first()                
    8275        else: #then process all the DataEntities 
    83             ret = EPB.getAllObjects(DataEntityMigration, intSession) 
    84         if session is None: 
    85             intSession.close()                    
     76            ret = EPB.getAllObjects(DataEntityMigration)                   
    8677        return ret 
    87  
    88     @classmethod         
    89     def updateMigrationObject(self, migration_object, cols_to_update, session = None): 
     78      
     79    def updateMigrationObject(self, migration_object, cols_to_update): 
    9080        """ 
    9181            Update and eventually commit a Migration Object in Migration db 
     
    9484            @param session: the external session to use. If None a new session will be open to add and commit the object and then closed at the exit. The object is committed 
    9585        """ 
    96         intSession = MigrationEPB._getSession(session) 
    9786        coll = None 
    9887        try: 
    99             coll = intSession.merge(migration_object)      
     88            coll = self._session.merge(migration_object)      
    10089        except Exception as e: 
    10190            print e                
     
    10594                    val = None 
    10695                    try: 
    107                         val = intSession.merge(v) 
     96                        val = self._session.merge(v) 
    10897                    except Exception: 
    10998                        val = v 
     
    116105                    else: 
    117106                        setattr(coll, k, val) 
    118         EPB.persistInstance(coll, intSession)                         
    119         #intSession.commit()         
    120         if session is None: 
    121             intSession.close() 
    122         #if coll: 
    123         #    return coll 
     107        EPB.persistInstance(coll, self._session)                              
    124108             
    125  
    126     @classmethod 
    127     def getNewMigrationSession(self): 
    128         return MigrationEPB._getSession() 
    129  
    130     @classmethod 
    131     def loadAttributes(self, instance, attributes, session = None): 
     109    def loadAttributes(self, instance, attributes): 
    132110        """ 
    133111            Returns the attribute of an instance. The parameter "attributes" is a single string or a list of attributes  
     
    149127            @return: a detached instance (or array of instances) of the required attribute.                      
    150128        """ 
    151         intSession = MigrationEPB._getSession(session) 
    152         instance = intSession.merge(instance) 
    153         EPB.loadAttributes(instance, attributes, session)         
    154         if session is None: 
    155             intSession.close()                 
     129        instance = self._session.merge(instance) 
     130        EPB.loadAttributes(instance, attributes)                         
    156131        return instance   
    157132 
    158     @classmethod 
    159     def search(self, clazz, inst_id, session = None): 
     133    def search(self, clazz, inst_id): 
    160134        if clazz is None or inst_id is None: 
    161135            return None  
    162         intSession = MigrationEPB._getSession(session) 
    163         ret = EPB.search(clazz, inst_id, intSession) 
    164         if session is None: 
    165             intSession.close() 
     136        ret = EPB.search(clazz, inst_id, self._session) 
    166137        return ret  
    167138          
    168  
    169     @classmethod 
    170     def _getSession(self, session = None): 
    171         if MigrationEPB._migrationDB is None: 
    172             raise NoDBManager("MigrationEPB") 
    173         return EPB._getSession(MigrationEPB._migrationDB, session) 
Note: See TracChangeset for help on using the changeset viewer.