问题描述
我有一个 Web 服务,可以处理向 DB 插入/更新的数据.当客户端调用此网络服务时,UserId(当前登录到门户的用户)将在请求中发送.我需要将此 userId 传递给 Db 连接或在 sys 上下文 中设置它以用于审计目的.我们有现有的审计表和触发器,可以在实际表上插入/更新后插入/更新审计表.因此,为了跟踪这些更改,我需要以某种方式将此 UserId 传递给 connection,以便可以从 Sys Context 或 $session 中的 DB 中检索它并插入到 Audit 表中.我目前正在使用 Spring 和 Hibernate 事务来处理 DB 数据.
I have a web service which processes inserts/updated data to DB. When client calls this webservice, UserId(currently logged in user to portal) will be sent in Request. I need to pass this userId to Db connection or set it in sys context for Audit purpose. we have existing audit tables and triggers to inserts/updates to Audit table after insert/update on actual table. So to track these changes I need to pass this UserId somehow to connection so that it can be retrieved from DB from Sys Context or $session and inserts in Audit table. I am currently using Spring and Hibernate transactions to process data with DB.
我尝试在 Connection 上设置客户端信息,但它不起作用.我在下面试过:
I tried to Set client info on Connection but it's not working. I tried below:
Session session=sessionFactory.getCurrentSession();
SessionImpl sImpl=(SessionImpl) session;
Connection connection=sImpl.connection();
connection.setClientInfo("ClientUser", "ABC");
而且我还试图在每次从应用程序代码对 DB 执行操作之前调用存储过程 DBMS_APPLICATION_INFO.SET_CLIENT_INFO 来设置客户端信息.但我不起诉它是否是处理它的正确方法.
And also I am trying to set client info by calling Stored procedure DBMS_APPLICATION_INFO.SET_CLIENT_INFO before performing operation on DB every time from application code.but I am not sue if it's a correct way to handle it.
我正在尝试使用 OCI 和瘦 JDBC 驱动程序,但找不到设置此用户 ID 的方法.
I am trying it with both OCI and thin JDBC drivers but not able find a way to set this user id.
有人可以告诉我是否有任何有效的方法可以在 sys 上下文或 Connection.h 中传递用户 ID.我目前正在使用 hibernate4、Spring、Websphere Server、Oracle DB.
Can someone let me know if there is any efficient way to pass user id on sys context or with Connection. I am currently using hibernate4, Spring, Websphere Server, Oracle DB.
我使用 Spring @Transactional 来处理休眠连接和事务来对 DB 执行操作.连接来自连接池,我使用 org.springframework.jndi.JndiObjectFactoryBean 作为 dataSource强>.
I am using Spring @Transactional to handle hibernate Connections and transactions to perform operation on DB.Connections are from Connection pool and I am using org.springframework.jndi.JndiObjectFactoryBean for dataSource.
当我们从连接池中获取连接时,有没有办法让拦截器或包装器围绕连接设置它.
is there any way to have interceptor or wrapper around connection to set it when we get the connection from connection pool.
以前有人这样做过吗?
推荐答案
这在 适用于 Oracle 数据库的 spring 数据 JDBC 扩展
章节
8.2 自定义数据源连接准备器的配置
8.2 Configuration of a Custom DataSource Connection Preparer
...但是您可以实现一个使用当前用户登录 ID 的 ConnectionPreparer.这样,即使您的数据源配置了共享用户名,您也可以捕获用户登录信息.
...but you could implement a ConnectionPreparer that would use the current users login id. That way you can capture user login information even if your data source is configured with a shared user name.
这是 oracle 的解决方案,我认为您正在使用.也应该可以将其应用于另一个数据库.
This is a solution for oracle, which I think you are using. It should be also possible to adapt that to another database.
这篇关于出于审计目的,在 syscontext/连接上传递 ClientInfo/ClientIdentifier 与 Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!