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

how to use the underlying connection pool in *gorm.DB for multiple concurrent sessions that are independent of each other? #6753

Open
alkuma opened this issue Dec 18, 2023 · 2 comments
Assignees
Labels
type:question general questions type:with reproduction steps with reproduction steps

Comments

@alkuma
Copy link

alkuma commented Dec 18, 2023

Your Question

Please see the details at go-gorm/playground#673 (comment)
This is to implement row level security (RLS) in postgresql, ie each session / connection (or whatever terminology gorm uses) will have a separate session variable value for tenantId but modifying it in one should not impact the value in the others.

The underlying code example is https://github.com/go-gorm/playground/pull/673/files#diff-ac94c91f8ea84762d55b6932ef1466a2165984591d3e646360c5bb31b2130900R115-R144

The document you expected this should be explained

https://gorm.io/docs/connecting_to_the_database.html#PostgreSQL

Expected answer

An example of how gorm connection pooling can be used to acquire separate connections / sessions which can run in parallel and can have different values of session variables.

@a631807682
Copy link
Member

Is this what you need? https://gorm.io/docs/sql_builder.html#Connection

@alkuma
Copy link
Author

alkuma commented May 24, 2024

ok I figured out the following now

  1. changing a session variable in one session changes it in all the other sessions created out of the same connection. It looks like the gorm Session is not the same as a database session, but something new with the gorm layer.
  2. I can set the session variable directly in the out of of gorm.Open(), there is no need to .Session() it and then create the session variable.

So for now I am doing the following:

  1. create a new gorm.Open() connection for every time I need to set a new session variable
  2. reuse the same connection for unique value of the session variable.

This satisfies the functionality we are trying to achieve via RLS in postgresql while still using gorm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

3 participants