jakarta-eejakarta-mailmulti-tenant

Are there advantages of server defined jndi MailSession over runtime creation?


I have multiple MailSessions, one for each of our client (domain) saved in our glassfish server admin panel which are injected via jndi resource. For simplification in this example they are hardcoded. We retrieve the correct session by jndi lookups.

// mail session for domain "foo.com"
@Resource(name = "mail/fooCom")
private Session mailSessionFooCom;


// mail session for domain "bar.com"
@Resource(name = "mail/barCom")
private Session mailSessionBarCom;

Depending for which client we want to send a mail the specific Session instance is selected.

This is not very configuration friendly, since for new clients we need to add a new mail session into our admin console of glassfish and restart server.

Instead we could save the mail properties for each client in our database and simply create a instance of Session at runtime whenever we send a mail.

Are there any drawbacks doing this at runtime? What are the advantages of JNDI bound Mail sessions?


Solution

  • There is no single answer to that as "it depends" on particular case and usage scenario.

    Advantages of the server defined:
    - you dont depend on external resource (DB in your case)
    - app code is simpler, just accessing JNDI instead of querying db and creating mail objects in the code
    - if you have 10 different apps accessing same mail server e.g. for sending, you just have 1 mailsession instead of 10

    Advantages of runtime defined:
    - dont depend on server configuration (but might have issues when SSL connection is used and certs not available by default in server)
    - create "new" sessions on the fly

    So if you dont need to add new domains daily, I'd probably stick with the server config, but if managing mail sessions is crucial for your app, it might be better for you to handle it all in the app ;-)