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

Does pg.DB support concurrent calls? #50

Closed
medvednikov opened this issue Apr 13, 2015 · 4 comments
Closed

Does pg.DB support concurrent calls? #50

medvednikov opened this issue Apr 13, 2015 · 4 comments

Comments

@medvednikov
Copy link

Hello,

I see that unlike sql.DB, pg.DB needs to be closed explicitly via Close()

What I like about sql.DB is that I can open it once when my web application starts, and then safely use it in all go routines/HTTP requests.

Can I do the same with pg.DB, or do I need to call Connect()/Close() in every handler?

Thanks!

@medvednikov medvednikov changed the title Does the pg.DB support concurrent calls? Does pg.DB support concurrent calls? Apr 13, 2015
@vmihailenco
Copy link
Member

Actually it is the opposite. pg.DB can be used concurrently and it manages connections behind the scene, so you don't have to to call Close manually. But sql.DB requires you to manually call rows.Close to ensure that connection can be freed.

pg.DB.Close closes the client and all connections in the client pool. You should call it when app is about to exit or you want to connect to another database. I will remove Close from examples to avoid confusion.

@medvednikov
Copy link
Author

Wow you are right.

I was using gorp, and it called rows.Close() for me, so I didn't realize it was necessary.

That's great! Thanks a lot for this great library. I'm migrating my application from gorp to pg. After playing with pgx and some other libs I've come to conclusion that yours is the best one, hands down.

Two things I was missing and had to implement manually are a way to insert an entire object and nil receivers:

newUser := &User{Name:"Bob", Age:20}
Insert("INSERT INTO User", newUser)

var u *User
QueryOne(&u, "SELECT * FROM User LIMIT 1")

The nil receiver thing means less typing if you need a pointer, and it's also easier to check if nothing was found by simply comparing the receiver to nil afterwards.

Would you be willing to accept these 2 features if I submitted a pull request?

@vmihailenco
Copy link
Member

The nil receiver thing means less typing if you need a pointer, and it's also easier to check if nothing was found by simply comparing the receiver to nil afterwards.

This is the desired behaviour and I will accept a PR implementing it. Though keep in mind that QueryOne returns ErrNoRows when there are no rows.

newUser := &User{Name:"Bob", Age:20}
Insert("INSERT INTO User", newUser)

Adding Insert means that we also need Update and Delete. Then we find that we want to support more and more SQL features in Insert/Update/Delete until we fully support all SQL syntax. I don't want to go this route.

@medvednikov
Copy link
Author

Ok great, I'll submit a pull request before Tuesday.

Thanks!

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