Delphi XE2 Datasnap Session Management - get session information after page reload
Asked Answered
V

4

6

I am trying to determine how to retrieve session information using a Delphi REST DataSnap server.

I know that, when on the same client page, you have access to the current thread session using the TDSSession method GetThreadSession.

What I want to do, however, is store data in the session (putData) and still be able to retrieve it when the user moves from page1 to page2. At present, if the user moves to a different page, the session is lost and a new one is created, thus loosing the data in the session that I had previously set.

I have tried playing with TDSSessionManager.SetThreadSession(sessionid) - but I cant seem to get it working.

I've reviewed the much acclaimed Marco Cantu white paper, however, it doesn't provide a solution to this issue.

Any help I can get on this would be great - even if its just 'hey, this topic is covered in book X'.

Thanks!

Viscometer answered 9/7, 2012 at 13:5 Comment(0)
A
1

The TDSSessionManager.SetThreadSession(sessionid) works with Session.sessionname.

Plus make sure your Lifecycle is set to Session (as stated by tondrej).

If you reconnect your client. a new session is started. So you want to keep your Datasnap connection open.

Or you can set the lifecycle to Server and mannage the client-sessions yourself.

Edit: Rest Servers are Stateless. So you need to store the page you are on on the Client. And Query the needed Page from the Server

Agustinaah answered 14/11, 2012 at 12:18 Comment(0)
T
1

You have to tweak the client side JavaScript to use a cookie to store session info.

See the last part of JavaScript Client Sessions

If you want to keep server side objects active for the session use the Session life cycle.

Trapani answered 26/7, 2015 at 3:30 Comment(1)
To remember the session ID between page loads, you need to enable the session cookie. To do this, for instance, call: initSessionData(true, "chat_");Trapani
W
0

I believe what you need to do is set LifeCycle property of your TDSServerClass instance to Session (stateful). From your question it seems you are currently using Invocation (stateless).

Winy answered 9/7, 2012 at 13:16 Comment(3)
Apologies - that would have been helpful information to have provided initially. The TDSServerClass does have its LifeCycle property currently set to Session.Viscometer
I think this should work out of the box if HTTP 1.1 KeepAlive connection is in effect.Winy
Out of the box, the REST applications use the TIdHTTPWebBrokerBridge as a server. By default, the KeepAlive property is 'false', so, following your suggestion, when the server is started, I am setting the KeepAlive property to 'true', however, the issue persists unfortunately.Viscometer
F
0

Well, in Datasnap REST (GET, POST, DELETE, PUT) if you set your TDSServerClass to session, as is a REST in this case session is the same as invocation, is stateless (http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Server_Class_LifeCycle#REST_Clients). It is right, you give the oportunity to all kind of clients to use your datasnap server with JSONs for example.

You need to create your owner model to session control to your REST server, or look for some framework to do this. In my case I use custom objects on lifecicle server (some cases with database too), and using tokens on request headers and other informations, I know if is the same client and I control too when the token expires and need to do new login, for exemple and I can give much more security too as on PUT resquests, only on records gave to client (it is only one case, but there are much others...). You need to resolve other way, not with classic way using TDSSession.

Firooc answered 22/10, 2019 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.