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

EJB questions  RSS feed

 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to EJB.I use stateless sessionbean.
I have the below questions.Could anyone give me your suggestions / comments?

1.My ejb method calls a Data Access Object class's method. In my Data Access Object's class method can I catch Exception? Is this ok?

2. My ejb method performs single row insert into database.Is it ok if i set the transaction attribute to NotSupported?

Thanks in advance,
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using NotSupported means that there is no guarantee that your method will run in a transaction. As you should certainly be in a transaction for any DB changes, then a more appropriate transaction attribute is Required.
 
Andreas Schaefer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your DAO can do whatever it wants but that can lead to some problems. You need to determine what should be done when an exception is thrown. In addition the calling EJB does not know about a problem and cannot handle it.
I would suggest that you catch the Exception in the DAO, raise an exception that is appropriate to situation of the DAO and insert the original exception as the cause (see Throwable.setCause()). This way the EJB does not have to know anything about problems coming from the DB but still is able to handle it appropriately.

With regards to your second problem I would aggree that you should have a transactional context set up (Required, RequiresNew, Mandatory). This way a system exception (runtime exception, remote exception, error) that is uncaught by the SLSB will make the transaction to be rolled back (it also does dump the EJB instance). With one DB you can run in a non transactional context but that can lead to problems when you add a second transactional object (DB, JMS etc) or when the EJB is called by another EJB with a transactional context. What can happen is that your DB call is committed into the DB but the rest of execution fails to commit.

-Andy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!