ConBran
ConBran

Reputation: 379

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

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!

Upvotes: 6

Views: 5045

Answers (4)

Roberto Novakosky
Roberto Novakosky

Reputation: 371

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.

Upvotes: 0

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.

Upvotes: 1

r_j
r_j

Reputation: 1368

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

Upvotes: 1

Ondrej Kelle
Ondrej Kelle

Reputation: 37211

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).

Upvotes: 0

Related Questions