1

I have many services extending MsSQLAbstractService class, each for single database. For my application I need to connect to the proper database based on a parameter in the request.

How can I pass such a parameter and make the SQL class use the service I want? I created my own SQL class, when passing the ID manually everything works, but I need to pass it from the request. How can I pass that param from request to SQL class?

Patrick
  • 4,720
  • 4
  • 41
  • 71
sysdba
  • 57
  • 5
  • Where do you get your parameter from? Is it passed with every request, or do you have to deduce it from the path of the request? – Patrick Jun 03 '19 at 14:48
  • I would like to get it from URL. Like subdomain or URL parameter &id=5 – sysdba Jun 04 '19 at 14:46

1 Answers1

0

Extracting the parameter

Depends on your requirements:

  • Service call from front end: Probably you could register a @Replace-Annotated ServiceTunnelServlet-subclass and extract it from the request there.
  • Alternatively, there also is a thread-local that keeps the request for service tunnel calls: IHttpServletRoundtrip.CURRENT_HTTP_SERVLET_REQUEST. If you can easily extract it from the URL or header: great
  • REST call to your backend: Extract it in the appropriate Servlet

Storing the parameter

You can wrap method calls in a ServerRunContext, and pass properties along that you can extract at other locations! See the ServerRunContext#withProperty(String,String) method.

You can create a new Context using the ServerRunContexts helper methods.

Using it

This part you already seem to have solved: You can use a single registered Service or appropriate @Order annotation to get the request and then dispatch them to the proper service based on the ID that you stored in the ServerRunContext.

Patrick
  • 4,720
  • 4
  • 41
  • 71