5

I am Trying to upload files continuously to SFTP server using 5 threads in java, at starting program uploads files correctly but after some time ,

All threads throws UnknownHostException when trying to create new session and Exception continues upto 5 to 10 minutes,after some time program works normally, i cant able to find what will cause for this Exception,

This is the code used for connecting sftp,

        JSch jsch = new JSch();
        jsch.setKnownHosts(host_file);
        session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();

Exception :

 at td.bdops.clupload.CARUpload.uploadZip(CARUpload.java:398)
    at td.bdops.clupload.CARUpload.uploadZip(CARUpload.java:398)
Caused by: java.net.UnknownHostException: sftp.opsbank2-prod.tio.systems
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.jcraft.jsch.Util.createSocket(Util.java:343)
    at com.jcraft.jsch.Session.connect(Session.java:215)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at td.bdops.util.FTPUtility.uploadAWSFTP(FTPUtility.java:227)
    at td.bdops.util.FTPUtility.uploadAWSFTP(FTPUtility.java:247)

can anyone please explain me, what is the root cause this error

radhakrishnan
  • 1,399
  • 1
  • 14
  • 20

2 Answers2

1

The problem is not a JSCH-specific one as you can see with the stacktrace because it happens within a class of the java.net package and not within jsch-classes. So testing with FileZilla doesn't help here very much.

The most likely reason I can see here is that the resolving of the name did fail at some point (unavailability of the internal DNS-server, etc.). Java has its own DNS-cache that works independently from the operating system so even if you can get a name resolved on the command line it still will be seen as unresolvable within Java. You can change the TTL-settings of that internal cache with the system properties networkaddress.cache.ttl and networkaddress.cache.negative.ttl. Setting one or both of these properties to 0 leads to the deactivation of that particular cache.

You might try out if the situation gets better if you deactivate the latter but you should try to find out what the source of the problem is, i.e. why you are unable to resolve the name at some time during the day because deactivating the cache comes with a price (namely loss of performance).

Lothar
  • 5,323
  • 1
  • 11
  • 27
1

All threads throws UnknownHostException when trying to create new session and Exception continues up to 5 to 10 minutes,after some time program works normally, i cant able to find what will cause for this Exception...

This is pretty self explanatory. Reading the javadocs for UnknownHostException:

Thrown to indicate that the IP address of a host could not be determined.

Looking at the code for AbstractPlainSocketImpl I see:

if (addr.isUnresolved())
   throw new UnknownHostException(addr.getHostName());

So your sftp.opsbank2-prod.tio.systems hostname does not resolve. That means that Java's name resolving code can't determine what the IP for that hostname is.

Here are some things to try:

  • Use the IP of that hostname instead of the name.
  • Use dig or host commands to lookup that hostname on that system to see if it resolves.
  • Try the following line of code to see if it works. It too should throw:

    new java.net.Socket("unknown.host.should.throw.com", 80).close();
    
  • Try your hostname now:

    new java.net.Socket("sftp.opsbank2-prod.tio.systems", 80).close();
    

If you see that your hostname is not resolving then you'll need to add it to the DNS configurations or /etc/hosts file. If you already have then there's something wrong with those files and you'll need to recheck your configuration.

Gray
  • 115,027
  • 24
  • 293
  • 354