python-cx_Oracle icon indicating copy to clipboard operation
python-cx_Oracle copied to clipboard

Using `appcontext` with SessionPool

Open ehaspulat opened this issue 5 years ago • 5 comments

  1. What versions are you using?
cx_Oracle 8.0.1

>>> import sys
>>> import platform
>>> 
>>> print("platform.platform:", platform.platform())
platform.platform: Linux-4.18.0-193.29.1.el8_2.x86_64-x86_64-with-centos-7.9.2009-Core
>>> print("sys.maxsize > 2**32:", sys.maxsize > 2**32)
sys.maxsize > 2**32: True
>>> print("platform.python_version:", platform.python_version())
platform.python_version: 3.6.9

>>> import cx_Oracle
>>> print("cx_Oracle.version:", cx_Oracle.version)
cx_Oracle.version: 8.0.1
>>> print("cx_Oracle.clientversion:", cx_Oracle.clientversion())
cx_Oracle.clientversion: (19, 9, 0, 0, 0)
  1. Describe the problem

When I want to get a connection using cx_Oracle.connection(..., appcontext=CTX_ENTRIES), I succesfully see that context entries are applied.

But when I want to use a connection pool, I was unable to find a way to provide an appcontext.

So my question is, is there a way to provide appcontext when using SessionPool, such as:

pool = cx_Oracle.SessionPool(..., appcontext=CTX_ENTRIES)

ehaspulat avatar Mar 18 '21 14:03 ehaspulat

There doesn't seem to be a direct way.

Is calling DBMS_SESSION.SET_CONTEXT in a pool sessionCallback function usable?

What are you using contexts for?

cjbj avatar Mar 19 '21 02:03 cjbj

What are you using contexts for?

We have a LOGON TRIGGER on our production database that performs certain validations, one of them expecting the sys_context('USERENV', 'OS_USER') to be a certain value. Due to some operational challenges, I cannot rely on the username cx_Oracle resolves, and need to override it.

Is calling DBMS_SESSION.SET_CONTEXT in a pool sessionCallback function usable?

Since I cannot get past the logon trigger, I assumed the sessionCallback would not work. I've performed some tests now, and they did not work out for me.

What I tried was to create a simple all denying logon trigger like this one and use sessionCallback:

CREATE OR REPLACE TRIGGER test_logon_trigger
  AFTER LOGON
  ON SOME.SCHEMA
BEGIN
  raise_application_error(-20001, 'You cannot connect, your os_user is ' || sys_context('USERENV', 'OS_USER') || '');
END;
/

EDIT: Just to give a better idea about the requirement I have, when using the JDBC driver we set -Doracle.jdbc.v$session.osuser=XXXXXX to override the required context. If there is a way to override this on oracle client level, maybe that would work out for me as well.

ehaspulat avatar Mar 19 '21 06:03 ehaspulat

Hello ,

were you able to resolve the issue? I am facing same case

Thanks

Nermin-Rabieh avatar Nov 01 '21 15:11 Nermin-Rabieh

Hello ,

were you able to resolve the issue? I am facing same case

Thanks

Unfortunately no, not at the time of discussion. We went ahead with an internal workaround that is not relevant to this issue.

ehaspulat avatar Nov 01 '21 16:11 ehaspulat

Python-oracledb 2.5 added support for this in Thin mode. See https://python-oracledb.readthedocs.io/en/latest/api_manual/module.html#oracledb.create_pool and https://python-oracledb.readthedocs.io/en/latest/release_notes.html#oracledb-2-5-0-november-2024 and

cjbj avatar Aug 25 '25 01:08 cjbj