我使用 jOOQ 一段时间了,一切都很好。 我在 Spring Boot 应用程序中使用 PostgreSQL 作为数据库。 我正在使用 jOOQ 代码生成来生成 DAO、POJO 等。 另外,使用
spring-boot-starter-jooq
版本 3.2.2
并且它使用 jooq
版本 3.18.9
现在我必须像下面这样扩展自动生成的 DAO:
@Repository
public class SomeRepositoryImpl extends SomeDao {
private final DSLContext dslContext;
public SomeRepositoryImpl(DSLContext dslContext) {
super(dslContext.configuration());
this.dslContext = dslContext;
}
public void save(Example example) {
insert(example);
}
}
上面提到的SomeDao
、
Example example
分别是自动生成的DAO和POJO。
insert()
来自超类的方法或 jOOQ 自动生成的 DAO。如果我从服务层调用
someRepositoryImpl.save(example)
,存储库保存会抛出如下错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.jooq.exception.DataAccessException: SQL [insert into "public"."example" ("name", "domain", "description", "website", "language_id") values (?, ?, ?, ?, ?) returning "public"."example"."example_id"]; ERROR: cannot execute INSERT in a read-only transaction] with root cause
org.postgresql.util.PSQLException: ERROR: cannot execute INSERT in a read-only transaction
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse
and so on....
我没有在代码中的任何地方使用@Transactional(readOnly = true)
。但是,如果我在
SomeRepositoryImpl
中使用下面提到的代码,那么它就可以工作:
@Transactional
public void save(Example example) {
insert(example);
}
另外,如果我使用下面提到的直接在服务层中使用生成的 DAO 的代码,那么它就可以工作:
@Autowired
SomeDao someDao;
someMethod(Example example){
someDao.insert(example);
}
@Transactional
上下文之外访问数据库都是只读的。这样做是为了避免实体的更改在事务之外无意中传播到数据库。
请参阅
SimpleJpaRepository javadoc,它将向您显示该类本身带有 @Transactional(readOnly = true)
注释,这使得所有方法默认为“只读”,除了用
@Transactional
注释的方法(例如
delete
或
save
) )