OpensessionInView的FlushMode问题

   在整合S2SH的时候用到了OpensessionInView,不料在添加信息的时候却出现如下错误:

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

 

  后来通过网上查询得知,OpensessionInView在获取session的时候首先将FlushMode设置为NEVER,这样获取到的session被OpensessionInView掌管时成为只读,因此要添加数据就会抛异常,可以采用spring的事务声明,使方法受transaction控制:

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice transaction-manager="txManager" id="txadvice"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="add*"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(public * com.spl.business.impl..*.*(..))" id="txbusiness"/> <aop:advisor advice-ref="txadvice" pointcut-ref="txbusiness"/> </aop:config>

 

 

你可能感兴趣的:(spring,bean,session,Class)