users@glassfish.java.net

Re: best way to handling ejb exceptions

From: Marina Vatkina <marina.vatkina_at_oracle.com>
Date: Wed, 07 Apr 2010 17:04:51 -0700

Try adding em.flush() after em.persist(). The problem you are facing (google for
it - there will be plenty of discussions on that) is that the actual database
insert happens only at commit time, i.e. in the EJB container outside of your code.

Keep in mind that calling em.flush() after each em operation can take too much
time if you do it too often in a single transaction.

Regards,
-marina

mSephiroth wrote:
> Hi, I dont know if this is the correct forum to post this but i have a
> situation.
>
> Im working on Glassfish v3, jee 6, jsf 2 and netbeans 6.8.
>
> I have a package with session beans that access to the database (facades),
> and a package with session beans that manage the bussines rules(bussines).
> The situation is when i have a error in the facades beans like duplicated
> key on the Database, the error is catch by a DataExcepcion create by me, but
> the excepcion jump until the managedBeans on the web tier and shows and
> EJBexcepcions, the DataExcepcion dont work, can you tell me a way to handle
> this exceptions??
>
> -------- The facade bean:
>
> @Stateless
> public class SisUsuarioFacade {
>
> @PersistenceContext(unitName = "ELearningIATIC_EE-ejbPU")
> private EntityManager em;
>
> public void create(SisUsuario sisUsuario) throws
> DataAccessLayerException {
> try {
> em.persist(sisUsuario);
> } catch (Exception e) {
> throw new DataAccessLayerException("Clase: " +
> SisUsuarioFacade.class.getName() + " Método: create(SisUsuario
> sisUsuario)");
> }
>
> }
> }
>
> ---------- the Bussines bean
> @Stateless
> @LocalBean
> public class UsuarioServicio {
> @EJB
> private SisUsuarioFacade sisUsuarioFacade;
>
>
> public void crearUsuario(SisUsuario usuario, SisRol rolUsuario) throws
> ServiceLayerException, DataAccessLayerException {
>
> sisUsuarioFacade.create(usuario);
> }
> .....
> }
>
>
>
> thanks