[quoted text, click to view] >> The main drawback of SqlAuthentication (authing from browser thru
>> website thru database) is that connections can't be pooled.
>>
> This is what you posted in an earlier post.
> From the MSDN connections are pooled based on the connection string,
> not
> whether your an SQL Login or the ASPNET User.
Ah, I see your point. My use of "SqlAuthentication" was misunderstood. The
qualifier I provided after "authing from browser thru website thru database"
was the important piece which is consonnant with your post. So yes, I apologise
for the misleading terminology. But my point is still valid -- using the
credentials of the user in the browser all the way to the database means
you can't share/pool that connection.
[quoted text, click to view] >> The trick to avoid latency is to not make the trip to the database,
>> so in essence you need to cache data (if possible).
>>
> It's not possible. parameters change based on users requesting
> information. If all parameters are the same then static pages would
> do. So regardless, you're going to have to go to the database to get
> your data. I'm not taking about retrieving commonly used data either,
> as in state codes, etc... those can be cached.
It always depends upon what your data is. And you can cache larger result
sets and then show subsets based upon individual requests. But what you're
trying to say is that's not always possible. I don't disagree. But I maintain
that it is sometimes possible.
[quoted text, click to view] > Now what doesn't make sense is for me is to close the command object,
> dispose it, then null it. Why wouldn't I just cache that command
> object so that I could just reuse it again without having to set a connection
> object to it, add all the parameter descriptions again (only one in this
> example), then open the command object again.
> If I put in in cache then all I have to do is get it from cache, set
> my one parameter employee id and execute it.
> Once I'm done with it I don't have to put it back into cache, it's
> there for anyone else to use it.
I understand the sentiment here, but I still think it's specious. If you
cache the command (and I presume you're implying that you leave the connection
open), then you've just guaranteed the connection will remain open for the
duration of your cache. If there are no other users in your application then
you've guaranteed the resource is not released when it otherwise could have
been. The important resource here is the connection not the command. Caching
the command object itself does save you the creation time, but that's negligible
in the greater scheme of things. Leaving the connection open when you're
not using it is wasting the resource.
Now you could implement a timeout for the cache. Once the cache times out
(presumably because there are no users in your application) then you could
close the connection. But this is what connection pooling already does for
you. Also, the connection pool is dynamic, meaning it grows and shrinks based
upon demand. In your example, if you only cache a single command then that
becomes your bottleneck, meaning only one thread can ever access the database
at any one time. I assume you wouldn't cache only a single command, but then
you're building your own pooling mechanism. And given that the command itself
is a negligible resource you've just reinvented the wheel that the connection
pool already has available.
Now that we're discussing multiple pages sharing your command object, the
docs specifically state for the SqlCommand, OleDbCommand, ODBCCommand and
OracleCommand that these objects are not thread safe. This is, of course,
another consideration in caching. Perhaps they're not thread safe to be used
at the same time across two threads, or perhaps this means they're simply
not safe to be accessed by any other thread than the one that created them.
If you were building your own pooling mechanism then I assume you'd not hand
out the same command to two different clients (and then perhaps threads)
at the same time. So as for the ability to create the command on one thread
and use it on another, I'm sure you could do some testing to become reasonably
comfortable if they're only used one at a time that it would work. But it's
not outside the realm of possibility that these objects have some sort of
thread affinity and wouldn't be so happy working on a separate thread. This
has happened to me in the past (specifically with Oracle, FWIW).
[quoted text, click to view] > That's why caching the command objects is better. The only thing
> that's changing with the command object is the paramters being passed,
> it's the same connection, same stored procedure, same number of
> parameters. I just don't see why programmers close and dispose of it
> when they're just going to use it again.
Again, I know what you're getting at. I'm curious what your performance tests
have shown with this strategy. How much faster is it than simply relying
upon the built in connection pool? I'm curious now... :)
-Brock
DevelopMentor
http://staff.develop.com/ballen