cx_Oracle和数据源范例 [英] cx_Oracle and the data source paradigm

查看:219
本文介绍了cx_Oracle和数据源范例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java DataSource 中实现的数据库访问有一个Java范例。此对象创建围绕数据库连接创建的有用的抽象。 DataSource 对象保持数据库配置,但将仅根据请求创建数据库连接。这允许您将所有数据库配置和初始化代码保存在一个地方,并可以轻松更改数据库实现,或使用模拟数据库进行测试。

There is a Java paradigm for database access implemented in the Java DataSource. This object create a useful abstraction around the creation of database connections. The DataSource object keeps database configuration, but will only create database connections on request. This is allows you to keep all database configuration and initialization code in one place, and makes it easy to change database implementation, or use a mock database for testing.

我目前工作在使用cx_Oracle的Python项目。在cx_Oracle中,直接从模块获取连接:

I currently working on a Python project which uses cx_Oracle. In cx_Oracle, one gets a connection directly from the module:

import cx_Oracle as dbapi
connection = dbapi.connect(connection_string)
# At this point I am assuming that a real connection has been made to the database.
# Is this true?

我试图找到一个平行于 DataSource 在cx_Oracle。我可以很容易地通过创建一个新类并包装cx_Oracle来创建它,但是我想知道这是否是在Python中做正确的方法。

I am trying to find a parallel to the DataSource in cx_Oracle. I can easily create this by creating a new class and wrapping cx_Oracle, but I was wondering if this is the right way to do it in Python.

推荐答案

您可以通过查看 PEP-249:Python数据库API规范v2.0 cx_Oracle 符合此规范,就像Python的许多数据库驱动程序一样。

You'll find relevant information of how to access databases in Python by looking at PEP-249: Python Database API Specification v2.0. cx_Oracle conforms to this specification, as do many database drivers for Python.

在本规范中, Connection 对象表示数据库连接,但没有内置池。诸如 SQLAlchemy 之类的工具确实提供了汇集功能,虽然SQLAlchemy经常作为ORM结算,但它没有

In this specification a Connection object represents a database connection, but there is no built-in pooling. Tools such as SQLAlchemy do provide pooling facilities, and although SQLAlchemy is often billed as an ORM, it does not have to be used as such and offers nice abstractions for use on top of SQL engines.

如果你想要做对象关系映射,那么SQLAlchemy会做这个事情,您可以考虑自己的声明性语法或其他层,例如 Elixir ,它位于SQLAlchemy之上,并提供增加了更常用的用例的易用性。

If you do want to do object-relational-mapping, then SQLAlchemy does the business, and you can consider either its own declarative syntax or another layer such as Elixir which sits on top of SQLAlchemy and provides increased ease of use for more common use cases.

这篇关于cx_Oracle和数据源范例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆