By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

How to organize a polling thread

P: 1
Hello guys.

I have a polling thread in my application. It calls GetMyStructs method with interval 1,5 seconds.

// ...
SqlConnection _sqlConnection = new SqlConnection();
_sqlConnection.ConnectionString = string.Format("Server = {0}; User Id = {1}; pwd = {2}; Database = {3}; Pooling = false; MultipleActiveResultSets = true", Instance, Login, Password, Database); // OK

_activateAppRole = new SqlCommand("EXEC sp_setapprole application, '12345'", _sqlConnection); // OK
// ...

public MyStruct[] GetMyStructs()
{
List<MyStruct> structs = new List<MyStruct>();

try
{
_sqlConnection.Open();
_activateAppRole.ExecuteNonQuery();

SqlCommand cmd = new SqlCommand("GetMyStructs", _sqlConnection);
cmd.CommandType = CommandType.StoredProcedure;

SqlDataReader rdr = cmd.ExecuteReader();

while (rdr.Read())
{
MyStruct struct = new MyStruct();

struct.Id = Convert.ToInt32(rdr["ID"]);

// ...

structs.Add(struct);
}
}
catch (Exception exc)
{
// occasionally, I get an error described below here 'impersonate security context' ....

throw;
}
finally
{
try
{
_sqlConnection.Close();
}
catch
{
}
}

return structs.ToArray();
}


Occasionally (only occasionally), I get an error:

"Impersonate Session Security Context" cannot be called in this batch because a simultaneous batch has called it.


I know that interval 1,5 seconds is too small but my manager insists on that interval. I understand that error occurs because of previous request is not completed yet (Am I right ?).

I'm not very familiar with ms sql server or ado .net internals, but how can I manage this situation ? What should I do in order to fix my problem ?


PS: i'm not a native english speaker, so forgive me my possible grammar errors.
Sep 24 '10 #1
Share this Question
Share on Google+
1 Reply


ck9663
Expert 2.5K+
P: 2,878
It looks like you're using a single connection profile to your sql-server to execute your sp. Since your interval of 1.5 is too small, the first connection is still active when your second tries.

Can you try using a different connection type? This, however, would depend on your front-end, which I assume a C-based (C#, Java, etc) tool...

Good Luck!!!

~~ CK
Sep 24 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.