如何将 Oracle 的 ClientId 属性与 Enterprise Library 一起使用?

发布于 2024-08-22 20:36:29 字数 430 浏览 5 评论 0原文

刚刚偶然发现了 Oracle 10g 及以上版本中提供的新 ClientId(又名 client_identifier)变量,并且很乐意将其合并到应用程序中以协助审计跟踪。

该应用程序使用生成的企业库 DAAB 基础层 (netTiers),该层连接到使用带有 EntLibContrib 的 ODP.NET 驱动程序,因此 CRUD 函数创建一个 OracleDatabase 对象,然后从中检索通用 DbCommand 对象 它

看起来 OracleConnection 类具有ClientId 属性,那么在该模式中获取 Connection 对象的最简洁方法是什么?我应该从我创建的每个 DbCommand 中获取连接并将其设置在那里,还是太过分了?

因为 EntLib 在幕后进行大部分连接管理,所以我不确定是否可以在 CRUD 函数之外的某个位置设置 ClientId 并期望它持续存在?

Just stumbled across the new ClientId (aka client_identifier) variable that is available in Oracle 10g onwards, and would love to incorporate that into an app to assist with audit trails.

The app is using a generated Enterprise Library DAAB based layer (netTiers) that is wired to use the ODP.NET drivers with EntLibContrib, so CRUD functions create an OracleDatabase object and then retrieve generic DbCommand objects from it

It looks like the OracleConnection class has the ClientId property, so what is the cleanest way to get to the Connection object within that pattern? Should I be grabbing the connection out of every DbCommand I create and setting it there, or is that overkill?

Because EntLib is doing much of the connection management behind the scenes, I'm not sure whether I can set the ClientId somewhere outside of the CRUD functions and expect it to persist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-08-29 20:36:29

如果连接是分部类,您可以实现一个触发器,每当连接状态更改为打开时,该触发器就会设置客户端 ID。
这就是我实现它的方式。

我不知道你是否可以使用其中的一部分:

public partial class DataContext
{
    partial void OnContextCreated()
    {
        if ( null == this.Connection ) return;

        this.Connection.StateChange += Connection_StateChange;
    }
    private EntityConnection EntityConnection
    {
        get { return this.Connection as EntityConnection; }
    }
    private OracleConnection OracleConnection
    {
        get { return this.EntityConnection.StoreConnection as OracleConnection; }
    } 
    private void Connection_StateChange( object sender, StateChangeEventArgs e )
    {
        if ( e.CurrentState != ConnectionState.Open ) return;

        OracleConnection conn = this.OracleConnection;
        if ( null == conn ) return;

        //closes connection on DataContext (bug?), and passes closed/broken connection 
        //conn.ClientId = HttpContext.Current == null ? "Anonymous" : HttpContext.Current.Profile.UserName;

        //working solution
        string identity = HttpContext.Current == null ? "Anonymous" : HttpContext.Current.Profile.UserName;
        OracleCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "DBMS_SESSION.SET_IDENTIFIER";
        cmd.Parameters.Add( new OracleParameter { ParameterName = "client_id", Value = identity } );
        cmd.ExecuteNonQuery();
        cmd.Dispose();

        return;
    }

    protected override void Dispose( bool disposing )
    {
        if ( null != this.Connection )
            this.Connection.StateChange -= Connection_StateChange;

        base.Dispose( disposing );
    }
}

If the connection is a partial class you can implement a trigger that sets the client id whenever the connection changes state to open.
That's the way I implemented it.

I don't know if you can use part of this:

public partial class DataContext
{
    partial void OnContextCreated()
    {
        if ( null == this.Connection ) return;

        this.Connection.StateChange += Connection_StateChange;
    }
    private EntityConnection EntityConnection
    {
        get { return this.Connection as EntityConnection; }
    }
    private OracleConnection OracleConnection
    {
        get { return this.EntityConnection.StoreConnection as OracleConnection; }
    } 
    private void Connection_StateChange( object sender, StateChangeEventArgs e )
    {
        if ( e.CurrentState != ConnectionState.Open ) return;

        OracleConnection conn = this.OracleConnection;
        if ( null == conn ) return;

        //closes connection on DataContext (bug?), and passes closed/broken connection 
        //conn.ClientId = HttpContext.Current == null ? "Anonymous" : HttpContext.Current.Profile.UserName;

        //working solution
        string identity = HttpContext.Current == null ? "Anonymous" : HttpContext.Current.Profile.UserName;
        OracleCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "DBMS_SESSION.SET_IDENTIFIER";
        cmd.Parameters.Add( new OracleParameter { ParameterName = "client_id", Value = identity } );
        cmd.ExecuteNonQuery();
        cmd.Dispose();

        return;
    }

    protected override void Dispose( bool disposing )
    {
        if ( null != this.Connection )
            this.Connection.StateChange -= Connection_StateChange;

        base.Dispose( disposing );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文