def save(self): self.logger.debug(".save()") db_session = DbSession() # Prevent expiration after the session is closed or object is made transient or disconnected db_session.expire_on_commit = False try: # No need to 'add', committing this class db_session.add(self) db_session.commit() # Keep it detached make_transient(self) make_transient_to_detached(self) except InvalidRequestError as e: self.logger.error( ".save() - Could not commit to {} table in database".format( self.__tablename__), exc_info=True) self.__cleanupDbSession(db_session, self.__class__.__name__) except Exception as e: db_session.rollback() self.logger.error( ".save() - Could not commit to {} table in database".format( self.__tablename__), exc_info=True) raise DbException( "Could not commit to {} table in database".format( self.__tablename__))
def save(self): db_session = DbSession() try: db_session.add(self) db_session.commit() except InvalidRequestError as e: self.__cleanupDbSession(db_session, self.__class__.__name__) except Exception as e: db_session.rollback() self.logger.error("Could not save to {} table in database".format( self.__tablename__), exc_info=True) raise DbException("Could not save to {} table in database".format( self.__tablename__))
def update(self, data) -> None: for key, item in data.items(): setattr(self, key, item) self.modified_at = datetime.now() db_session = DbSession() try: db_session.add(self) db_session.commit() except InvalidRequestError as e: self.__cleanupDbSession(db_session, self.__class__.__name__) except Exception as e: db_session.rollback() self.logger.error( "Could not update to {} table in database".format( self.__tablename__), exc_info=True) raise DbException( "Could not update to {} table in database".format( self.__tablename__))