简体   繁体   English

错误:无法在只读事务中执行nextval()-PostgreSQL

[英]ERROR: cannot execute nextval() in a read-only transaction - PostgreSQL

I can see internal server error on my app developed on spring mvc, using wildfly as the webserver and database is PostgreSQL . 在使用wildfly作为Web服务器和数据库是PostgreSQL Spring MVC开发的应用程序上,我可以看到内部服务器错误。 What does this exception mean? 此异常是什么意思?

ERROR: cannot execute nextval() in a read-only transaction

It was working all fine before. 之前一切正常。 I tried to look for the solution here on stackoverflow but didn't find anything that could fix this issue. 我试图在stackoverflow上寻找解决方案,但没有找到任何可以解决此问题的方法。

The function nextval() is used to increment a sequence, which modifies the state of the database. 函数nextval()用于递增序列,该序列修改数据库的状态。

You get that error because you are in a read-only transaction. 您收到该错误,因为您处于只读事务中。 This can happen because 发生这种情况是因为

  1. You explicitly started a read-only transaction with START TRANSACTION READ ONLY or similar. 您已使用START TRANSACTION READ ONLY或类似内容显式启动了只读事务。

  2. The configuration parameter default_transaction_read_only is set to on . 配置参数default_transaction_read_only设置为on

  3. You are connected to a streaming replication standby server. 您已连接到流复制备用服务器。

If default_transaction_read_only is set to on , you can either start a read-write transaction with 如果将default_transaction_read_only设置为on ,则可以使用以下命令启动读写事务

START TRANSACTION READ WRITE;

or change the setting by editing postgresql.conf or with the superuser command 或通过编辑postgresql.conf或使用超级用户命令来更改设置

ALTER SYSTEM SET default_transaction_read_only = off;

As others have pointed out nextval() actually updates the database to get a new sequence, so can't be used in a transaction that is marked as read only. 正如其他人指出的那样, nextval()实际上会更新数据库以获取新序列,因此不能在标记为只读的事务中使用。

As you're using Spring I suspect this means that you're using the spring-transaction support. 当您使用Spring我怀疑这意味着您在使用spring-transaction支持。 If you're using annotation based transaction support, then you'd get a read only transaction if you have 如果您使用的是基于注释的交易支持,那么您将获得只读交易

@Transactional(readOnly=true)

That means that when spring starts the transaction it will put it into read only mode. 这意味着,当spring启动事务时,它将进入只读模式。

Remove the readOnly=true bit and a regular writable transaction is created instead. 删除readOnly=true位,而是创建一个常规的可写事务。

Spring transaction control at http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html Spring交易控制位于http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html

I means exactly what it says, here is example: 我的意思就是它的意思,这里是示例:

t=# create sequence so49;
CREATE SEQUENCE
t=# begin;
BEGIN
t=# select nextval('so49');
 nextval
---------
       1
(1 row)

t=# set transaction_read_only TO on;
SET
t=# select nextval('so49');
ERROR:  cannot execute nextval() in a read-only transaction
t=# end;
ROLLBACK

I presume you connected as so called ro user, which is user with "transaction_read_only" set to true, EG: 我假设您以所谓的ro用户连接,该用户是将“ transaction_read_only”设置为true的用户,例如EG:

t=# select rolconfig from pg_roles where rolname ='ro';
         rolconfig
----------------------------
 {transaction_read_only=on}
(1 row)

you can switch that off for your user of course, but this is out of scope I believe 您当然可以为您的用户关闭该功能,但是我认为这超出了范围

https://www.postgresql.org/docs/current/static/sql-set-transaction.html https://www.postgresql.org/docs/current/static/sql-set-transaction.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只读事务中的序列 nextval - postgresql 9 - sequence nextval in read-only transactions- postgresql 9 无法在事务中间更改事务只读属性 - Cannot change transaction read-only property in the middle of a transaction 带有锁定表的Spring只读事务 - Spring read-only transaction with locked table “在只读模式下不允许写操作”错误:与Spring @Service @transaction @Repository和Hibernate相混淆 - “Write operations are not allowed in read-only mode” error : confused with Spring @Service @transaction @Repository and Hibernate 只读事务在我的AOP配置中不起作用 - read-only transaction doesn't work in my AOP config Spring只读事务将数据提交给DB - Spring read-only transaction committing data to the DB 我应该避免大事务并从事务中排除只读查询吗 - should I avoid big transaction and exclude read-only queries from transaction liquibase 是只读的:liquibase 已锁定且无法解锁(openshift 上的 pod) - liquibase is read-only : liquibase is locked and cannot unlock it (pod on openshift) mybatis在@transaction mdoe中插入所有nextval唯一约束错误 - mybatis insert all nextval unique constraint error in @transaction mdoe 尽管所有事务方法都被Spring事务管理标记为只读,但Hibernate更新了数据库 - Hibernate updates database though all the service methods marked as read-only by spring transaction management
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM