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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Embracing Reactive Programming With Spring WebFlux
  • Spring Boot Annotations: Behind the Scenes and the Self-Invocation Problem
  • Comparing ModelMapper and MapStruct in Java: The Power of Automatic Mappers
  • A Guide to Enhanced Debugging and Record-Keeping

Trending

  • Beyond the Prompt: Unmasking Prompt Injections in Large Language Models
  • LLMs for Bad Content Detection: Pros and Cons
  • Distributed Tracing Best Practices
  • Best GitHub-Like Alternatives for Machine Learning Projects
  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.

Grzegorz Ziemoński user avatar by
Grzegorz Ziemoński
·
Jul. 20, 17 · Code Snippet
Like (5)
Save
Tweet
Share
37.79K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Embracing Reactive Programming With Spring WebFlux
  • Spring Boot Annotations: Behind the Scenes and the Self-Invocation Problem
  • Comparing ModelMapper and MapStruct in Java: The Power of Automatic Mappers
  • A Guide to Enhanced Debugging and Record-Keeping

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: