Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 128495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:38:41+00:00 2026-05-11T05:38:41+00:00

I had no luck with this question so I’ve produced this simple-as-possible-test-case to demonstrate

  • 0

I had no luck with this question so I’ve produced this simple-as-possible-test-case to demonstrate the problem.

In the code below, is it possible to detect that the connection is unusable before trying to use it?

    SqlConnection c = new SqlConnection(myConnString);      c.Open();  // creates pool      setAppRole(c);  // OK      c.Close(); // returns connection to pool      c = new SqlConnection(myConnString); // gets connection from pool      c.Open(); // ok... but wait for it...      // ??? How to detect KABOOM before it happens?      setAppRole(c); // KABOOM 

The KABOOM manifests as a error in the Windows event log;

The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context. This scenario is not supported. See ‘Impersonation Overview’ in Books Online.

…plus an exception in code.

setAppRole is a simple method to set an application role on the connection. It is similar to this…

static void setAppRole(SqlConnection conn) {      using (IDbCommand cmd = conn.CreateCommand())         {             cmd.CommandType = CommandType.Text;             cmd.CommandText = 'exec sp_setapprole ';             cmd.CommandText += string.Format('@rolename='{0}'',myUser);             cmd.CommandText += string.Format(',@password='{0}'',myPassword);             cmd.ExecuteNonQuery();         }     } 

In the real code an attempt is made to use sp_unsetapprole prior to closing the connection but it cannot always be guaranteed (inherited buggy multithreaded app). In any case it still seems reasonable to expect to be able to detect the kaboom before causing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:38:41+00:00Added an answer on May 11, 2026 at 5:38 am

    In short, it doesn’t look like you can in any simple way.

    My first thought was to run this SQL:

    SELECT CASE WHEN USER = 'MyAppRole' THEN 1 ELSE 0 END 

    This works if you use SQL Server Management Studio, but fails when you run it from C# code. The trouble is the error you are getting is not occuring when the call to sp_setapprole is made, it is actually occuring when connection pooling calls sp_reset_connection. Connection pooling calls this when you first use a connection and there is no way to get in before it.

    So I guess you have four options:

    1. Turn connection pooling off by adding ‘Pooling=false;’ to your connection string.
    2. Use some other way to connect to SQL Server. There are lower level APIs than ADO.Net, but frankly it is probably not worth the trouble.
    3. As casperOne says you could fix your code to close the connection correctly.
    4. Catch the exception and reset the connection pool. I’m not sure what this will do to other open connections though. Example code below:
    class Program {     static void Main(string[] args)     {         SqlConnection conn = new SqlConnection('Server=(local);Database=Test;UID=Scrap;PWD=password;');          setAppRole(conn);         conn.Close();          setAppRole(conn);         conn.Close();     }      static void setAppRole(SqlConnection conn)      {         for (int i = 0; i < 2; i++)         {             conn.Open();             try             {                 using (IDbCommand cmd = conn.CreateCommand())                 {                     cmd.CommandType = CommandType.Text;                     cmd.CommandText = 'exec sp_setapprole ';                     cmd.CommandText += string.Format('@rolename='{0}'', 'MyAppRole');                     cmd.CommandText += string.Format(',@password='{0}'', 'password1');                     cmd.ExecuteNonQuery();                 }             }             catch (SqlException ex)             {                 if (i == 0 && ex.Number == 0)                 {                     conn.Close();                     SqlConnection.ClearPool(conn);                     continue;                 }                 else                 {                     throw;                 }             }             return;         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this isn't a unique issue but I've not had much luck finding
Has anyone had any luck running two instances of the iPhone simulator to test
Has anyone had luck with removing large amount of issues from a jira database
Has anyone had any luck with querying/changing SPNs on a Windows domain? Most of
Has anyone had any luck of using Oracle from .Net on a 64 bit
Had a coworker ask me this, and in my brain befuddled state I didn't
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
I had used Server Explorer and related tools for graphical database development with Microsoft
I had been happily coding along on a decent sized solution (just over 13k
I had a discussion with some colleagues mentioning that there are not too many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.