Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java mail - session cache?  RSS feed

 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Roberto Perillo
Bartender
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's slide this over to the Other JSE/JEE APIs forum!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!