Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?

Java docs of close() method of java.lang.AutoCloseable says

Note that unlike the close() method of Closeable, this close() method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike Closeable#close() which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.


What do they mean by idempotent method and what are the side effects of calling this close() method twice?

And since interface Closeable extends AutoCloseable why are the side effects not to be seen in the close of Closeable interface?

like image 305
Aniket Thakur Avatar asked Oct 11 '13 14:10

Aniket Thakur


People also ask

What is idempotent method in Java?

Idempotent means that you can apply the operation a number of times, but the resulting state of one call will be indistinguishable from the resulting state of multiple calls. In short, it is safe to call the method multiple times.

What is idempotent method in rest?

An idempotent method means that the result of a successful performed request is independent of the number of times it is executed.

What do you mean by idempotent operation?

Idempotence, in programming and mathematics, is a property of some operations such that no matter how many times you execute them, you achieve the same result. In programming, idempotence can be a property of many different code elements, including functions, methods, requests and statements.

Why is idempotent important in rest?

Idempotency is important in APIs because a resource may be called multiple times if the network is interrupted. In this scenario, non-idempotent operations can cause significant unintended side-effects by creating additional resources or changing them unexpectedly.


2 Answers

Idempotent means that you can apply the operation a number of times, but the resulting state of one call will be indistinguishable from the resulting state of multiple calls. In short, it is safe to call the method multiple times. Effectively the second and third (and so on) calls will have no visible effect on the state of the program.

So if you close this object once, and it closes, you don't have enough information to know if it is idempotent. However, if you close it twice, and the first time it closes, but the second time it throws an exception, it is clearly not idempotent. On the other hand, if you close it once, and close it twice, and the second closure results in the item remaining closed in the same manner (perhaps it is a noop), then it is idempotent.

One technique of making an idempotent Closeable could be:

public class Example implements Closeable {    private boolean closed;    public Example() {     closed = false;   }    public void close() {     if (!isClosed()) {       closed = true;     }   }    public boolean isClosed() {     return closed;   } } 

Where it is now obvious that if close() is called once or multiple times, all returns of the state through isClosed() will forever return true. Therefore, the method close() would be considered idempotent.

like image 78
Edwin Buck Avatar answered Oct 04 '22 14:10

Edwin Buck


Explanation of Concept Without Code

Einsteins definition of indempotency

To adopt Einstein's aphorism, if you do the same thing, and get different results, then the method is not idempotent.

Example of idempotency

"Please sir, can I have a pay rise?"  "No" 

Same result every time. Asking for a pay rise is an idempotent operation.

Examples with HTTP Requests:

  • Making a get request: If properly implemented then no matter how many times you make this request, you will get the same response. e.g. I make a get request to view my bank balance and it's the same. everytime. Unlike the game monopoly, I've never had an extra $100 magically added to my bank balance.
  • An operation that is not idempotent, for example, would be making a delete request to destroy a resource - every time you do this you will be changing state of the application. For example, the IRS keeps making delete requests to my bank account - and every-time they do so the "state" of my bank account depletes and is halved. It is an idempotent operation.
like image 29
BenKoshy Avatar answered Oct 04 '22 15:10

BenKoshy