2010年1月11日 星期一

Hibernate OpenSessionInView

1.設了 OpenSessionInViewFilter 後, mysql裡的connection pool都沒作用, 連線一直增加
       
            把 singleSession 改成 true
            但web.xml裡的singleSession 設為true後, 會有交易的問題, 去sping-hibernate.xml裡, 有查詢類的method要加上readonly,
            並把有用到的dao加入org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator

            org.springframework.dao.InvalidDataAccessApiUsageException:
            Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL):
            Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

2.把PageViewMonthDAO加入交易後, 會出現下列問題
            org.springframework.orm.hibernate3.HibernateSystemException:
            a different object with the same identifier value was already associated with the session:
             [com.xxx.db.hibernate.pojo.PageViewMonth#200912];
           
             nested exception is org.hibernate.NonUniqueObjectException:
             a different object with the same identifier value was already associated with the session:
             [com.xxx.db.hibernate.pojo.PageViewMonth#200912]

            改成 getHibernateTemplate().merge();
           
            session.merge() 方法的註釋:
            Copy the state of the given object onto the persistent object with the same identifier.
            If there is no persistent instance currently associated with the session,
            it will be loaded. Return the persistent instance.
            If the given instance is unsaved, save a copy of and return it as a newly persistent instance.
            The given instance does not become associated with the session.
            This operation cascades to associated instances if the association is mapped with cascade="merge".
            The semantics of this method are defined by JSR-220.

0 意見: