Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack: Detect if IDbConnection is "busy" - is a DataReader is open (trying to implement a "connection pool")
    text
    copied!<p>I am testing out ServiceStacks OrmLite. I have previosly used MySql without OrmLite and now I am faced with the problem easiest described in this error message:</p> <blockquote> <p>There is already an open DataReader associated with this Connection which must be closed first.</p> </blockquote> <p>Since I have a multi-threaded application, certain threads will be polling the database, while other will insert, update or select "on demand", when needed. This results in the above mentioned exception.</p> <p>What I need to do is to be able to detect if a connection (IDbHandler) is "busy"; has an open DataReader or something else that. If it is busy, take the next connection (from the "connection pool" i want to implement). The problem is, there is no method or property I can use in the IDbHandler object to determine if it is busy or not.</p> <p>I have solved this in the "normal" mysql case by simply having a method where I send in the MySqlCommand or just the query string, like:</p> <pre><code>dbConnections.ExecuteQuery("SELECT * FROM test"); dbConnections.ExecuteQuery(cmd); // cmd == MySqlCommand </code></pre> <p>and the ExecuteQuery will handle of finding an open connection and just passing on the cmd/query there. </p> <p>But, since I am using OrmLite it has a lot of extension methods to IDbConnection and I do not want to create "proxy methods" for each one. In the simple mysql case above, there is really only one method needed, that takes in a MySqlCommand, but not so with the many methods in OrmLite.</p> <p>The first question:</p> <ul> <li>How can I detect if a connection is busy? I want to avoid a try-catch situation to detect it.</li> </ul> <p>Second question:</p> <ul> <li>Is there some way to pass the entire "method" call, something like:</li> </ul> <p>Example:</p> <pre><code>dbConnections.Run(iDbHandler.Select&lt;MyObject&gt;(q =&gt; q.Id &gt; 10)); // or dbConnections.Run(iDbHandler.Where&lt;MyObject&gt;(q =&gt; q.Id &gt; 10)); // or dbConnections.Run(iDbHandler.SomeOtherWeirdMetod&lt;MyObject&gt;(q =&gt; bla bla bla)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload