Are you sure you catch the HibernateException?

On 4/4/07, oguzhan tortop <[EMAIL PROTECTED]> wrote:


hi all,
i am using struts2 with hibernate i have a basic form which inputs the
given
data via hibernate. I get the hibernate session from my custom hibernate
interceptor and from that interceptor i call the action which creates the
hibernate transaction with data from its form and after that i return the
success case from action but there is a finally statement in my hibernate
interceptor which commits the transaction that's data filled in my action
class. But for ex. if the data is exist in db before which means duplicate
then it gives an error and when i get this error i return and ACTION.ERROR
from my hibernate interceptor but what i see on the page is the SUCCESS
result from the action class. What could be the problem  ? :) Any help
will
be usefull thanks for your support
Here is my default stack and action conf.
         <interceptor-stack name="defaultStack">
                   <interceptor-ref name="exception"/>
                   <interceptor-ref name="servlet-config"/>
                   <interceptor-ref name="timer"/>
                   <interceptor-ref name="logger"/>
                   <interceptor-ref name="static-params"/>
                   <interceptor-ref name="params"/>
                   <interceptor-ref name="hibernate"/>
           </interceptor-stack>


        <action name="CreateService"
class="tr.com.mkk.admin.action.CreateServiceAction">
               <result name="input" >
                       <param
name="location">/admin/createService.jsp</param>
               </result>
               <result name="success" >
                       <param
name="location">/admin/viewService.jsp</param>
               </result>
               <interceptor-ref name="validation"/>
               <interceptor-ref name="validatorErrors"/>
               <interceptor-ref name="defaultStack"/>
       </action>
Here is my interceptor class :

public String intercept(ActionInvocation invocation) throws Exception {
               Action action = (Action)invocation.getAction();
               if ( !(action instanceof BaseActionSupport) ) return
invocation.invoke();
               if (null == factoryName || null == packageName)
                       throw new IllegalArgumentException("factoryName and
packageName
parameters should be given \n" +
                                       "check parameters in xml file
<param name='factoryName'> \n" +
                                       "and <param
name='packageName'>\n");

               // adds factory to cache if not available
               HibernateSessionFactory.addFactory(factoryName,
packageName,
extConfigFilePath);

               HibernateSession hs = new HibernateSession();
               hs.setFactoryName(factoryName);
               ((BaseActionSupport)action).setHibernateSession(hs);

               try {
                       result = invocation.invoke();//calls the action
here it creates
transaction data
               }

               // Note that all the cleanup is done
               // after the view is rendered, so we
               // have an open session in the view

               catch (Exception e) {
                       hs.setRollBackOnly(true);
                       if (e instanceof HibernateException) {
                               LOG.error("HibernateException in
execute()", e);
                               return Action.ERROR;
                       }
                       else {
                               LOG.error("Exception in execute()", e);
                               throw e;
                       }
               }

               finally {
                       try {
                               hs.disposeSession();//commits transaction ,
and closes hibernate session
                               return result;//expected result when an
exception occurs with hibernate
operations
                       }
                       catch (HibernateException e) {
                               LOG.error("HibernateException in
dispose()", e);
                               return Action.ERROR;
                       }
               }
       }

---------------------Action
Class----------------------------------------------------------
public class CreateServiceAction extends BaseActionSupport {

       private Service service;

       public CreateServiceAction() {
               service = new Service();
       }

       public String execute() throws Exception {
               ServiceManager.getInstance().createService(getSession(),
service);//gets
the hibernate session and sends it to the serviceManager object which
creates transaction
               set("service", service);
               return SUCCESS;
       }

       public Service getService() {
               return service;
       }
}

*********

--
View this message in context:
http://www.nabble.com/Struts-2-exception-problem-tf3525255.html#a9835135
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to