class ConnectionManager:
    """Context Management Protocol for Database access.
    Implementation for accessing Oracle Databases."""

    def __init__(self, config: tuple) -> None:
        """config should contain the following Infromation:
            (username, password, connection_info)"""
        self.config = config
        pass

    def __enter__(self) -> Cursor:
        """Returns a cursor for the interaction with the db"""
        try:
            self.connection = Connection(*self.config)
            self.cursor = self.connection.cursor()
            return self.cursor
        except ProgrammingError as e:
            raise CMProgrammingError(e)
        except InterfaceError as e:
            raise CMInterfaceError(e)
        except Error as e:
            raise CMError(e)
        except Exception as e:
            raise CMError(e)
            pass

    def __exit__(self, exc_type, exc_val, exc_tb) -> None:
        """Commits the buffered interactions and closes the cursor and the connection"""
        self.connection.commit()
        self.cursor.close()
        self.connection.close()
        if exc_type:
            raise exc_type(exc_val)
        pass
示例#2
0
    def cancel(self, connection):
        connection_name = connection.name
        oracle_connection = connection.handle

        logger.info("Cancelling query '{}' ".format(connection_name))

        try:
            Connection.close(oracle_connection)
        except Exception as e:
            logger.error('Error closing connection for cancel request')
            raise Exception(str(e))

        logger.info("Canceled query '{}'".format(connection_name))
示例#3
0
def _check_matches(con: cx_Oracle.Connection):
    """
    Check there are not errors in imported matches

    :param con: Oracle connection object
    """
    cur = con.cursor()

    # Matches outside of the protein
    logger.info("checking out-of-bound matches")
    cur.execute("""
        SELECT M.PROTEIN_AC, M.METHOD_AC, M.POS_TO, P.LEN
        FROM INTERPRO.MATCH_NEW M
        INNER JOIN INTERPRO.PROTEIN P
          ON M.PROTEIN_AC = P.PROTEIN_AC
        WHERE M.POS_TO > P.LEN
        """)
    cnt = 0
    for row in cur:
        logger.critical("out-of-bound: {}\t{}\t{}\t{}".format(*row))
        cnt += 1

    if cnt:
        cur.close()
        con.close()
        raise RuntimeError(f"{cnt} out-of-bound matches")

    # Matches with invalid start/end positions
    logger.info("checking invalid matches")
    cur.execute("""
        SELECT PROTEIN_AC, METHOD_AC, POS_FROM, POS_TO
        FROM INTERPRO.MATCH_NEW
        WHERE POS_FROM < 1 OR POS_FROM > POS_TO
        """)
    cnt = 0
    for row in cur:
        logger.critical("invalid: {}\t{}\t{}\t{}".format(*row))
        cnt += 1

    if cnt:
        cur.close()
        con.close()
        raise RuntimeError(f"{cnt} invalid matches")

    cur.close()
示例#4
0
def close_connection(conn_db: cx_Oracle.Connection):
    """Close the connection
       parameters:
       *) conn_db : connection object 
    """
    conn_db.close()
from cx_Oracle import Connection
import time
import sys

while True:
    time.sleep(0.2)
    try:
        c = Connection("cx_Oracle/dev@localhost")
        c.close()
        sys.stdout.write(".")
        sys.stdout.flush()
    except Exception, e:
        print e
        sys.stdout.write("x")
        sys.stdout.flush()