4 Replies Latest reply on Jul 3, 2009 2:24 PM by Samuele Sanvito

    Raise exception after calling stored procedure

    Wouter van Reeven Newbie

      Hi all,



      In our EntityHome subclasses we need to call Oracle stored procedures to persist data to our Oracle database. Using code like this



      HibernateSessionProxy hsp = (HibernateSessionProxy)getEntityManager().getDelegate();
      CallableStatement stmt = null;
      Connection conn = null;
      ConnectionManager conManager = null;
      try {
           conManager = hsp.getJDBCContext().getConnectionManager();
           conn = conManager.borrowConnection();
           stmt = conn.prepareCall("{call OURPROCEDURE(?,?,?)}");
           stmt.setObject(1, getInstance().getId());
           stmt.registerOutParameter(2, Types.VARCHAR);
           stmt.registerOutParameter(3, Types.VARCHAR);
           stmt.execute();
           message = stmt.getString(19);
           failed = stmt.getString(20);
           if (failed.equals("TRUE")) {
           } else {
                getEntityManager().flush();
                assignId(newId);
                createdMessage();
                raiseAfterTransactionSuccessEvent();
                return "persisted";
           }
      } catch (SQLException e) {
      
           System.out.println("Fout: " + e.getMessage());
      } finally {
           if (conn != null) {
                try {
                     conManager.releaseBorrowedConnection();
      } catch (Throwable th) {
                System.out.println(th.getMessage());
      }
      }
      }
      return "";
      




      The question is what to put in the if (failed.equals(TRUE)) block. If I add a faces message it will show in the page that is loaded after the submit button is clicked. However, our users get redirected to an overview page.


      How can I make sure that the page in which the user enters a new entity is loaded again with the entered values and a message specifying the error returned by the procedure?



      Thanks in advance for your help.



      Cheers, Wouter van Reeven