How to save an object created between multiple requests using Java (Glassfish) SOAP requests

I went around this topic a lot, but all I could find was related to connecting to the database, etc.

I am creating a web service that downloads a Shapefile and uses the GeoTools library to make some funky files based on location. What I would like to do is load the Shapefile once, get all the “functions” in memory for the Shapefile, and then just be able to check this collection every time - note that a huge amount of shapefile files are currently loaded.

I wrapped my Geo-based material in the class by loading shapefiles into the collection when I instantiated the class. My @WebService class checks to see if myGeoClass is created, otherwise it creates it and loads the files into memory. These files never change, so I would like to keep the same instance of the object between several requests, but I added some traces and it seems to create a new instance with each request.

Is there a way to keep one instance in memory common to all requests?

Hooray!

+4
source share
1 answer

At least you can use a static variable.

If this is a web application, you can set it as a ServletContext attribute.

If you are using Java EE 6, you can define your class as a managed bean @ApplicationScoped or Singleton

+1
source

Source: https://habr.com/ru/post/1434278/


All Articles