INT-1664, INT-1665 added isOpen() method to the Session strategy, added check for stale connection to the CachingSessionFactory which will ensure that it only returns valid (non-stale) Session
This commit is contained in:
@@ -25,7 +25,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link SessionFactory} implementation that caches Sessions for reuse without
|
||||
@@ -63,21 +62,23 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean {
|
||||
this.queue = new ArrayBlockingQueue<Session>(this.maxPoolSize, true);
|
||||
}
|
||||
|
||||
|
||||
public Session getSession() {
|
||||
Assert.notNull(this.queue, "SftpSession is unavailable since the pool component is not started");
|
||||
this.lock.lock();
|
||||
try {
|
||||
Session session = this.queue.poll();
|
||||
if (session != null && !session.isOpen()){
|
||||
this.queue.remove(session);
|
||||
session = null;
|
||||
}
|
||||
if (null == session) {
|
||||
session = sessionFactory.getSession();
|
||||
}
|
||||
|
||||
return (session != null) ? new CachedSession(session) : null;
|
||||
}
|
||||
finally {
|
||||
this.lock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
@@ -96,7 +97,7 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean {
|
||||
}
|
||||
catch (Throwable e) {
|
||||
// log and ignore
|
||||
logger.warning("Exception was thrown while destroying SftpSession. " + e);
|
||||
logger.warning("Exception was thrown while destroying Session. " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +134,10 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean {
|
||||
public void copy(InputStream inputStream, String destination) throws IOException{
|
||||
this.targetSession.copy(inputStream, destination);
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return this.targetSession.isOpen();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,4 +40,6 @@ public interface Session {
|
||||
void copy(InputStream inputStream, String destination) throws IOException;
|
||||
|
||||
void close();
|
||||
|
||||
boolean isOpen();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user