python dbpool
来源:互联网 发布:优化很好的单机游戏 编辑:程序博客网 时间:2024/06/04 08:49
dbpool module
Synopsis
The dbpool
module is a wrapper for PythonDB-API 2.0-compliantdatabase modules to (a) keep a pool of physical connections available and (b)upgrade the modules to threadsafety
level 2
, whichmeans that threads can share logical database connections.
Usage
The module provides only one function not described by theDB-API 2.0spec. The module is initialized by callingset_database
with a reference to theunderlying database module to be used and the number of physical databaseconnections to keep in the pool. The dbpool
module is thenconfigured to act precisely like the underlying database module, except thatphysical database connections are not always closed when you have finished withthem, and if you try to create more than one cursor on a single logicalconnection, a new physical connection is created for the new cursor.
When a logical database connection is no longer being used (i.e. you callthe close
function or all references to the connection aredeleted), the physical connection is returned to the pool to be used the nexttime a connection is requested. If the pool is full (i.e. the limit set in thecall to set_database
is reached) thenthe phsyical database connection is closed.
Note that your code can either create one logical database connection, andthen create many cursors from that, or create many logical databaseconnections, and create one or more cursors from each of these. Either way thebehaviour of the dbpool
module will be the same - it will poolphsyical database connections, and only create one cursor per physicaldatabase connection.
Example:
import jon.dbpool as dbpool
import MySQLdb, MySQLdb.cursors
dbpool.set_database(MySQLdb, 5)
dbh = dbpool.connect(user="example", passwd="s3cr3t", db="example",
cursorclass=MySQLdb.cursors.DictCursor)
dbc1 = dbh.cursor()
dbc2 = dbh.cursor()
Note that unfortunately, due to the way the DB-API works, once you haveconfigured dbpool
to act as a wrapper for a database module,you cannot re-configure with a different module.
Globals
Variables
apilevel
The string 2.0
, indicating the DB-API level.
threadsafety
The integer 2
, meaning that threads may shared the module,and database connections.
paramstyle
This variable is only available afterset_database
has been called. It willcontain the value from the underlying database module.
Warning
Error
InterfaceError
DatabaseError
DataError
OperationalError
IntegrityError
InternalError
ProgrammingError
NotSupportedError
These exception classes are only available afterset_database
has been called. Theyare copied from the underlying database module.
Functions
set_database(dbmod, minconns)
dbmod
: moduleminconns
: integer
Configures the dbpool
module to act as a wrapper around thespecified DB-API module. dbmod
is a reference to the databasemodule to use. minconns
is an integer, which must be 1 or greater,which indicates the number of physical database connections of each 'type' tokeep in the pool. Physical database connections are of the same 'type' if allthe parameters to the DB-API connect
function are the same (i.e.they are connecting to the same database, on the same host, with the sameusername, with the same options).
The database module to be used must have a threadsafety
levelof at least 1
, i.e. if the database cannot cope at all withmulti-threading then there is nothing dbpool
can do to make itwork.
Note that you can only call this function once.
Example:
set_database(MySQLdb, 5)
connect(...)
Returns
: database connection instance
This function may only be used afterset_database
has been called. Theparameters are dependent on the underlying database module being used.A logical database connection from the pool corresponding to databaseconnections with the parameters given is returned. If the pool is empty then anew physical connection is created.
Example:
dbh = dbpool.connect(user="example", passwd="s3cr3t", db="example")
dbc = dbh.cursor()
- python dbpool
- dbpool reference
- Hibernate DBpool Configure
- DBpool--单例
- dbPool sybase connection pool
- dbpool的简单的例子
- 基于DBPool的数据库连接池
- DBPool---Jdbc 各种数据库连接的配置
- DBpool连接池的使用与总结
- 数据库连接池DBPool分析(一):简介
- 数据库连接池DBPool分析(五):Makefile
- 数据库连接池DBPool分析(六):gtest
- 数据库连接池DBPool分析(三):Mysql连接控制:MysqlObj
- 数据库连接池DBPool分析(四):mysql连接池 mysql_connection_pool
- 数据库连接池DBPool分析(七):Redis连接控制 RedisObj
- 数据库连接池DBPool分析(八):redis连接池 redis_connection_pool
- ORA-01000: maximum open cursors exceeded, 引发的JDBC, DBPool的思考
- 数据库连接池DBPool分析(二):存放Mysql的查询结果 QueryResult.h
- spring总结
- mono试用手记
- InstallShield制作安装包实现自动化
- 右键菜单添加清空文件夹内容
- 通过ACM将wav转换成Windows GSM 6.10
- python dbpool
- 百日JAVA WEB魔鬼训练
- 转载:一位老工程师给年轻人的“十大忠告”
- Howto Backup PostgreSQL Databases Server With pg_dump command
- [ZZ]Ubuntu电源管理问题的说明和解决方案
- Java 面向对象编程思想之我见
- ASP.NET学习笔记(15,16)--格式化显示数据,实现计算域
- A Simple Windows HTTP Wrapper Using C++
- 《Ext详解与实践》节选:文件上传