When are servlet static variables gced in gae?
I'm trying to have a GAE app that all it does is register users and associate contacts with them, and return their online status. If online return their IP. 开发者_如何学编程So i've a JPA entity with the userid and contacts list, and a transient ip field. Every time a user logs in I keep it in a map.
private static final Map<String, SSUser> connectedRegisteredUsers = new HashMap<String, SSUser>();
Now the surprise is that it seems after a while the map is emptied, and will forget the users. I've no idea why that might be happening, how to verify it (beside from observing the behavior), and alternative exists, beside storing the IP in the datastore, too.
App Engine can start up and shut down instances of your app at any time, in response to changing traffic patterns. There's two issues here: First, your app will be running multiple distinct instances, on multiple machines, each of which has separate state (including your static variables). Second, instances can be shut down and started up at any time, clearing the static variable.
If you need globally consistent data, you need to use memcache and/or the datastore.
精彩评论