Should the @Transactional annotation be added to the delete method in the SimpleJpaRepository class, which takes a parameter of type Specification<T>? Otherwise, it may cause a JDBC connection read-only exception when calling this method due to the @Transactional(readOnly = true) annotation on the class. #3188
Labels
In my work, I have been using the convenient features provided by Spring Data JPA's JpaRepository for rapid development. However, when I attempted to call the following code for the first time, it resulted in a java.sql.SQLException.
The exception message "Connection is read-only. Queries leading to data modification are not allowed" indicates that a read-only transaction is in effect, even though you didn't explicitly declare any "read-only transactions" in your business code. Upon investigation, I discovered that Spring Data JPA, specifically within the
org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor.RepositoryAnnotationTransactionAttributeSource#computeTransactionAttribute
method, obtainsTransactionAttribute
based on certain priorities. By default, it first checks whether yourJpaResourceOwnershipRepository
methods and classes have relevant transaction annotations. Then, it looks at the declarations in Spring Data JPA'sSimpleJpaRepository
class. Subsequently, I reviewed the source code for the corresponding method inSimpleJpaRepository
. Please see the following code snippet:Upon reaching this point, I wonder whether this method should have been annotated with
@Transactional
but it wasn't. The@Transactional
annotation's default value forreadOnly
isfalse
, and considering the semantics of the method name, "delete" is clearly a write operation. It should not use the default false value that be specified onSimpleJpaRepository
class . To confirm my suspicion, I examined other methods inSimpleJpaRepository
with deletion semantics, and I found that all the other methods have the@Transactional
annotation, except for the one I am using. Is this intentional behavior, or is it indeed a bug?The text was updated successfully, but these errors were encountered: