views:

271

answers:

2

I have a classic ASP site where I create a dictionary when the user logs in and then stores that dictionary in a session variable like so...

dim objDict
set objDict = server.createobject("scripting.dictionary")
' processing here to fill dictionary
set session("user") = objDict

That all works fine and dandy but when I navigate to another page and try to access a value from the stored dictionary like this...

session("user").item("id")

I get the following error...

error '80020009'

Can anyone tell me if I'm accessing the stored dictionary incorrectly? Is storing the dictionary object in a session variable a bad/wrong thing to do?

Thanks

+2  A: 

Have you tried in your code doing something like the following when you want to access it?

Dim objDict
Set objDict = session("user") 
Response.Write objDict("id")

Try that and see if it works. I wouldn't think this would be necessary but Classic ASP wasn't exactly the most robust language. What you want to do should be workable, since the Session object simply stores objects.

eidylon
When do you free this object ? What if 1000's of requests are executed ?
Edelcom
Well, If you're going this route, you should definitely issue a call to Set objDict = Nothing when you're done with it either right after using it, or if you have a "clean up" section in your code. Although to be honest, I do like AnthonyWJones' answer below... really whatever's being stored in this dictionary could just be stored in the session directly, generally speaking. Although, maybe the OP has a reason for doing it this way, I don't know.
eidylon
@eidylon: `Set objDict = Nothing` doesn't really achieve much in this case.
AnthonyWJones
Well, it would release the local handle on the variable, which theoretically should be released when the page finishes rendering anyway. But sure enough the root Dictionary object would still live in the session object. That should theoretically be released when the session dies.
eidylon
+3  A: 

The error you are getting is a remote procedure call error. I can't explain why you get this error or why extracting into a local variable fixes it.

However I can tell that its really bad idea. When you store an object such as this in the Session object you create an affiliation between the current thread executing the script and the session. As result all subsequent requests for that session must now be handle only by this specific thread. If that thread happens to be busy handling someone elses request the sessions request is queued even if there are plenty of available work threads that could be used. Hence storing an object in the Session can significantly damage the scalability of the app.

I'd also question the wisdom of storing a Dictionary in something which is already a dictionary?

Why not just user :-

Dim userID : userID = Session("user_id")

Where anything you would normally have stored in the "user" dictionary, such as "id", would simply have the prefix "user_" and stored direcly in the Session?

AnthonyWJones