dot*_*abs 4 postgresql hibernate spring-data-jpa spring-boot hikaricp
将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
Run Code Online (Sandbox Code Playgroud)
我们使用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);
}
Run Code Online (Sandbox Code Playgroud)
和存储库是
public interface Repository extends CrudRepository<Entity, UUID>{
@Query("select e from Entity e where lower(u.name) = lower(?1)")
Entity findByName(String name);
}
Run Code Online (Sandbox Code Playgroud)
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')
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
重要的一点是,我在Hikari中将“自动提交”添加为false,否则它将失败,并出现异常,因为它可以在注释中看到。
注意:在某些线程中,建议检查postgres连接
show default_transaction_read_only;
default_transaction_read_only
-------------------------------
off
SELECT pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
Run Code Online (Sandbox Code Playgroud)
提前致谢。
readOnly是false默认设置,因此您永远不要使用@Transactional(readOnly = false),@Transactional而改为使用。@Trasnacional Spring标记某些方法或类时,将创建该类的代理以注入Transaction Manager的逻辑。它使用实现接口的beanorg.springframework.transaction.PlatformTransactionManagerorg.springframework.orm.jpa.JpaTransactionManager将创建一个bean 。readOnly = true,为了禁用“脏检查”机制,该机制在Hibernate中执行所有UPDATE操作。@TransactionalSessionpropagation@Transactional第一次调用方法时设置事务的配置,并且将这些配置用于同一线程中的所有方法调用。请参见代码示例:import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@Service
public class ServiceA {
@Transactional(readOnly = true)
public void a() {
boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
System.out.println(isReadOnly);
}
}
Run Code Online (Sandbox Code Playgroud)
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ServiceB {
private final ServiceA serviceA;
public ServiceB(ServiceA serviceA) {
this.serviceA = serviceA;
}
@Transactional
public void b() {
serviceA.a();
}
}
Run Code Online (Sandbox Code Playgroud)
serviceA.a() 将打印 trueserviceB.b() 将打印 false| 归档时间: |
|
| 查看次数: |
527 次 |
| 最近记录: |