oracle session 连接池,cx_Oracle.SessionPool 连接池

官网地址:

The example below shows how to connect to Oracle Database using a connection pool:

# Create the session pool

pool = cx_Oracle.SessionPool("hr", userpwd,

"dbhost.example.com/orclpdb1", min=2, max=5, increment=1, encoding="UTF-8")

# Acquire a connection from the pool

connection = pool.acquire()

# Use the pooled connection

cursor = connection.cursor()

for result in cursor.execute("select * from mytab"):

print(result)

# Release the connection to the pool

pool.release(connection)

# Close the pool

pool.close()

Applications that are using connections concurrently in multiple threads should set the threaded parameter to True when creating a connection pool:

# Create the session pool

pool = cx_Oracle.SessionPool("hr", userpwd, "dbhost.example.com/orclpdb1",

min=2, max=5, increment=1, threaded=True, encoding="UTF-8")

Python Callback

If the sessionCallback parameter is a Python procedure, it will be called whenever acquire() will return a newly created database connection that has not been used before. It is also called when connection tagging is being used and the requested tag is not identical to the tag in the connection returned by the pool.

An example is:

# Set the NLS_DATE_FORMAT for a session

def initSession(connection, requestedTag):

cursor = connection.cursor()

cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI'")

# Create the pool with session callback defined

pool = cx_Oracle.SessionPool("hr", userpwd, "orclpdb1",

sessionCallback=initSession, encoding="UTF-8")

# Acquire a connection from the pool (will always have the new date format)

connection = pool.acquire()

If needed, the initSession() procedure is called internally before acquire() returns. It will not be called when previously used connections are returned from the pool. This means that the ALTER SESSION does not need to be executed after every acquire() call. This improves performance and scalability.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值