Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session bean lifetime

 
David Fahy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a session bean that performs encryption/decryption of credit card numbers and then sends the data to an entity bean to be written to the database. When the session bean is created a key is read from the database (in ejbcreate method) and used in the encryption/decryption process. I am just wondering if the key will be present in the bean for its lifetime for each method call or will I have to read the key from the database each time I want to do encryption/decryption. Any help is appreciated.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Fahy:
I am just wondering if the key will be present in the bean for its lifetime for each method call or will I have to read the key from the database each time I want to do encryption/decryption. Any help is appreciated.

If you store the key in an instance member of your session bean, then *that* session bean instance will not need to hit the database again. If you store the key in a class member, then it will be available to *every* bean instance. This is the textbook case for using members in session beans.
One design enhancement would be to factor out the code that reads the key into a helper class and use that helper from the session bean. That way if more session beans or other objects need the service it will already be available.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic