Reputation: 679
I have done a lot of looking around on this topic but all I could find related to database connections etc.
I'm building a web service which loads a Shapefile and uses the GeoTools library to do some funky location based stuff. What I'd like to do is load the Shapefile once, get all the 'Features' in memory for the Shapefile, and then just be able to check against that collection each time - note there are actually many shapefiles loaded now.
I've wrapped my Geo-based stuff in a class, with it loading the shapefiles into a collection when the class is instantiated. My @WebService class checks to see if myGeoClass is instantiated, else it creates it and loads the files into memory. These files never change, so I'd like to keep the same instance of the object between multiple requests, but I've added some traces in and it seems to be creating a new instance in with every request.
Is there a way to keep the single instance in memory, shared across all requests?
Cheers!
Upvotes: 4
Views: 764
Reputation: 877
At the very least you could use a static variable.
If it is a web app , then you can set it as an attribute of the ServletContext.
If you are using Java EE 6 then you can define your class as a managed bean @ApplicationScoped or a Singleton
Upvotes: 1