hibernate查询报 Cannot execute statement in a READ ONLY transaction


 WARN [http-bio-8604-exec-1] org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler.logWarning(232) | SQL Warning Code: 1792, SQLState: 25006
WARN [http-bio-8604-exec-1] org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler.logWarning(233) | Cannot execute statement in a READ ONLY transaction. 
字面意思:不能执行只读的事务 、
刚看到这个问题我跟郁闷,网上百度一下,别人都是更新啊,我只查询了啊,为什么还会报这个错误?还有为什么会限制只读?
首先回答第二个问题:
    <!-- 配置事务传播 -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*"  read-only="true"  />
            <tx:method name="query*" read-only="true"  />
            <tx:method name="get*"   read-only="true"  />
            <tx:method name="*" propagation="REQUIRED"  />
        </tx:attributes>
    </tx:advice>

由于我的方法名是getXmlName,所以被限制成只读了

第一个问题:首先看我的sql:

select CONCAT(CONCAT(date_format(now(),'%Y%m%d'),'-'),lpad(nextval('s_blog_account') + 100000, 6, '0')) from dual

下面为nextval()函数的创建:

DROP FUNCTION IF EXISTS `nextval`;  
  
DELIMITER //  
  
CREATE  FUNCTION `nextval`(seq_name VARCHAR(50)) RETURNS int(11)  
  
    DETERMINISTIC  
  
BEGIN  
  
UPDATE sequence SET current_value = current_value + increment WHERE NAME = seq_name;  
  
RETURN currval(seq_name);  
  
END//  
  

DELIMITER ;

可以看出每次调用nextval()函数都会更新更新数据库,所以涉及到了事务.....

解决办法:修改方法名,不以get、find、query开头,或者修改配置文件注释掉事务传播(不建议);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 PG cannot execute UPDATE in a read-only transaction | How to add column if not exists on PostgreSQL MySQL报错解决:The MySQL server is running with the --read-only option so it cannot execute this statement 1209 - The MySQL server is running with the --read-only option so it cannot execute this statement hibernate 异常:could not execute statement Hibernate学习错误集锦-GenericJDBCException: could not execute statement Cannot assign to read only property 'exports' of object '#' Cannot assign to read only property 'className' of object '#' Uncaught TypeError: Cannot assign to read only property 'exports' of object '#' AD保存PCB出现 cannot write to read - only file rm: cannot remove `xxx': Read-only file system
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM