Timo Ernst
Timo Ernst

Reputation: 15973

JavaMail Store connection: Connect on demand or keep-alive?

What does make more sense for JavaMail?

  1. Create a Store connection for each request to the email server and then close it immediately after the task has been completed.

  2. Open a Store connection and only re-connect if the connection got closed by the server or the local application got terminated.

While (1) can lead to many connect() and close() calls in a row, (2) can block resources although that wouldn't be really necessary (but IMAP/POP3 calls would be fast since no extra .connect() call is required).

Upvotes: 4

Views: 1774

Answers (1)

Puspendu Banerjee
Puspendu Banerjee

Reputation: 2651

Connect on Demand is better unless you have a requirement to poll the server frequently from a single thread.

Upvotes: 4

Related Questions