简体   繁体   English

如果我们使用 PostgreSQL 多连接而不关闭它会怎样?

[英]What happen if we use PostgreSQL multiple connection without closing it?

connection.py File connection.py 文件

def create_connection():
    connection = psycopg2.connect("dbname=suppliers user=postgres password=postgres")
    return connection

def create_cursor():
    connection = create_connection()
    cursor = connection.cursor()
    return cursor

Above example will create a connection 2 times when calling both create_connection() and create_cursor() method当同时调用 create_connection() 和 create_cursor() 方法时,上面的示例将创建连接 2 次

Query File查询文件

def common_query():
    sql_query = "INSERT INTO supply VALUES (1, 'test', 'test')"
    conn = create_connection()
    cursor = create_cursor()
    with conn:
         with cursor as cur:
            cur.execute(sql_query)
   conn.close()

Above example will call create_connection and create_cursor method, but as you can see while calling create_connection, connection has already been established and in create_cursor() while calling create_connection() method again it create another connection.上面的示例将调用 create_connection 和 create_cursor 方法,但是正如您在调用 create_connection 时看到的那样,连接已经建立,并且在 create_cursor() 中再次调用 create_connection() 方法时它会创建另一个连接。

So while execution query it does't show any error nor it insert my data into database.因此,在执行查询时它不会显示任何错误,也不会将我的数据插入数据库。 Let me know whats happened in it?让我知道里面发生了什么?

You create two connections for each call to common_query.您为每次调用 common_query 创建两个连接。 One is explicitly closed, the other is closed at some point because it went out of scope. (Python is a garbage collected language)一个是显式关闭的,另一个是在某个时候关闭的,因为它超出了 scope。(Python 是一种垃圾收集语言)

You don't commit on either one, so whatever work you did gets rolled back automatically.你不提交任何一个,所以你所做的任何工作都会自动回滚。 This is unrelated to the first point.这与第一点无关。 The same thing would happen if you had only created one connection (and also didn't commit on it).如果您只创建了一个连接(并且也没有提交),同样的事情也会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我们在 python 的同一个 class 中有多个 __init__ 方法会发生什么? - what will gonna happen if we have multiple__init__ methods in the same class in python? Flask + Postgresql和打开/关闭数据库连接 - Flask + Postgresql and opening/closing a db connection 在Python中,如果我将普通变量传递给函数并使用apply_async来执行多个进程,将会发生什么? - In Python, what will happen if I pass a normal variable to a function and use apply_async to execute it with multiple processes? 如何在不关闭连接的情况下获取整个消息 - 导致可能获取多条消息? - How can I get the Whole Message Without Closing Connection — Resulting in Possibility to Get Multiple Messages? Python多重继承问题会发生什么? - what will happen in Python multiple Inheritance problem? 如果我们在 Python 中使用耗尽的生成器进行循环会怎样? - What happen if we do for loop using exhausted generator in Python? 多次调用mpi时关闭连接 - Closing connection when calling mpi multiple times 我们如何在 postgresql 中使用 INSERT 和 WHERE - How do we use INSERT with WHERE in postgresql 当我在 DataFrame 中使用双括号时会发生什么? - What happen when I use double bracket with DataFrame? 在不关闭连接的情况下获取python中更新的MySQL表条目 - Get updated MySQL table entries in python without closing connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM