Some tricks when using cx_Oracle

简介:

cx_Oracle is a Python module that enables access to Oracle databases.
However, users may have confusion due to some fetures of this module.

Thread safe

The default setting of cx_Oracle is not thread-safe. 
So if user have multiple threads, make sure that specifying threaded=True when creating the connection.

conn = cx_Oracle.connect(user + '/' + passwd + "@" + host + "/" + db, threaded=True)

Otherwise, the program will crash with error message like

ORA-24550: signal received: [si_signo=11] [si_errno=0] [si_code=2] [si_addr=0000000000000000]

Fetch LOB column

Internally, Oracle uses LOB locators which are allocated based on the cursor array size. Thus, it is important that the data in the LOB object be manipulated before another internal fetch takes place. The safest way to do this is to use the cursor as an iterator. In particular, do not use the fetchall() method. The exception “LOB variable no longer valid after subsequent fetch” will be raised if an attempt to access a LOB variable after a subsequent fetch is detected.

Use curosr as an iterator rather than use fetchall() method.

self._cursor.execute(sql, *args)
def fix_lob(row):
    def convert(col):
        if isinstance(col, cx_Oracle.LOB):
            return str(col)
        else:
            return col

    return [convert(c) for c in row]

return [fix_lob(r) for r in self._cursor]

目录
相关文章
|
安全 程序员 编译器
Python有多少个版本?不同Python版本之间有什么区别?我应该选择哪一个?
Python有多少个版本?不同Python版本之间有什么区别?我应该选择哪一个?
5301 0
Python有多少个版本?不同Python版本之间有什么区别?我应该选择哪一个?
|
9月前
|
JSON 关系型数据库 PostgreSQL
PostgreSQL 9种索引的原理和应用场景
PostgreSQL 支持九种主要索引类型,包括 B-Tree、Hash、GiST、SP-GiST、GIN、BRIN、Bitmap、Partial 和 Unique 索引。每种索引适用于不同场景,如 B-Tree 适合范围查询和排序,Hash 仅用于等值查询,GiST 支持全文搜索和几何数据查询,GIN 适用于多值列和 JSON 数据,BRIN 适合非常大的表,Bitmap 适用于低基数列,Partial 只对部分数据创建索引,Unique 确保列值唯一。
|
Web App开发 JavaScript 前端开发
python获取浏览器localstorage与sessionstorage数据
python获取浏览器localstorage与sessionstorage数据
|
存储 搜索推荐 Oracle
|
2天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
13天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1288 5
|
12天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1318 87