• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java mail - session cache?

 
Ranch Hand
Posts: 36
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in general when we want to send a java mail we make a new java mail session

something like :-



This works fine. My question is for an enterprise project can we cache the mailSession object?

does it have a time out?

is it wise to cache it?

If cached - is there some function to call every time sending to see if it needs to re-initialized?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's slide this over to the Other JSE/JEE APIs forum!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If by "an enterprise project" you are referring to a web application, then yes, that's the normal thing to do. All of them (Tomcat, Websphere, etc) have a way to configure a mail session which can be accessed via JNDI.
 
Tushar Kapila
Ranch Hand
Posts: 36
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeap i mean a web app with a lot of tx that need emails to be sent out ...

do i need to put it in jndi?

i have a class that sends emails now - cant i just put in as a static member of this class?

and more importantly - once we get the session from JNDI/static member - do we ever need to re init if its the same host/port as before?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to use any of the features of the container if you think you can reimplement them in your own code. Just be aware that the container code will have been written to take care of annoying details like having to deal with simultaneous access from multiple threads and reconnection after failures and all those other issues that you didn't acknowledge in your post.

Actually, sorry, you do acknowledge some of those issues:

Tushar Kapila wrote:and more importantly - once we get the session from JNDI/static member - do we ever need to re init if its the same host/port as before?



If you get a session from JNDI you don't need to deal with any of those issues. Unless you were proposing to keep a long-running reference to the session, of course. But don't do that. If you want to send an e-mail, then get a session from JNDI and use that to send the e-mail. Don't keep a reference to that session.

If you roll your own session, then I don't know.
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic