示例#1
0
    def __init__(self,
                 host,
                 username,
                 password,
                 dbname,
                 rootcc,
                 dbtype='mysql'):
        """@type rootcc: L{satella.instrumentation.CounterCollection}"""
        assert dbtype == 'mysql', 'I cannot support other databases!'

        dd = DatabaseDefinition(
            MySQLdb.connect,
            (MySQLdb.OperationalError, MySQLdb.InterfaceError),
            (host, username, password, dbname))

        self.cp = ConnectionPool(dd)

        # Set up instrumentation
        insmgr = CounterCollection('database')
        self.cursors_counter = PulseCounter('cursors',
                                            resolution=60,
                                            units=u'cursors per minute',
                                            description='SQL cursors created')
        insmgr.add(self.cursors_counter)
        rootcc.add(insmgr)
示例#2
0
    def test_pool_simple_query_executemany(self):
        """Creates a pool with a single connection and does SELECT 2+2 with that with executemany"""
        cp = ConnectionPool(self.dd)
        with cp.cursor() as cur:
            cur.executemany('SELECT 2+2', ())

        cp.close()
示例#3
0
    def test_transaction(self):
        """Creates a pool with a single connection and does a simple transaction on it"""
        cp = ConnectionPool(self.dd)
        with cp.transaction() as cur:
            cur.execute('SELECT 2+2')
            cur.execute('SELECT 2+2')
            cur.execute('SELECT 2+2')

        cp.close()
示例#4
0
    def test_cursorwrapper_destructor(self):
        cp = ConnectionPool(self.dd)
        c1 = cp.cursor()
        del c1  # at this point c1 should die

        import gc
        gc.collect() # PyPy could keep c1 alive, we need
                     # to ensure it's dead.

        self.assertEquals(cp.connections.qsize(), 1)
示例#5
0
    def test_pool_simple_query(self):
        """Creates a pool with a single connection and does SELECT 2+2 with that"""
        cp = ConnectionPool(self.dd)
        with cp.cursor() as cur:
            cur.execute('SELECT 2+2')
            a, = cur.fetchone()

        self.assertEquals(a, 4)

        cp.close()
示例#6
0
    def test_threads(self):
        """Pool, two threads, basic queries"""
        class TT(BaseThread):
            def __init__(self, cp, tc):
                BaseThread.__init__(self)
                self.cp = cp  #: connection pool
                self.tc = tc  #: core test case

            def run(self):
                for x in xrange(0, 10):
                    with self.cp.cursor() as cur:
                        cur.execute('SELECT 2+2')
                        a, = cur.fetchone()

                    self.tc.assertEquals(a, 4)

        cp = ConnectionPool(self.dd, 2)
        a = TT(cp, self)
        b = TT(cp, self)
        a.start(), b.start()

        cp.close()
示例#7
0
    def __init__(self, host, username, password, dbname, rootcc, dbtype='mysql'):
        """@type rootcc: L{satella.instrumentation.CounterCollection}"""
        assert dbtype == 'mysql', 'I cannot support other databases!'

        dd = DatabaseDefinition(MySQLdb.connect, 
                                (MySQLdb.OperationalError, MySQLdb.InterfaceError), 
                                (host, username, password, dbname))

        self.cp = ConnectionPool(dd)

        # Set up instrumentation
        insmgr = CounterCollection('database')
        self.cursors_counter = PulseCounter('cursors', resolution=60, 
                                            units=u'cursors per minute',
                                            description='SQL cursors created')
        insmgr.add(self.cursors_counter)
        rootcc.add(insmgr)
示例#8
0
    def test_pool_regenerate_query(self):
        """Creates a pool with a single connection and does SELECT 2+2 with that.
        Regenerates connections in the meantime"""
        cp = ConnectionPool(self.dd)
        cp.regenerate_all()
        with cp.cursor() as cur:
            cur.execute('SELECT 2+2')
            a, = cur.fetchone()

        self.assertEquals(a, 4)

        cp.close()
示例#9
0
class DatabaseManager(object):
    def __init__(self,
                 host,
                 username,
                 password,
                 dbname,
                 rootcc,
                 dbtype='mysql'):
        """@type rootcc: L{satella.instrumentation.CounterCollection}"""
        assert dbtype == 'mysql', 'I cannot support other databases!'

        dd = DatabaseDefinition(
            MySQLdb.connect,
            (MySQLdb.OperationalError, MySQLdb.InterfaceError),
            (host, username, password, dbname))

        self.cp = ConnectionPool(dd)

        # Set up instrumentation
        insmgr = CounterCollection('database')
        self.cursors_counter = PulseCounter('cursors',
                                            resolution=60,
                                            units=u'cursors per minute',
                                            description='SQL cursors created')
        insmgr.add(self.cursors_counter)
        rootcc.add(insmgr)

    def query_interface(self, ifc):
        if ifc == SelectLayerInterface:
            return SelectLayerProxy(self)
        elif ifc == PlayerDBInterface:
            return PlayerDBProxy(self)
        else:
            raise ValueError, 'Unknown interface'

    def __call__(self):
        """
        Use as in:

            with database_manager() as cur:
                cur.execute('I CAN DO SQL')
        """
        self.cursors_counter.update()
        return self.cp.cursor()
示例#10
0
    def test_pool_onetime_reconnect(self):
        """Creates a pool with a single connection, get the connection, close it
        and return it, anticipating that it will be regenerated"""
        cp = ConnectionPool(self.dd)
        # get the connection, close it and put it back
        c = cp.get_connection()
        c.close()
        cp.put_connection(c)

        # do the query, anticipating cursor regen
        with cp.cursor() as cur:
            cur.execute('SELECT 2+2')
            a, = cur.fetchone()

        self.assertEquals(a, 4)

        cp.close()
示例#11
0
class DatabaseManager(object):
    def __init__(self, host, username, password, dbname, rootcc, dbtype='mysql'):
        """@type rootcc: L{satella.instrumentation.CounterCollection}"""
        assert dbtype == 'mysql', 'I cannot support other databases!'

        dd = DatabaseDefinition(MySQLdb.connect, 
                                (MySQLdb.OperationalError, MySQLdb.InterfaceError), 
                                (host, username, password, dbname))

        self.cp = ConnectionPool(dd)

        # Set up instrumentation
        insmgr = CounterCollection('database')
        self.cursors_counter = PulseCounter('cursors', resolution=60, 
                                            units=u'cursors per minute',
                                            description='SQL cursors created')
        insmgr.add(self.cursors_counter)
        rootcc.add(insmgr)

    def query_interface(self, ifc):
        if ifc == SelectLayerInterface:
            return SelectLayerProxy(self)
        elif ifc == PlayerDBInterface:
            return PlayerDBProxy(self)
        else:
            raise ValueError, 'Unknown interface'

    def __call__(self):
        """
        Use as in:

            with database_manager() as cur:
                cur.execute('I CAN DO SQL')
        """
        self.cursors_counter.update()
        return self.cp.cursor()