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

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