GoogleCloudPlatform / GoogleCloudPlatform/spring-cloud-gcp

PartTree Delete queries cause nested Spanner transaction when using @Transactional

Open
#619 5 comments 3 reactions 0 assignees View on GitHub
priority: p3 spanner type: bug
Dominant language
Java
Stars
551
Forks
349
Avg merge
1d 13h
Merged PRs (30d)
14

Description

**Describe the bug**
I am not sure if this is expected behaviour, but Spanner delete operations raise exception within methods annotated with `@Transactional`.

**Sample**
```
@Transactional
public void handle(String ownerId, List newEntities) {

itemRepository.deleteByOwnerId(ownerId);

itemRepository.saveAll(newEntities);
}
```

On entering this method a transaction is created: `Creating new transaction with name [com.xxx.xxx.UpdateItem.handle]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT`

But executing `deleteByOwnerId` then triggers an `exception:java.lang.IllegalStateException: There is already declarative transaction open`.

I know Spanner does not allow nested transactions, and [the docs](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/blob/8a26f8c866afdf879dc416460822fa6b94ff2809/docs/src/main/asciidoc/spanner.adoc#query-methods) state "The delete operation happens in a single transaction". But I wonder if this expected behaviour when using @Transactional? Should the delete run inside the existing transaction?

**Workaround**
The workaround is to first read the entities to delete and then call `itemRepository.deleteAll(entitiesToDelete)`

NB: This is using version 1.2 of spring-cloud-gcp

Contributor guide

Open the contributing guide

Research direction

Start with the PartTree delete path invoked by itemRepository.deleteByOwnerId inside the @Transactional handler and reproduce the reported nested-transaction exception. Compare it with the deleteAll(entitiesToDelete) workaround and the query-method documentation linked in the issue. Done means the expected transaction behavior is established and the delete path either works within the existing transaction or is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.