Jersey client closes connection on exception?

I read the Jersey documentation and it says that Jersey automatically closes the connection after reading the object (e.g. response.readEntity (SomeObject.class))

But when an exception is thrown, either a failed request or a socket timeout, does Jersey automatically close the connection, or should I have a finally clause that calls client.close ()?

+5
source share
1 answer

Not. Also, Jersey does not call client.close() in the event of an exception, and does not make JerseyClient AutoCloseable .

You can easily check it out. The client throws an IllegalStateException if you call the method after closing:

 Client client = ClientBuilder.newClient(); client.close(); client.target("http://stackoverflow.com").request().get(); // IllegalStateException 

But you can call the method after throwing an exception:

 Client client = ClientBuilder.newClient(); try { client.target("http://foo.bar").request().get(); // java.net.ConnectException: Operation timed out } catch (Exception ex) { client.target("http://stackoverflow.com").request().get(); // works } 

So closing is your job.

Update: JAX-RS 2.1 will use AutoClosables .

+6
source

Source: https://habr.com/ru/post/1201849/


All Articles