Ignore:
Timestamp:
17/06/12 15:40:15 (7 years ago)
Author:
mnagni
Message:

Major refactoring about migration (now handles better create/update, even if the single updates have to be quite fully implemented)
Added the connection pool from SQLAlchemy

File:
1 edited

Legend:

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

    r8344 r8358  
    11''' 
     2BSD Licence 
     3Copyright (c) 2012, Science & Technology Facilities Council (STFC) 
     4All rights reserved. 
     5 
     6Redistribution and use in source and binary forms, with or without modification,  
     7are permitted provided that the following conditions are met: 
     8 
     9    * Redistributions of source code must retain the above copyright notice,  
     10        this list of conditions and the following disclaimer. 
     11    * Redistributions in binary form must reproduce the above copyright notice, 
     12        this list of conditions and the following disclaimer in the documentation 
     13        and/or other materials provided with the distribution. 
     14    * Neither the name of the Science & Technology Facilities Council (STFC)  
     15        nor the names of its contributors may be used to endorse or promote  
     16        products derived from this software without specific prior written permission. 
     17 
     18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
     19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  
     20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR  
     21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
     22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
     23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  
     24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
     25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
     26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  
     27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     28 
    229Created on 17 Nov 2011 
    330 
    4 @author: mnagni 
     31@author: Maurizio Nagni 
    532''' 
    633from sqlalchemy import MetaData, create_engine 
    7 from sqlalchemy.orm import sessionmaker, scoped_session 
     34from sqlalchemy.orm import sessionmaker 
    835from sqlalchemy.sql.expression import text 
    936class DbManagerCollection(object): 
     
    2552        Represents a database instance managed by an SQLAlchemy engine.         
    2653    """ 
    27     def __init__(self, connection, script = None, sql_echo=False, session_event_manager = None): 
     54    def __init__(self, connection, script = None, sql_echo=False, session_event_manager = None, ipool_size = 10): 
    2855        """ 
    2956          @param connection: the URL used to open a connection to the database 
     
    3158          @param sql_echo: sets the SQLalchemy create_engine.echo param. Default is False  
    3259          @param session_event_manager: a python method defining session events \ 
    33           (see http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=session%20events#sqlalchemy.orm.events.SessionEvents)      
     60          (see http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=session%20events#sqlalchemy.orm.events.SessionEvents) 
     61          @param ipool_size: sets the connections pool size (note that this is the maximum size not the number of the pre-allocated ones)       
    3462        """ 
    3563        self._connection = connection         
    3664        self._script = script 
    37         self.engine = None 
    38         try: 
    39             self.engine = create_engine(self._connection, echo=sql_echo) 
    40         except Exception as e: 
    41             print e 
     65        self.engine = create_engine(self._connection, pool_size = ipool_size, pool_recycle=3600, echo=sql_echo) 
    4266        self.metadata = MetaData(bind=self.engine)         
    4367        self._session = None 
     
    4670            self._script(self.metadata) 
    4771            self.metadata.create_all(bind=self.engine) 
    48             #self._session = scoped_session(sessionmaker()) 
    4972        if self.engine: 
    5073            self.engine.connect() 
     
    5578           @return: a sqlalchemy.orm.session.Session instance if the ORM is fully configured, otherwise a sqlalchemy.engine.base.Connection instance  
    5679        """ 
    57         #session = scoped_session(sessionmaker(bind=self.engine))() 
    5880        session = sessionmaker(bind=self.engine)()         
    5981        if self._session_event_manager: 
    6082            self._session_event_manager(session) 
    6183        return session         
    62         #if self._session: 
    63         #    ret = self._session() 
    64         #    if self._session_event_manager: 
    65         #        self._session_event_manager(ret) 
    66         #    return ret 
    67         #if self.engine: 
    68         #    return self.engine.connect() 
    6984 
    7085    def closeDbSession(self, dbSession): 
     
    8499        t = text(sqlNative) 
    85100        return self._connection.execute(t) 
     101 
     102class NoDBManager(Exception): 
     103    def __init__(self, value): 
     104        self.value = value 
     105    def __str__(self): 
     106        return "No DBManager has been set for %s" % (self.value)  
    86107 
    87108def doInsertOrUpdate(items, session, update = True): 
Note: See TracChangeset for help on using the changeset viewer.