java - 关于Spring整合Hibernate的问题
PHP中文网
PHP中文网 2017-04-17 14:49:31
[Java讨论组]

我执行数据库操作的时候一直报出: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.
这个错误。

applicationContext.xml:



    
    
    
    




    
        
    
    
        
            
                org.hibernate.dialect.MySQLDialect
            
            update
            
                true
            
            
                update
            
            
                thread
            
        
    

    
    
        
            classpath:/com/sx/bean/*.hbm.xml
        
    




    




    
    
        
            PROPAGATION_REQUIRED
            PROPAGATION_REQUIRED
            PROPAGATION_REQUIRED
            readOnly
        
    



    
        
    

UserDao下的save方法:

public boolean save(User user) {
    try
    {
        getHibernateTemplate().save(user);
    } catch (RuntimeException e)
    {
        System.out.println("UserDao >> save >> " + e);
        return false;
    }
    return true;
}

每次调用Dao方法都会报出上述那个错误,求救。。。。。

PHP中文网
PHP中文网

认证0级讲师

全部回复(3)
高洛峰

问题已经解决!!
后来我换了一个实现方法:
<!-- 配置事务管理器 -->

<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 所有find开头的方法为只读,所有查询方法都以find开头 -->
        <tx:method name="find*" read-only="true" />
        <!-- 其余方法如果没有事务的时候,就开启新事务运行该方法,否则使用已有的事务运行该方法 -->
        <!-- 关闭read-only保证save*,update*,delete*等方法可以修改数据 -->
        <tx:method name="*" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
</tx:advice>

<!-- 设置com.sx.dao包下及其子包下的类的所有方法都加上txAdvice事务通知切片(主要用于自动实现Hibernate事务的提交回滚等) -->
<aop:config>
    <aop:pointcut id="DaoPointCut" expression="execution(* com.sx.dao..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="DaoPointCut"/>
</aop:config>
阿神

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

伊谢尔伦
<prop key="*">readOnly</prop>

所有方法都是readOnly了,你应该设置*的readOnly属性为false,然后设置Insert,Update,Delete,Save等等开头的方法的readOnly为true

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号