Make Workers open and close a database connection
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
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
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
hi @hut8 the solution was to put the sql.Open and Close to the start and the end of the job's function.