由於spring的只讀模式所引起的錯誤

使用的框架是ssh.

今天在使用hibernate的HibernateTemplate模板進行插入操作事報錯了,錯誤如下:

Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition

意思是隻讀模式下寫的操作是不允許的。

這是因爲在spring中readonly這個屬性默認是true,即默認是隻讀模式,所以我們要修改它的爲false。

有兩種方法:第一種是直接使用註解的方式,直接在DAO層的實現類上加上如下注釋即可(在方法上添加也可以):

 

@Transactional(readOnly = false)

第二種是在配置文件裏面配置readyonly爲false

 

!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
 <!--事務的策略-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!--在這裏配置read-only="false"-->
        <tx:method name="*"  propagation="REQUIRED" read-only="false" />
    </tx:attributes>
</tx:advice>
<!--事務的aop-->
<aop:config>
    <aop:pointcut expression="execution(* com.mysql.dao.*.* (..))"
                  id="mypointcut" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut" />
</aop:config>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章