Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda