[英]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
发生这种情况是因为
You explicitly started a read-only transaction with START TRANSACTION READ ONLY
or similar. 您已使用
START TRANSACTION READ ONLY
或类似内容显式启动了只读事务。
The configuration parameter default_transaction_read_only
is set to on
. 配置参数
default_transaction_read_only
设置为on
。
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.