【发布时间】:2019-10-29 03:25:58
【问题描述】:
将 Spring Boot 1.5 更新到 2.1.5
当尝试执行操作 repository.save(entity) 时,会出现以下错误:
Caused by: com.impossibl.postgres.jdbc.PGSQLSimpleException: cannot execute UPDATE in a read-only transaction
我们使用 org.springframework.data.repositoryCrudRepository 接口来执行操作。
1) @Transactional(readOnly = false),据我了解,将只读模式设置为 false 仅作为对子层的提示,我如何检查和更改其他层?
@Service
public class ServiceImpl
private final Repository repository;
@Autowired
public ServiceImpl(Repository repository) {
this.repository = repository;
}
@Transactional(readOnly = false)
public void operation(Entity entity){
repository.save(entity);
}
而存储库是
public interface Repository extends CrudRepository<Entity, UUID>{
@Query("select e from Entity e where lower(u.name) = lower(?1)")
Entity findByName(String name);
}
build.gradle
------------
`dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.5.RELEASE")
}
`
```runtime("org.springframework.boot:spring-boot-properties-migrator")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-jersey")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-mail")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.quartz-scheduler:quartz:2.3.1")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
compile("com.fasterxml.woodstox:woodstox-core:5.2.1")
compile("org.glassfish.jersey.media:jersey-media-multipart:2.28")
compile("net.java.dev.msv:msv-core:2013.6.1")
compile("com.impossibl.pgjdbc-ng:pgjdbc-ng:0.8.2")
compile('org.apache.commons:commons-lang3:3.9')
compile('commons-io:commons-io:2.6')
compile('org.apache.commons:commons-compress:1.18')
compile('org.apache.poi:poi-ooxml:4.1.0')
compile('org.apache.xmlbeans:xmlbeans:3.1.0')
compile('org.mitre.dsmiley.httpproxy:smiley-http-proxy-servlet:1.10')
compile('com.monitorjbl:xlsx-streamer:2.1.0')
compile('com.zaxxer:HikariCP:3.3.1')
application.properties
spring.datasource.driverClassName=com.impossibl.postgres.jdbc.PGDriver
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.idle-timeout=10000
# Set auto-commit = false, otherwise - Caused by: java.sql.SQLException:
Clobs require connection to be in manual-commit mode...
spring.datasource.hikari.auto-commit=false
logging.level.ROOT=INFO
logging.level.org.springframework.orm.jpa=DEBUG
logging.level.org.springframework.transaction=DEBUG
一个重要的事情是我在 Hikari 中将 Auto-Commit 添加为 false,否则它会失败并出现异常,因为它可以在评论中看到。
注意:在某些线程中建议检查 postgres 连接
show default_transaction_read_only;
default_transaction_read_only
-------------------------------
off
SELECT pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
提前致谢。
【问题讨论】:
-
你能发布堆栈跟踪吗?
-
您好,每次我尝试添加堆栈跟踪时都会出现格式代码错误,所以我同时提供链接,谢谢:pastebin.com/a60pN4FV
-
从 1.5 升级到 2.0 是向前迈出的一大步。就个人而言,我最终放弃了,因为没有时间进行所有需要的调整。
-
日志显示有一个事务被创建并且没有被这个语句关闭:no.app.application.security.UserDetailsService.getUserEntity(UserDetailsService.java:65) 你有这个作为只读事务吗?同一线程继续并获得在上述方法调用期间未正确关闭的同一事务。您能否分享有关该方法的更多详细信息?
-
如果你能分享完整的代码,那真的很有帮助。
标签: postgresql hibernate spring-boot spring-data-jpa hikaricp