[英]How to manage a webapp with many DB roles (users) with hibernate?
How to handle the situation where we have a Postgres database running with many database roles (representing the users) and want to use hibernate so every database statement would be executed using a connection fetched with the specific user?如何处理我们有一个 Postgres 数据库运行多个数据库角色(代表用户)的情况,并希望使用 hibernate 以便使用与特定用户获取的连接来执行每个数据库语句?
To get a Session
/ EntityManager
we need to fetch it from an EntityManagerFactory
, which requires a DB user/password, usually specified in persistence.xml
like this:要获取Session
/ EntityManager
我们需要从EntityManagerFactory
获取它,这需要数据库用户/密码,通常在persistence.xml
指定,如下所示:
<property name="javax.persistence.jdbc.user" value="SYSDBA"/>
<property name="javax.persistence.jdbc.password" value="masterkey"/>
Of course i can create a Session
/ EntityManager
for every user using a separate EntityManagerFactory
, but this is a costly operation.当然,我可以创建一个Session
/ EntityManager
使用一个单独的每个用户EntityManagerFactory
,但是这是一个代价高昂的操作。 How can this problem be solved?如何解决这个问题?
If the RDBMS is PostgreSQL I think the best way to accomplish this would be to call the SET ROLE
command.如果 RDBMS 是 PostgreSQL,我认为最好的方法是调用SET ROLE
命令。 This command will change the role and permissions to whatever role is specified.此命令会将角色和权限更改为指定的任何角色。 It will carry out all SQL commands during the session as if you logged in with that role in the beginning.它将在会话期间执行所有 SQL 命令,就像您在开始时以该角色登录一样。
Here is a link to the Postgres documentation. 这是 Postgres 文档的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.