How to know if an OracleConnection coming from the connection pool was used before
My application needs to authenticate all session on the DB via a trusted procedure (that sets some values in the session context). Currently this procedure is called for each new session just after it is opened.
I'd now like to improve this by removing unneeded round-trips. Connections from the connection pool which were used (and authenticated) before don't need to call the procedure again because the session context variables are still set on the server.
But I can't find a way to identify reused connections. Is there any way (which of course doesn't need a round-trip too)?
Architecture: Multiple client applications use the same DB account (a read-only account with synonyms to the real schema) to connect. After the connection it is required that each new session calls an authentication procedure to set some session context variables. These context variables are checked on select/insert/update/delete by Oracle FGAC (virtual private database).
My code:
OracleConnection conn = new OracleConnection();
conn.ConnectionString = _connectionString;
conn.Open();
if (true) { // TODO: Identify not yet authenticated connections.
using (OracleCommand cmd = new OracleCommand("authentication.log开发者_StackOverflow中文版in", conn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("i_user_id", OracleDbType.Int64).Value = _userId;
cmd.Parameters.Add("i_role_id", OracleDbType.Int64).Value = _roleId;
cmd.ExecuteNonQuery();
}
}
You could create a connection pool for your oracle connections.
And each connection you create could be inserted with a some key (using conn.setSessionInfo(key)
) that can be verified later when you get the connection back.
The key can be a any thing of your choice or maybe a hashkey you generate.
I finally found an answer that clearly states that this isn't possible: ODP.NET connection pooling: How to tell if a connection has been used
You should clear all contexts and reset all packages anyway when you get a connection from a connectionpool. Add this authentication to your initialization.
Preparing an Oracle Connection after being retrieved from a ConnectionPool
I'm not clear what your authentication is doing - verifying that the connection comes from your app? - and whether you're passing anything to your procedure. Could you consider using a logon trigger to call your procedure from the DB side, only for the user your pool is using, as the session is created?
精彩评论