Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda