I can't set clientid if the connection is closed, so I have to do it when the state is changed to open.
If I use the builtin clientid = "something" the connection on the context unexpectedly changes to closed...(?)
Code: Select all
namespace MyProject.Models
{
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 );
}
}
}