【发布时间】:2023-02-01 16:24:13
【问题描述】:
我有一种特殊情况,我必须断开并重新连接 Oracle 数据库。 (我必须检查我的连接字符串是否仍然有效,即我的密码是否仍然有效。)
不幸的是,connection.Close() 并没有关闭会话。当我重新连接新连接时,我将恢复旧会话。
这是我的代码:
using Oracle.ManagedDataAccess.Client;
...
string connectionString = "Data Source=mydb;User Id=myuser;Password=\"mypwd\";";
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
using (OracleCommand command = new OracleCommand("DBMS_APPLICATION_INFO.SET_CLIENT_INFO", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("input", OracleDbType.Varchar2, "hello", System.Data.ParameterDirection.Input);
command.ExecuteNonQuery();
}
connection.Close();
}
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
using (OracleCommand command = new OracleCommand("DBMS_APPLICATION_INFO.READ_CLIENT_INFO", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("output", OracleDbType.Varchar2, 4000, "", ParameterDirection.Output);
command.ExecuteNonQuery();
string clientInfo = command.Parameters["output"].Value.ToString();
MessageBox.Show(clientInfo);
}
connection.Close();
}
此代码会导致显示“hello”的消息框,尽管我的新会话从未设置会话变量,因此一定不知道该值。
那么,我如何在 Oracle.ManagedDataAccess 中确保我的旧会话关闭并在我需要时获得新会话?
(我知道我可以保持我的旧连接打开然后打开另一个,但是通过每次打开一个额外的会话,我的程序最终可能会在某个时间为单个用户打开数百个打开的会话,而它应该只有一个,当然。)
【问题讨论】:
标签: c# oracle-manageddataaccess