Discussion:
[JSch-users] Lifecycle of Sessions and SftpChannels
Andy Yang
2014-01-28 16:01:39 UTC
Permalink
Hi,

A general question that isn't obvious to me from scanning the available sample code:


What is the convention for connecting/disconnecting Channels and Sessions? I got the impression that sessions and channels could be long-lived, but testing my code is suggesting that this might be a mistake.

As a simple example, if I want to download a list of files, should it be (in sort-of-pseudo-code):
        session = jsch.getSession(userId, server, 22);
        session.connect();
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();

        for (String fname : fileList) {
            channel.get(fname, destPath);
        }

        channel.disconnect();
        session.disconnect();

Or should it be:

        session = jsch.getSession(userId, server, 22);
        session.connect();

        for (String fname : fileList) {
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            channel.get(fname, destPath);
            channel.disconnect();
        }
        session.disconnect();

And should I be using setServerAliveInterval() and setDaemonThread() on the session?

I'm finding that my initial approach of keeping a single session and a single sftp channel over multiple operations results in intermittent 'inputstream closed' and 'pipe closed' errors.

Thanks,
Andy

Loading...