SpringMVC4+hibernate遇到 readOnly問題FlushMode.MANUAL

錯誤類型:web

HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): 
Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

嘗試解決方案 1:在web.xml下配置以下代碼(行不通)spring

<filter>
        <filter-name>openSession</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name> flushMode </param-name>
            <param-value>AUTO </param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSession</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2:利用spring的事物管理機制(行不通)express

<!-- 聲明一個切面 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="update" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="get*" propagation="REQUIRED" read-only="true" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>
    <!-- 把切面注入到業務中 -->
    <aop:config>
        <aop:pointcut id="businessService" expression="execution(public * com.yanxi.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/>
    </aop:config>

3:網上百度後解決方案(利用sessionFactory本身寫save和update方法)session

public Book save(Book book) {
       Session session= sessionFactory.getCurrentSession();
        session.beginTransaction();
        session.save(book);
        session.getTransaction().commit();
        session.close();
        System.out.println("a book save success ");
        return null;
    }

尚未找到其它更好的解決方案,反正這個問題困擾我很久時間了 哎app

相關文章
相關標籤/搜索