views:

170

answers:

1

I am migrating a Java RMI application to Java Web Service (school assignment) and I've encountered an issue...

Currently my Java Server creates an instance of the Remote Object, this object has a constructor and takes a parameter (int ID) which tells it which database to load in memory - works like a charm ...

Now, migrating this to Web Services is causing my a problem - first I needed to add a default constructor because it wouldn't deploy without it, and then while doing some reading all these discussions about "stateless web services" kept coming up ...

For example, if I "start" my webservice with parameter(0) it would load from Databse 0 and all requests from Clients would be done using that data... I want this to only happen when I start the WebService and NOT everytime the client connects... Loading from the DB is expensive and takes time, so I want to do it once so that clients when they connect just deal with the data in memory ...

This is how it works with my Java RMI .... but can this also work with Web Services?

Any advice would be much appreciated. Thanks,

A: 

Hey Shaintan00,

Perhaps you should consider splitting your presentation from the backing service. Consider the WebService simply a presentation layer and the database processing the service being presented. The web service should really just be referencing the database via a handle to a single instance rather than actually being that instance. This means that the "state" is then held in the database instance not the web service.

One way to consider this (although I would not suggest that you implement it this way) is to consider the Web Service as the RMI client of the database rather than the RMI server service.

Hope this helps.

Kind regards,

Malcolm

Malcolm Featonby
How would the WebService get a reference to the correct database? I want to have multiple web services running (one for each database) that get defined by the parameter I pass into the WebService - so I don't splitting them up but I still need to tell each Web Service which DB (or reference to DB) it needs to access ...Is there a way to have a server "start' a web service instead of doing it on deploy?
Shaitan00
You could consider having a database lookup class (you could get complicated but simply this could be a set of named "database" instances in a Hashmap) with the lookup creating the database instance of it is not present in the Hashmap. Depending on whether you are naming the database in the web service request you could either have the web service request the database named in the message or alternatively, publish a web service per database instance if not named in the request.(I am assuming that data caching is a requirement of your solution and so not questioning your approach :) )
Malcolm Featonby