Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewFilter and Hibernate Related Exceptions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewFilter and Hibernate Related Exceptions

    I have a question regarding OpenSessionInViewFilter and Exceptions from Spring's Hibernate support classes.

    I am using the OpenSessionInViewFilter for the lazyload issues that I have encountered due to the fact that we are using composite primary keys and trying to display data in a jsp. (We use JSF for presentation)

    I have a DAO class that extends HibernateDaoSupport. Typically I would perform Hibernate operations like this:

    Code:
    //just a simple example of hibernate save method.
    public void save(TbDepartment transientInstance) {
       try {
    	   getHibernateTemplate().save(transientInstance);
       	   log.debug("save successful");
       } catch (RuntimeException re) {
          log.error("save failed", re);
          throw re;
       }
    }
    The problem is, when I set SingeSession to "true" in my web.xml, it seems like I cannot trap the exceptions anywhere in my code. I see the exceptions in the weblogic 9.2 log, however I would like to trap them and display in my jsp's.

    When I set SingleSession to "false", I can trap exceptions as usual.

    Is there anything I am doing wrong here?

    Thanks

  • #2
    You probably misconfigured your transactions. Also You probably don't want to catch the exceptions in the way/place you do.

    Comment


    • #3
      Originally posted by mdeinum View Post
      You probably misconfigured your transactions. Also You probably don't want to catch the exceptions in the way/place you do.
      Thanks for the quick reply.
      Can you elaborate on your points?

      I am quite a newbie at Spring/Hibernate.

      Thank You

      Comment


      • #4
        Normally a transaction is committed after execution of your dao method (not to be confused with your hibernatetemplate call). So your try/catch is well pretty useless.

        Code:
        - start transaction
        - save
        - commit transaction
        Also transactions should be defined at your service layer as a good practice.

        So the actuall catch should be in your calling code and not inside your dao method. However a better solution (if you only want to log the messages) is to create an Aspect which reacts on DataAccessExceptions and logs a message.

        Comment


        • #5
          Originally posted by mdeinum View Post
          Normally a transaction is committed after execution of your dao method (not to be confused with your hibernatetemplate call). So your try/catch is well pretty useless.

          Code:
          - start transaction
          - save
          - commit transaction
          Also transactions should be defined at your service layer as a good practice.

          So the actuall catch should be in your calling code and not inside your dao method. However a better solution (if you only want to log the messages) is to create an Aspect which reacts on DataAccessExceptions and logs a message.
          Actually what I really want is to display some user friendly exception message on the jsp.

          As far as singleSession=true, is there a reason that the exception aren't being thrown in my DAO, as opposed to when I have it set to false?

          Comment


          • #6
            As I said before it has ALL to do with your transaction management. Can you post your transaction configuration?

            Comment


            • #7
              Originally posted by mdeinum View Post
              As I said before it has ALL to do with your transaction management. Can you post your transaction configuration?
              I am sorry for my ignorance.
              Still trying to get a handle on all of this!

              If by transaction configuration you mean what I have set up in my applicationContext.xml for spring, then here it is. (Pretty much a copy and paste from a sample app I had seen)

              Code:
              <bean id="addSponsors" class="com.natixis.blr.web.jsf.model.AddSponsorsModel">
                 <property name="tbProjectDepartmentDAO">
                    <ref local="TbProjectDepartmentDAO" />
                 </property>
                 <property name="tbDepartmentDAO">
                    <ref local="TbDepartmentDAO" />
                 </property>		
              </bean>
              	
              <bean id="addSponsorsModel" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                 <property name="transactionManager">
                    <ref local="transactionManager" />
                 </property>
                 <property name="target">
                    <ref local="addSponsors" />
                 </property>
                 <property name="transactionAttributes">
                    <props>
                       <prop key="save*">PROPAGATION_REQUIRED</prop>
                      <prop key="remove*">PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                    </props>
                 </property>	
              </bean>
              Also, I realized that I have not integrated my JSF stuff with Spring. So I changed around my code a little bit essentially by adding another layer. This layer communicates with the DAO now instead of my JSF bean. (The "addSponsors" bean above is an example of this)

              Something like this:
              JSF Bean -> New Layer -> DAO

              Anyways, I am seeing something different now. Now I am seeing the error displayed on the webpage. Before the integration, the error message would only appear in the log.

              Not sure if this means I am getting closer.

              Again, sorry for my ignorance. I feel like my brain is about to explode!

              Comment


              • #8
                OK, I was correct in saying that I was getting closer.

                I had put my try..catch in the wrong spot. (In my spring managed bean)
                Once I put it in the JSF managed bean, I got things to work. Not sure if I am making any sense here...

                But the key as you said numerous times was setting up the transactions, which was not possible until I integrated my JSF code with Spring.

                Thanks!!!

                Comment

                Working...
                X