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

Why doesn't it automatically recycle connections in the connection pool? #425

Open
zhang570221322 opened this issue Aug 23, 2023 · 1 comment · May be fixed by #427
Open

Why doesn't it automatically recycle connections in the connection pool? #425

zhang570221322 opened this issue Aug 23, 2023 · 1 comment · May be fixed by #427

Comments

@zhang570221322
Copy link

zhang570221322 commented Aug 23, 2023

#%%
import threading
import time
import dataset

engine_kwargs={"echo":False, "pool_size":3, "max_overflow":2, "pool_recycle":180,"pool_pre_ping":True,"pool_use_lifo":True}
config_str='postgresql://postgres:[email protected]:5432/test'
db = dataset.connect(config_str,engine_kwargs=engine_kwargs)

print(len(db.connections))
def insert_data():
    with db as tx:
        print(len(db.connections),"insert",threading.current_thread().getName())
        tx['user'].insert(dict(name='John Doe', age=46, country='China'))
        print(len(db.connections),"end",threading.current_thread().getName())
        
threads = []
for _ in range(5):
    thread = threading.Thread(target=insert_data)
    threads.append(thread)
    thread.start()
 
for thread in threads:
    thread.join()
 
time.sleep(0.5) 
print(len(db.connections))
print("start block")

# QueuePool limit of size 3 overflow 2 reached, connection timed out, timeout 30.00 
print(db['user'].count())
# %%

0
1 insert Thread-1
2 insert Thread-2
3 insert Thread-3
4 insert Thread-4
5 insert Thread-5
5 end Thread-5
5 end Thread-1
5 end Thread-2
5 end Thread-3
5 end Thread-4
5
start block
Traceback (most recent call last):
sqlalchemy.exc.TimeoutError: QueuePool limit of size 3 overflow 2 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/14/3o7r)

@rrr2rrr
Copy link

rrr2rrr commented Nov 1, 2023

#418

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

Successfully merging a pull request may close this issue.

2 participants