def connect(self): """Connect to the database. Connects to the database only if the store has not already and provided that the store has a valid model. The default implementation of connect() is usually sufficient provided that subclasses have implemented newConnection(). """ assert self._model, 'Cannot connect:' \ ' No model has been attached to this store yet.' if not self._connected: self._connection = self.newConnection() self._connected = True self.readKlassIds() poolSize = self.setting('SQLConnectionPoolSize', 0) if poolSize: args = self._dbArgs.copy() self.augmentDatabaseArgs(args, pool=True) try: self._pool = DBPool(self.dbapiModule(), poolSize, **args) except TypeError: if 'database' in args: del args['database'] self._pool = DBPool(self.dbapiModule(), poolSize, **args) else: raise
def testDbPool(self): pool = DBPool(sqlite3, 10, database=':memory:') for _count in range(15): con = pool.connection() cursor = con.cursor() cursor.execute("select 1 union select 2 union select 3 order by 1") rows = cursor.fetchall() self.assertEqual(rows, [(1, ), (2, ), (3, )]) con.close()
def Test(iterations=15): try: dbapi_name = 'pgdb' dbapi = __import__(dbapi_name) pool = DBPool(dbapi, 10, database='template1') for i in range(iterations): db = pool.connection() cursor = db.cursor() cursor.execute("select datname from pg_database order by 1") r = cursor.fetchmany(5) r = ', '.join(map(lambda s: s[0], r)) print i, r db.close() except Exception: import traceback traceback.print_exc() print 'You need the pgdb adapter and a test database for this example.'
def connect(self): """ Connects to the database only if the store has not already and provided that the store has a valid model. The default implementation of connect() is usually sufficient provided that subclasses have implemented newConnection(). """ assert self._model, 'Cannot connect: No model has been attached to this store yet.' if not self._connected: self._connection = self.newConnection() self._connected = 1 self.readKlassIds() poolSize = self.setting('SQLConnectionPoolSize', 0) if poolSize: args = self._dbArgs.copy() if not args.get('db'): args['db'] = self._model.sqlDatabaseName() self._pool = DBPool(self.dbapiModule(), poolSize, **args)