c#


What is the benefit of caching an IReader?


Reviewing some legacy code, there is a commonally used table that gets updated very infrequently.
To save having to constantly go to the database to get the same data each time, it seems like the developer was trying to cache the data. The code looks like this:
private static IDataReader _cachedCheckList;
public override IDataReader GetDataReader()
{
if (_cachedCheckList == null)
{
using (var oneTimeRead = base.GetDataReader())
{
_cachedCheckList = new CachedDataReader(oneTimeRead);
}
}
return _cachedCheckList ?? base.GetDataReader();
}
Then elsewhere in the system the function that uses this follows the pattern of:
IDataReader reader = new CheckList().GetDataReader();
while (reader.Read())
{
[snip]
}
By loading the IReader into memory, I don't think that this provides much in the way of a performance increase.
I'm trying to understand the developers reason for this code. What is the benefit of caching the IReader?
Update: The CachedDataReader() method is basically:
SqlConnection connection = new SqlConnection(ConnectionString);
connection.Open();
var sqlCommand = new SqlCommand(commandText, connection)
command.CommandType = CommandType.StoredProcedure;
return command.ExecuteReader();
I'd not seen anyone cache a DataReader before and was wondering there was a good reason to do this before refactoring the code.
As DataReader uses connected architecture .It keep the database connection while looping thru each records.So database connection will be maintain for long .
So there is really no benefit caching DataReader.
When caching is require we use dataset because it work in disconnected architecture.
Even SqlParameter is cached in old Microsoft enterprise library DAL. But again this had only this more demerit.
Just curious,you are missing the code between where connection is open till connection is close
May be
they used DataReader cache for following reasons and they are as follows :-
DataReader readonly and forward only. It fetches the record from databse and stores in the network buffer and gives whenever requests. DataReader release the records as query executes and do not wait for the entire query to execute. Therefore it is very fast as compare to the dataset. It releases only when read method is called.
DataReader should not be Cached. You should fetch data in DataSet or DataTable and then use:
Cache["Data"] = DataTable;
Never cache DataReader objects. Because a DataReader object holds an open connection to the database, caching the object extends the lifetime of the connection, affecting other users of the database. Also, because theDataReader is a forward-only stream of data, after a client has read the information, the information cannot be accessed again. Caching it would be futile.
Caching DataReader objects disastrously affects the scalability of your applications. You may hold connections open and eventually cache all available connections, making the database unusable until the connections are closed. Never cache DataReader objects no matter what caching technology you are using.
Above quotes taken from https://forums.asp.net/post/3224692.aspx

Related Links

Consuming cookies in web API
Changing View on parent viewmodel from child viewmodel
cancel a c# httpClient GetStreamAsync call
Visual Studio - C# Script Task Error (SSIS)
asp.net/C# :Non-static method requires a target [duplicate]
Unable to unload an AppDomain after running wpf application in it
C# Binary search returns negative indexes
Open email in Outlook 2013/2016 retrieved via Outlook REST API
c# call constructor by assigning
Setting cultureInfo for an async delegate
How to customize excel auto filter popup screen using VSTO com add-ins in C#
How to enforce future on IObservable?
How to bind a data table column as a data source to jquery autocomplete
FileSystemWatcher: how to rise events only for new files in directory?
The type or namespace name could not be found, but it's in the project [closed]
How to minimize the app on Deactivated event(Application_Deactivated) in wpf

Categories

HOME
airflow
algolia
redux
alasql
intellij-idea
mobile
salesforce
spss
redhawksdr
datetimepicker
thermal-printer
jshell
docker-compose
flowtype
install
sahi
oms
google-search-appliance
distinct
android-notifications
repeater
amazon-data-pipeline
cherrypy
typedef
policy
deployment-descriptor
datanucleus
symlink
multilanguage
ibm-bpm
oracle-nosql
machine-code
border-layout
motherboard
android-graphview
scrapinghub
codepen
risc
android-doze-and-standby
spring-data-commons
stream-socket-client
cloudconvert
e4
seyren
jtextarea
suitesparse
scanf
addressbook
blackberry-webworks
eclipse-mars
ooad
instabug
markerspiderfier
overlapping
magnet-uri
commercetools
play-json
edgar
biginsights
sparkcore
mergecursor
veeam
kss
illegalargumentexception
indic
winrun4j
sqldatasource
kuka-krl
ekevent
mosix
junction-table
gpl
netbiscuits
sup
flatten
pdf-reader
animationdrawable
idtabs
code-organization
cgaffinetransform
symfony-1.2
hs-err
cpack
xslt-grouping
linq-to-mysql
retrospectiva
projectgen
disk-based

Resources

Encrypt Message