source: mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py @ 8325

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py@8325
Revision 8325, 3.2 KB checked in by mnagni, 8 years ago (diff)

Great improvement on how SA alchemy session is managed

RevLine 
[8054]1'''
2Created on 9 Jan 2012
3
4@author: mnagni
5'''
[8325]6from MolesManager.moles3epb import Moles3EPBFactory
[8077]7from libs.migration.client import MigrationThread
[8155]8from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
9    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
[8223]10    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
[8155]11from libs.commons_db import DbManager
[8223]12from libs.migration.InfodbEPB import InfodbEPB
[8325]13from libs.migration.MigrationEPB import MigrationEPBFactory
[8054]14
[8088]15
[8229]16
[8054]17class MolesSessionMiddleware(object):
[8077]18   
19    #This attribute should be not here.
20    #unfortunately I cannot find any start/stop signals from Django
[8325]21    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
[8155]22    _epbInitialized = False
[8325]23    _moles3EPBFactory = None
24    _migrationEPBFactory = None
[8077]25   
[8229]26    def _doInitialization(self):
27        infoDB = DbManager(INFO_DB_CONNECTION)
28        InfodbEPB.overrrideDBManager(infoDB)
29           
30        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
[8325]31        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
[8229]32
[8245]33        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
[8325]34        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
35       
36        MolesSessionMiddleware._migrationThread = MigrationThread( \
37            MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB(), \
38            MolesSessionMiddleware._migrationEPBFactory.createMigrationEPB(), \
39            interval = MIGRATION_INTERVAL )
[8323]40        MolesSessionMiddleware._epbInitialized = True     
41       
42        self._migration() #see the note on  MolesSessionMiddleware._migration     
[8229]43   
[8323]44    """
[8155]45    def _getMolesSession(self):
[8323]46        '''
47            @deprecated: db session is going to be removed from all GUI-related instances
48        '''
[8155]49        if not MolesSessionMiddleware._epbInitialized:
[8229]50            self._doInitialization()
[8155]51
[8217]52        return self._getNewMolesSession()
[8323]53    """
[8155]54   
[8077]55    def _migration(self, runMigration = RUN_MIGRATION):
56        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
57            #t.setDaemon(False)
58            MolesSessionMiddleware._migrationThread.start()
59           
[8054]60    """
[8077]61        Represents the access to the Moles database.
62        Creates from the existing db connections pools a new session
63        to be used by all the DB operations involved in the actual HTTPRequest
[8054]64    """ 
65   
[8323]66    def process_request(self, request): 
67        if not MolesSessionMiddleware._epbInitialized:
68            self._doInitialization()     
[8054]69
[8323]70        '''
71            @deprecated: request.moles_session is going to be removed from all GUI-related instances
72        '''
[8325]73        request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB()
[8088]74
[8323]75
[8054]76    def process_response(self, request, response):
[8325]77        request.moles3EPB.close()
[8054]78        return response
79
[8088]80
[8054]81    def process_exception(self, request, exception):
[8323]82        pass
83        """
[8054]84        try:
85            session = request.moles_session           
86        except AttributeError:
87            return
88        session.rollback()
[8323]89        session.close()
90        """       
Note: See TracBrowser for help on using the repository browser.