简体   繁体   English

Java Restful Web服务-静态成员变量

[英]Java Restful Web Service - Static Member Variables

I'm creating a web service and I need to map a bunch of String names to set of database names. 我正在创建一个Web服务,我需要将一堆字符串名称映射到一组数据库名称。 That is, a request will arrive with a data item, and I need to figure out which database it maps to. 也就是说,一个请求将与一个数据项一起到达,我需要弄清楚它所映射的数据库。

To create this mapping, I need to query the database also, preferably only once. 要创建此映射,我还需要查询数据库,最好只查询一次。 I want to be able to access this mapping whenever requests come in and not have to construct my map on each request. 我希望能够在请求到来时访问此映射,而不必在每个请求上构造我的映射。 What I don't want, is to have to query the database 10 times every 10 seconds, which is potentially what could happen, in this case. 我不希望的是必须每10秒查询数据库10次,在这种情况下,这可能会发生。

I considered storing the map as a static member variable upon the first request. 我考虑在第一次请求时将地图存储为静态成员变量。 So my question is - are there any known issues with having this map as static and sharing across requests? 所以我的问题是-将地图设为静态并在请求之间共享是否存在任何已知问题?

As long as you only fill this static structure at startup (and don't expect it to change during the lifetime of the service) there are no issues with this. 只要您仅在启动时填写此静态结构(并且不要期望它在服务的生命周期中发生变化),就不会有任何问题。 Specifically the only situations where this might be a problem are: 特别地,这可能是一个问题的唯一情况是:

  • If the map is very big and it doesn't make sense to include hold all of it in memory 如果地图很大,并且将所有内容都保存在内存中没有任何意义
  • If the map is changing often 如果地图经常变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM