2010-02-28 34 views
2

剛剛偶然發現了Oracle 10g中提供的新ClientId(aka client_identifier)變量,並且很想將其納入到應用程序中以協助審計跟蹤。如何在企業庫中使用Oracle的ClientId屬性?

應用程序是使用被連接到使用與EntLibContrib的ODP.NET驅動程序生成的企業庫DAAB基於層(netTiers),所以CRUD功能創建Oracle數據庫對象,然後從中檢索

通用的DbCommand對象它看起來像OracleConnection類具有ClientId屬性,那麼到達該模式中的Connection對象的最乾淨的方式是什麼?我應該從每一個創建並設置它的DbCommand中獲取連接嗎?還是那種矯枉過正?

因爲EntLib在幕後做了很多連接管理,所以我不確定是否可以將ClientId設置在CRUD功能之外的某個位置,並期望它能夠保留下來?

+0

我不明白爲什麼你不能只抓的連接並更新從DbCommand對象的ClientId屬性。雖然有沒有可以直接訪問DbConnection對象的地方?除非EntLib用ClientId(我懷疑)做了什麼事情,否則它將持續用於連接會話的剩餘部分。 – mservidio 2011-04-29 19:06:42

回答

0

如果連接是分部類,則只要連接狀態變爲打開狀態,就可以實現設置客戶端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); 
    } 
}