Esempio n. 1
0
class TestPoolFunctions(unittest.TestCase):

    def setUp(self):
        self.pool = Pool(MsgPackPoolableFactory(), **config)
    def tearDown(self):
        unittest.TestCase.tearDown(self)
        self.pool.destroy()
    def test_ping(self):
        try:
            client = self.pool.borrowObject()
            self.assertIsNotNone(client, "client is None")
            rs = client.call("ping")
            self.assertEqual(rs, "pong", "is pinged")
            print rs
        finally:
            self.pool.returnObject(client)
Esempio n. 2
0
class TestPoolFunctions(unittest.TestCase):
    def setUp(self):
        self.pool = Pool(MsgPackPoolableFactory(), **config)

    def tearDown(self):
        unittest.TestCase.tearDown(self)
        self.pool.destroy()

    def test_ping(self):
        try:
            client = self.pool.borrowObject()
            self.assertIsNotNone(client, "client is None")
            rs = client.call("ping")
            self.assertEqual(rs, "pong", "is pinged")
            print rs
        finally:
            self.pool.returnObject(client)
Esempio n. 3
0
class Transaction():
    """
      基本用法1:
    ta = Transaction(dbconfig, poolConfig)

    @ta
    def test(param1,param2,db):
        return db.get("select now() as time")
    test(param1,param2)
       注意db参数在函数的 第一个

    基本用法1:
    ta=Transaction(dbconfig, poolConfig)
    def fun():
        //do something
    ta.execute(fun)

    example1:

    dbconfig = {
        "host":"127.0.0.1:4000",
        "user":"******",
        "password":"",
        "database":"test"
    }
    poolConfig = {}
    ta = Transaction(dbconfig, poolConfig)

    @ta
    def test(db):
        return db.get("select now() as time")
    @ta
    def insert(name, birthday, is_relative,db):
        return db.insert("insert into person(name,birthday,is_relative) values(%s,%s,%s)", name, birthday, is_relative)

    class Test3():
        @staticmethod
        @ta
        def test(db):
            return db.get("select now() as time")



    print test()
    from datetime import date
    print insert("x1", date.today(), 1)
    print Test3.test()


    example2:

    def test(db):
        return db.get("select now() as time")

    def test2(db):
        return db.query("select now() as time")

    class Test3():
        @staticmethod
        def test(db):
            return db.get("select now() as time")

    class Test4():
        def test(self,db):
            return db.get("select now() as time")

    t=Transaction(dbconfig, poolConfig)
    rs = t.execute(test)
    print rs.time
    rses = t.execute(test2)
    print [str(rs.time) for rs in rses]
    rs = t.execute(Test3.test)
    print rs.time
    rs = t.execute(Test4().test)
    print rs.time
    rs= t.execute(lambda db:db.get("select now() as time"))
    print rs.time
    t.destory()
    """
    
    def __init__(self, dbConfig=None, poolConfig=None, pool=None):
        '''可给定dbConfig 和poolConfig 或者pool'''
        if pool is None:
            self._pool = Pool(Torndb2PoolableFactory(**dbConfig), **poolConfig)
        else:
            self._pool = pool
            
    def destory(self):
        self._pool.destroy()  
        
    def __call__(self, func):
       
        def execute(*args,**kwargs):
            try:
                db = self._pool.borrowObject()
                a = []
                a.append(db)
                for arg in args:
                    a.append(arg)
                
                newArgs = tuple(a)
#                 print newArgs
                return func(*newArgs,**kwargs)
            except Exception, e:
                db.rollback()
                raise e
            finally:
Esempio n. 4
0
class Transaction():
    """
      基本用法1:
    ta = Transaction(dbconfig, poolConfig)

    @ta
    def test(param1,param2,db):
        return db.get("select now() as time")
    test(param1,param2)
       注意db参数在函数的 第一个

    基本用法1:
    ta=Transaction(dbconfig, poolConfig)
    def fun():
        //do something
    ta.execute(fun)

    example1:

    dbconfig = {
        "host":"127.0.0.1:4000",
        "user":"******",
        "password":"",
        "database":"test"
    }
    poolConfig = {}
    ta = Transaction(dbconfig, poolConfig)

    @ta
    def test(db):
        return db.get("select now() as time")
    @ta
    def insert(name, birthday, is_relative,db):
        return db.insert("insert into person(name,birthday,is_relative) values(%s,%s,%s)", name, birthday, is_relative)

    class Test3():
        @staticmethod
        @ta
        def test(db):
            return db.get("select now() as time")



    print test()
    from datetime import date
    print insert("x1", date.today(), 1)
    print Test3.test()


    example2:

    def test(db):
        return db.get("select now() as time")

    def test2(db):
        return db.query("select now() as time")

    class Test3():
        @staticmethod
        def test(db):
            return db.get("select now() as time")

    class Test4():
        def test(self,db):
            return db.get("select now() as time")

    t=Transaction(dbconfig, poolConfig)
    rs = t.execute(test)
    print rs.time
    rses = t.execute(test2)
    print [str(rs.time) for rs in rses]
    rs = t.execute(Test3.test)
    print rs.time
    rs = t.execute(Test4().test)
    print rs.time
    rs= t.execute(lambda db:db.get("select now() as time"))
    print rs.time
    t.destory()
    """
    def __init__(self, dbConfig=None, poolConfig=None, pool=None):
        '''可给定dbConfig 和poolConfig 或者pool'''
        if pool is None:
            self._pool = Pool(Torndb2PoolableFactory(**dbConfig), **poolConfig)
        else:
            self._pool = pool

    def destory(self):
        self._pool.destroy()

    def __call__(self, func):
        def execute(*args, **kwargs):
            try:
                db = self._pool.borrowObject()
                a = []
                a.append(db)
                for arg in args:
                    a.append(arg)

                newArgs = tuple(a)
                #                 print newArgs
                return func(*newArgs, **kwargs)
            except Exception, e:
                db.rollback()
                raise e
            finally: