我有一个应用程序,需要针对Oracle DB执行用户创建的脚本(technical用户)。该应用程序的通用模型是将结果全部作为数据集返回。因此,我进行了以下单元测试,以验证它在调用静态方法时是否处理了超时。达到超时时,它只是无限期地挂起,而不是像对SQL Server DB使用SqlCommand / SqlDataAdapter一样引发异常。我正在使用Oracle.ManagedDataAccess v19.8.0。
[TestMethod]
public void Oracle_Query_Timesout()
{
string connectionString = OracleDatabaseAccess.GetDecryptedConnectionString(
_testContext.Properties[ENCRYPTION_KEY].ToString(),
_testContext.Properties[DATASOURCE].ToString(),
_testContext.Properties[USER].ToString(),
_testContext.Properties[ENCRYPTED_PASSWORD].ToString());
bool noException = false;
try
{
DataSet sampleData = OracleDatabaseAccess.ExecuteDatabaseResultSet(
connectionString, "SELECT MAX(level) AS Test1 FROM dual CONNECT BY level <= 20000000", 2);
noException = true;
}
catch (Exception ex)
{
Assert.IsTrue(ex is OracleException);
}
Assert.IsFalse(noException, "Query succeeded, expected timeout");
}
public static DataSet ExecuteDatabaseResultSet(string connectionString, string sqlStatement, int commandTimeout = 0)
{
DataSet resultSet = new DataSet();
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
using (OracleCommand command = new OracleCommand(sqlStatement, connection))
{
command.CommandType = CommandType.Text;
command.CommandTimeout = commandTimeout;
using (OracleDataAdapter adapter = new OracleDataAdapter(command))
{
adapter.Fill(resultSet); // <--- Hangs here when timeout reached
}
// ALTERNATIVE TEST - FAILS
// using (OracleDataReader reader = command.ExecuteReader()) // <--- Hangs here when timeout reached
// {
// while (reader.Read())
// {
// // do nothing
// }
//}
}
}
return resultSet;
}