views:

1433

answers:

11

What is preferable, keeping a dataset in session or filling the dataset on each postback?

A: 

Keep in session. Have option to repopulate if you need to, for example if the session memory is wiped.

IainMH
+5  A: 

That would depend on many factors. It is usually best not to keep too many items in session memory if your session is inproc or on a state server because it is less scalable.

If your session resides on the database, it might be better to just requery and repopulate the dataset unless the query is costly to execute.

+2  A: 

It depends how heavily you will do that and where your session data is stored on the server.

Keeping the datasets in session surely affects memory usage. All of the session variables are stored in memory, or in SQL server if you use SQL Server state. You can also offload the load to a different machine using the State Server mode.

The serialization/deserialization aspect. If you use insanely large datasets it could influence server seide performance badly.

On the other hand having very large viewstates on pages can be a real performance killer. So I would keep datasets in session or in the cache and look for an "outproc" way to store that session data.

splattne
A: 

Neither--don't "keep" anything! Display only what a user needs to see and build an efficient paging scheme to see rows backwards or fowards.

rp

rp
+1  A: 

I usually keep it in session if it is not too big and/or the db is far and slow. If the data is big, in general, it's better to reload.

Sometimes I use the Cache, I load from Db the first time and put it in the cache. During postback I check the chache and if it is empty I reload. So the server manage the cache by itself.

ema
+2  A: 

Since I want as few database operations as possible, I will keep the data in session, but I'm not sure what would be the best way to do it. How do I handle concurrency and also since the data is shared when two users simultaneously use the page how can I make sure each of them have their own set of data in the session?

iulianchira
Session memory, by definition, is unique to each user.
Yet what if the user opens two tabs for the same page?
iulianchira
+1  A: 

the trouble with the session is that if it's in proc it could disappear which isn't great, if it's state server then you have to serialize and if it's sql sate your're doing a round trip anyway!

if the result set is large do custom paging so that you are only returning a small subset of the total results.

then if you think more than one user will see this result set put each page in the cache as the user pages making sure that the cache is renewed when the data changes or after a while of it not being accessed.

if you don't want to make many round trips and you think you've got the memory then bung the whole dataset in the cache but be careful it doesn't melt the web server.

using the cache means users don't need their own set of data rather they use the global set.

as far as concurrency goes when you load up the insert/ edit page you need to populate it with fresh data and renew the cache after the add/update.

Charlie Bear
+1  A: 

I'm a big believer in decoupling and i rarely, if ever, see the need to throw a full dataset out to the user interface.

You should really only pass objects to the UI which needs to be used so unless you're showing a big diagram or some sort of data structure which needs to display relationships between data it's not worth the cost.

Smaller subsets of data, when applicable, is far more efficient. Is your application actually using all features within a dataset on the UI? if not, then the best way to proceed would be to only pass the data out that you're displaying.

If you're binding data to a control and sorting/paging etc through it, you can implement a lot of the interfaces which enables the dataset to support this, in a lot smaller piece of code.

On that note, i'd keep data, which is largely static (e.g. it doesn't update that often) in the cache. So you need to look at how often the data is updated before you can really make a decision for this.

Lastly, i'll say this again, i see the need to utilise a dataset in the UI very, very rarely..it's a very heavy data object and the cost benefits of looking at your data requirements, versus ease of implementation, could far outweigh the need to cache a dataset.

IMHO, datasets are rather bad to use if you're worried about performance or memory utilisation.

Brian H. Madsen
I display summary data in a gridview that requires data from several tables. The dataset is quite large and so viewstate is not an options. Also I need the data to survive on postback.
iulianchira
+1  A: 

Your answer doesn't hint at the use of the data. Is it reference data? Is the user actively updating it? Is more than one user meant to have update access at any one time?

Without any better information than you provided, go with the accepted wisdom that says keeping large amounts of data in session is a way to guarantee that your application will not scale and will require hefty resources to serve a handful of people.

There's are usually better ways to manage large datasets without resorting to loading all the data in-memory.

Failing that, if your application data needs are truly monsterous, then consider a heavy client with a web service back-end. It may be better suited than making a web page.

Robert Paulson
+4  A: 

Don't use the session!!! If the user opens a second tab with a different request the session will be reused and the results will not be the ones that he/she expects. You can use a combination of ViewState and Session but still measure how much you can handle without any sort of caching before you resort to caching.

Andrei Rinea
I just got bit on this one and hard to switch out once down this road. All worked fine until user opened two windows/tabs and navigated off on one. All updates on first window went into the account of the second - ouch.
JoeJoe
I feel your pain :(
Andrei Rinea
+1  A: 

As other answers have noted, the answer "depends". However I will assume that you are talking about data that is shared between users. In that case you should not store the dataset in the user's session, but repopulate on postback.

In addition, you should populate a cache near the data access layer of your application to increase interactive performance and reduce load on the database. The design of your cache will again depend on the type of data (read-only vs. read/write vs. read-mostly).

This approach decouples the user session (a UI-layer concept) from data management, and provides the added benefit of supporting shared use of cached data (in cases where data is common across users).

John Stauffer