Thread and DB

If I have a thread and place some code in the run method like this:

Dim myrec As RecordSet myrec = Session.db.SQLSelect("SELECT * FROM SOMETABLE")

I get a NilObjectException when it hits that line and no idea why.

Any ideas or is this the wrong way to do it?

Thanks.

Did you call a db = New SomekindofDatabase before?

I have issue all the time like that. 95% of the time Im calling bad SQL.

I always call:

if myrec =nil then msgbox Session.DB.ErrorMessage end if

so I can see what i screwed up

Yes I call it on the session open method first. I cannot even check the error cause it will stop on that line no matter what.

Also the same exact code any other place will run without a problem.

As far as i understand In a thread webapps can loose their sessioncontext, especially when thread.sleep is used.
WebSessionContext lost error is shown in the messages pane during debugging.
If this problem occurs, the recordset will be nil because there is no session available for returning to.

Studio Stable’s Web Essentials addresses this problem directly.

[quote=68291:@Tom Russell]If I have a thread and place some code in the run method like this:

Dim myrec As RecordSet myrec = Session.db.SQLSelect("SELECT * FROM SOMETABLE")

I get a NilObjectException when it hits that line and no idea why.

Any ideas or is this the wrong way to do it?

Thanks.[/quote]

Session is probably nil
The thread probably needs it own connection to the database
Sharing connections across threads is bound to eventually cause you problems

Subclass thread, add a property to it (myDBConnection)
Then when you create the thread create & assign a NEW db connection to it and yer off