Skip to content

Add no-op TransactionOperations implementation#23198

Closed
vpavic wants to merge 1 commit into
spring-projects:masterfrom
vpavic:noop-tx-ops
Closed

Add no-op TransactionOperations implementation#23198
vpavic wants to merge 1 commit into
spring-projects:masterfrom
vpavic:noop-tx-ops

Conversation

@vpavic

@vpavic vpavic commented Jun 26, 2019

Copy link
Copy Markdown
Contributor

A no-op implementation can be useful for:

  • having TransactionOperations consuming code switch to non-transactional behavior by injecting NoOpTransactionOperations instead of TransactionTemplate
  • testing purposes

For first point we have a use case in Spring Session's JdbcOperationsSessionRepository. I've used similar approach in a number of other projects so having Framework provide it OOTB would be nice. Also note that TransactionOperations isn't parameterized, but TransactionOperations#execute is which makes it impossible to have no-op implementation as lambda expression.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 26, 2019
@sbrannen sbrannen added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement for: team-attention labels Jun 27, 2019
@sbrannen

Copy link
Copy Markdown
Member

Thanks for the PR, @vpavic.

We'll consider it.

Also note that TransactionOperations isn't parameterized, but TransactionOperations#execute is which makes it impossible to have no-op implementation as lambda expression.

Yes, that is a bit unfortunate.

@jhoeller jhoeller self-assigned this Jun 28, 2019
@jhoeller jhoeller removed for: team-attention status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 28, 2019
@jhoeller jhoeller added this to the 5.2 RC1 milestone Jun 28, 2019
@jhoeller

jhoeller commented Jul 3, 2019

Copy link
Copy Markdown
Contributor

Good idea in general! I've implemented this slightly differently as a static withoutTransaction() method on TransactionOperations, along the lines of the recently introduced withDefaults() method on TransactionDefinition.

@vpavic

vpavic commented Jul 4, 2019

Copy link
Copy Markdown
Contributor Author

I don't see anything pushed to the master yet so I'm not sure if I understood the described changes correctly, but if non-transactional behavior is implemented using a new operation on TransactionOperations I'm afraid that won't allow changing transactional behavior by injecting a different TransactionOperations implementation which was the motivation for this PR.

Ideally, TransactionOperations consuming code shouldn't be aware of the fact that there's no transaction involved. This is similar like to, for instance, changing propagation behavior by injecting a differently configured TransactionTemplate.

@jhoeller

jhoeller commented Jul 5, 2019

Copy link
Copy Markdown
Contributor

I'm about to commit this along with a number of other changes now, so it'll be in master in a moment. Thanks for your patience :-)

As for the implementation, withoutTransaction() is a new static factory method on TransactionOperations, returning a TransactionOperations instance with a regular execute method which executes the callback without an actual transaction. This should allow for substitution underneath any TransactionOperations caller as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants