How to handle ice exceptions correctly? #1533
-
As we all know, the ice communitor is a heavyweight object,when we call a method and an exception is caught, should we destroy it each time and recreate it? Is there a better way? I didn't find a better way to do it from the demo, my code looks like this:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Typically, you should initialize the communicator at the beginning of your application and destroy it when the application exits. However, you don't need to destroy the communicator if an exception occurs during an invocation. |
Beta Was this translation helpful? Give feedback.
-
The connection establishment and reuse logic is described here: In your situation, what happens depends on the number of factors:
The "best" scenario here is the connection is closed OR the proxy is configured to use a different connection for each invocation, and the locator returns multiple endpoints. In this situation, it's likely your next invocation will open (or reuse) a connection to another server. On a side note, we reworked and simplified this logic in IceRPC, where everything is much more modular:
ConnectionCache always tries server addresses (the new name for endpoints) in order, while Communicator tries endpoints at random by default (see EndpointSelection). |
Beta Was this translation helpful? Give feedback.
As @pepone wrote, you should only create a single Communicator per application.
In particular, if you catch an Ice exception and this exception is something your application can tolerate, just continue. You should not recreate your Communicator. And you should not close any connection manually in your code either. Ice takes care of opening/closing connections for you. Just keep going.