Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not release connection from connection pool #12

Open
spree1010 opened this issue Aug 24, 2022 · 1 comment
Open

Not release connection from connection pool #12

spree1010 opened this issue Aug 24, 2022 · 1 comment

Comments

@spree1010
Copy link

Hi,

After get connection from connection pool, application run below code to make two database insert in a transaction.
The problem is when sql_2 execute fails, the database connection is released.
Would you please help suggest where is wrong.

with conn:
with conn.cursor() as cur:
sql_1= "INSERT INTO T1 (V1, V2) values(%s, %s)"
cur.execute(sql_1, ('Value1', 'Value2'))
sql_2= "INSERT INTO T2 (V3, V4) values(%s, %s)"
cur.execute(sql_2, ('Value3', 'Value4'))
conn.commit()

@jkklee
Copy link
Owner

jkklee commented Sep 1, 2022

  1. make two database insert in a transaction:

you didn't use conn.begin() before the insert sql.

if you want to make a transaction, you should use begin and commit explicit.

try:
    conn=pool.get_connection()
    conn.begin()
    #do something
    conn.commit()
    conn.close()
except Exception as e:
    conn.rollback()
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants