i’m building some application, where i have to use memcached.
I found quite nice client:
net.spy.memcached.MemcachedClient
Under this cliend everything works greate except one – i have problem with close connection, and after a while i’m startign to fight with memory leak.
I was looking for possibility for close connection, and i foud “shutdown” method. But if i use this method like this:
MemcachedClient c = new MemcachedClient(new InetSocketAddress(
memcachedIp, memcachedPort));
c.set(something, sessionLifeTime, memcache.toJSONString());
c.shutdown();
I have problem with adding anything do memcached – in logs i see that this method is opening connection, and before it will add anything to memcached, it’s closeing the connection.
Do you have any idea, what to do?
Additionally – i found method: c.shutdown(2, TimeUnit.SECONDS); – which should close connection after 2 seconds, but i have connected jmx monitor to my tomcat and i see, that Memcached thread isn’t finished after 2 seconds – this thread isn’t finished at all…
The reason you are having an issue adding things to memcached like this is that the set(…) function is asynchronous and all it does is put that operation into a queue to be sent to memcached. Since you call shutdown right after this the operation doesn’t actually have time to make it out onto the wire. You need to call set(…).get() in order to make your application thread actually wait for the operation to complete before calling shutdown.
Also, I haven’t experience IO threads not dying after calling shutdown with a timeout. One way you can confirm that this is an actual bug is by running a standalone program with Spymemached. If the process doesn’t terminate when it’s completed then you’ve found an issue.