简体   繁体   中英

How to create a connection (or session) pool in python multiprocessing code using cx_Oracle to connect to a Oracle database?

I have a very lengthy multiprocessing python code which involves interaction with the Oracle database several times during the run. The code is supposed to be an independent application which will run 24*7 and fetch data from the db, execute it using multiprocessing, write the results back to db and then again poll the database for fresh data and repeat this cycle on and on. How can I create a connection pool (or session pool , I don't know the difference between the 2, new in python and coding) to improve the performance of the code? Need to connect to Oracle db using cx_oracle . Any help will be appreciated. Many thanks in advance!!

How can I create a connection pool (or session pool, I don't know the difference between the 2, new in python and coding) to improve the performance of the code?

Each connection in a cx_Oracle connection pool corresponds to one Oracle session .

When applications frequently connect and disconnect from the database, Connection pooling will boost the performance. cx_Oracle connection pooling lets applications create and maintain a pool of connections to the database. Pools are created with cx_Oracle.SessionPool() and then SessionPool.acquire() can be called to obtain a connection from a pool. The initial pool size and the maximum pool size are provided at the time of pool creation. When the pool needs to grow, new connections are created automatically. The pool can shrink back to the minimum size when connections are no longer in use. Connections acquired from the pool should be released back to the pool using SessionPool.release() or Connection.close() when they are no longer required. Otherwise, they will be released back to the pool automatically when all of the variables referencing the connection go out of scope. The session pool can be completely closed using SessionPool.close() .

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()

Read more about Connection pooling .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM