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

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

Other improvement on how SA alchemy session is managed (infoDB)

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 InfodbEPBFactory
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    _infodbEPBFactory = None   
24    _moles3EPBFactory = None
25    _migrationEPBFactory = None
26   
27    def _doInitialization(self):
28        infoDB = DbManager(INFO_DB_CONNECTION)
29        MolesSessionMiddleware._infodbEPBFactory = InfodbEPBFactory(infoDB)
30        #InfodbEPB.overrrideDBManager(infoDB)
31           
32        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
33        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
34
35        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
36        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
37       
38        MolesSessionMiddleware._migrationThread = MigrationThread( \
39            MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB(), \
40            MolesSessionMiddleware._migrationEPBFactory.createMigrationEPB(), \
41            interval = MIGRATION_INTERVAL )
42        MolesSessionMiddleware._epbInitialized = True     
43       
44        self._migration() #see the note on  MolesSessionMiddleware._migration     
45   
46    """
47    def _getMolesSession(self):
48        '''
49            @deprecated: db session is going to be removed from all GUI-related instances
50        '''
51        if not MolesSessionMiddleware._epbInitialized:
52            self._doInitialization()
53
54        return self._getNewMolesSession()
55    """
56   
57    def _migration(self, runMigration = RUN_MIGRATION):
58        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
59            #t.setDaemon(False)
60            MolesSessionMiddleware._migrationThread.start()
61           
62    """
63        Represents the access to the Moles database.
64        Creates from the existing db connections pools a new session
65        to be used by all the DB operations involved in the actual HTTPRequest
66    """ 
67   
68    def process_request(self, request): 
69        if not MolesSessionMiddleware._epbInitialized:
70            self._doInitialization()     
71
72        '''
73            @deprecated: request.moles_session is going to be removed from all GUI-related instances
74        '''
75        request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB()
76
77
78    def process_response(self, request, response):
79        request.moles3EPB.close()
80        return response
81
82
83    def process_exception(self, request, exception):
84        pass
85        """
86        try:
87            session = request.moles_session           
88        except AttributeError:
89            return
90        session.rollback()
91        session.close()
92        """       
Note: See TracBrowser for help on using the repository browser.