DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Integrating Apache Spark With Drools: A Loan Approval Demo
  • Production-Grade LLM Regression Testing: Contracts, Evals, and Quality Gates
  • PII Detection Before RAG Ingestion: Screening Documents in C#

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Spring @Transactional and Private Methods [Snippets]

Spring @Transactional and Private Methods [Snippets]

Working around Spring's @Transactional and private methods using Java 8's lambdas can result in cleaner solutions and less fragmented objects.

By 
Grzegorz Ziemoński
·
Jul. 20, 17 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
45.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

As most of you probably know, due to the very nature of the solution, Spring's @Transactional annotation does not work on private methods unless you're using the AspectJ mode (which, in my experience, most of us aren't). In such a case, the only solution to the problem is to access the transactional method through an auto-wired bean, either via self-injection or delegation.

The problem that I have with the first approach is that it screams, "I AM A HACK!" And I obviously don't like when my code screams that. The problem that I have with the second one is not the mere fact of letting another object do the work, but the way we achieve that, i.e. by creating these strange beans with arbitrary names just to get it working.

With Java 8 "in the hood" for a good few years and a growing popularity of other languages with functional features, we can do better. Here's my way to do that using lambdas:

// SomeBean.java :
@Service
public class SomeBean {
    @Autowired
    private TransactionHelper helper;

    public void nonTransactional() {
        // non-transactional stuff
        String result = helper.withTransaction(() -> gimmeTheResult());
        // or:
        helper.withTransaction(() -> fireAndForget());
        // continue...
    }
}

// TransactionHelper.java :
@Service
public class TransactionHelper {

    @Transactional
    public <T> T withTransaction(Supplier<T> supplier) {
        return supplier.get();
    }

    @Transactional
    public void withTransaction(Runnable runnable) {
        runnable.run();
    }
}


Obviously, this ain't rocket science, and it's not something uncommon in other frameworks. Yet still, for some reason, more often than the code above, I see something like this:

// SomeBean.java :
@Service
public class SomeBean {
    @Autowired
    private TransactionalPart transactionalPart;

    public void nonTransactional() {
        // non-transactional stuff
        transactionalPart.execute();
        // continue...
    }
}

// TransactionalPart.java :
@Service
public class TransactionalPart {
    @Transactional
    public void execute() {
        // do stuff
    }
}


I believe that the first solution is cleaner, arguably simpler, and does not unnecessarily fragment objects. What about you? Let me know in the comments!

Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook