workerpool icon indicating copy to clipboard operation
workerpool copied to clipboard

Make Workers open and close a database connection

Open adnseek opened this issue 3 years ago • 3 comments

Hi Gammazero,

i'm using your great workerpool, and i have a problem. I use the submit() function to add jobs that are processed by the workers. But these workers need to perform database tasks. When i submit my worker function and put the db.Open and db.Close inside, i have too many connections.

My Question is now : is it possible to let the workers themselves open and close the connections, so that i always have the limited number of connections open. That would be awesome

Thanks in advance Adrian

adnseek avatar Jan 26 '23 17:01 adnseek

i changed my code now from .Submit() top .WaitSubmit(). Thats slower, but it somehow works.

Maybe you have an idea about my upper question nevertheless.

Adrian

adnseek avatar Jan 27 '23 09:01 adnseek

Are you using the standard library's database/sql package? If so, you can just create one sql.DB and share it. It has its own pool of connections. The limit is set by https://pkg.go.dev/database/sql#DB.SetMaxOpenConns , which by default is unlimited. If it tries to make that many connections, something else is wrong. Posting your code would help.

See: https://go.dev/doc/database/manage-connections

hut8 avatar Feb 26 '23 01:02 hut8

hi @hut8 the solution was to put the sql.Open and Close to the start and the end of the job's function.

adnseek avatar May 13 '23 20:05 adnseek