Oracle: Read-only access to the schema for another user?

前端 未结 2 1610
情书的邮戳
情书的邮戳 2021-01-15 15:29

I have a schema and a user with the same name: products. For development I would like to use it in a read-only mode from a java application. Therefore, I create

2条回答
  •  一个人的身影
    2021-01-15 15:34

    If you have control over the way your application connects (e.g. an initialization statement for your connection pool), all you need to do is run:

    ALTER SESSION SET CURRENT_SCHEMA = PRODUCTS;
    

    From that point onward (during the lifetime of the session) any unqualified object name will be searched for in the PRODUCTS schema.

    All grants given to PRODUCTS_READONLY will be in effect. The session will run under the credentials (and security restrictions) of the original user used to log in.

    If you can not change the way the connection is established or initialized a logon trigger should also accomplish this:

    create or replace trigger logon_trg
      after logon on database
    begin
        if (user = 'PRODUCTS_READONLY') then
          execute immediate 'alter session set current_schema = products';
        end if;
    exception
      when others then null; -- prevent a login failure due to an exception
    end logon_trg;
    /
    

    Note that it's crucial to trap any exception, because otherwise a potential error in the executed SQL will effectively log everyone out off the database. So use with care and test it well before putting that into production.

提交回复
热议问题