Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Should I put @Transactional annotation for submethods also in spring?

I have a main DB handler method, which calls other methods, which are also working with BD things.

I put @Transactional annotation for the main method, because I want to roll back everything, if something goes wrong.

My question is: should I put this annotation also for the submethods, or it will know that the submethods were called from a method which is transactional.

For example, in the deleting method an exception occurs, how can I make sure that the writing part will be also rollbacked:

@Transactional
public void maintDbTings() {
    writing();
    deleting();
}

@Transactional //do I need this?
public void writing() {
    //no exceptions
}

@Transactional //do I need this?
public void deleting() {
    //exception occurs
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In plain english, when you have this:

@Transactional
public void maintDbTings() {
    writing();
}


@Transactional //do I need this?
public void writing() {
    //no exceptions
}

And call mainDbTings, the @Transactional on the writing has no effect. Meaning that the transaction that was started for mainDbThings will still be present/open in writing. So in this case you can easily drop it.

On the other hand since writing is public someone might call it expecting it to be transactional, since it is a service class most probably. In this case making writing to be @Transactional is mandatory and you can't drop it.

So it's up your needs really.

about 4 years ago · Santiago Trujillo Report

0

Spring begins a transaction when it encounters a method annotated with @Transactional. The transaction’s scope covers the execution of that method, the execution of any methods that method invokes, and so on, until the method returns. Any managed resources that are covered by the configured PlatformTransactionManager and that you use during the transaction scope participate in the transaction. For example, if you use the org.springframework.jdbc.datasource.DataSourceTransactionManager, a Connection retrieved from the linked DataSource participates in the transaction automatically. The transaction terminates one of two ways: Either the method completes execution directly and the transaction manager commits the transaction, or the method throws an exception and the transaction manager rolls the transaction back.

I hope it is clear now.

about 4 years ago · Santiago Trujillo Report

0

You can use propagation properties like REQUIRED, REQUIRES_NEW, NESTED according to your requirement as described in the below link:

http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!