Propagate spring transaction to sibling call

bedrin

Consider I have following spring beans

CompositeService:

@Service
public class CompositeService {

    @Resource
    private ServiceA serviceA;

    @Resource
    private ServiceB serviceB;

    public ResultBean compositeMethod() {
        ResultBean result = new ResultBean();
        result.setA(serviceA.getA());
        result.setB(serviceB.getB());
        return result;
    }

}

ServiceA:

@Service
public class ServiceA {

    @Transactional
    @Cacheable
    A getA() {
        // calls DAO layer and makes a query to the database
    }

}

ServiceB:

@Service
public class ServiceB {

    @Transactional
    @Cacheable
    B getB() {
        // calls DAO layer and makes a query to the database
    }

}

The cacheable aspect has a higher order

The problem with this code is that it will start two transactions (and take two connections from the pool) in case of cache-miss in both services. Can I configure Spring to use the same transaction in this use case? I.E. propagate the transaction from ServiceA to the CompositeService and after that down to ServiceB?

I cannot set the CompositeService as transactional because I do not want to start a transaction (and borrow a connection from pool) in case of cache-hit both in ServiceA and ServiceB

M. Deinum

Spring will only propagate a transaction if everything is under the same transaction. So the short answer is that you should annotate your CompositeService with @Transactional.

@Service
public class CompositeService {

    @Transactional
    public ResultBean compositeMethod() {
        ResultBean result = new ResultBean();
        result.setA(serviceA.getA());
        result.setB(serviceB.getB());
        return result;
    }
}

Generally this is fast enough as it only does a checkout from the underlying connection pool. However if you experience latency or don't always need a connection you can wrap your actual DataSource in a LazyConnectionDataSourceProxy. This will obtain a Connection when first needed.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Call a function in a sibling component Reactjs

分類Dev

Call method from sibling component using ReactJS

分類Dev

Spring Hibernate Transaction Logging

分類Dev

Transaction management with Spring Batch

分類Dev

Spring Transaction Management @Transactional Behavior

分類Dev

Spring Change Transaction Isolation Mode

分類Dev

Spring transaction configuration minus Exception (`-Exception`) meaning

分類Dev

Spring JPA transaction over multiple methods

分類Dev

Spring + HibernateTemplate + AOP for transaction mamangement not working

分類Dev

Hibernate Envers Performance and Transaction Management in a Spring Application

分類Dev

Spring Boot Data Hibernate Transaction Manager

分類Dev

How to handle nested transaction exception in spring integration

分類Dev

Spring Declarative transaction management with hibernate using @Transactional

分類Dev

Creating bitronix transaction manager in spring-boot

分類Dev

Spring Data JPA: Pageable query rollbacks transaction

分類Dev

Does .net guarantee to call Transaction.Current.TransactionCompleted?

分類Dev

Set of sql commands executed in single call considered as under transaction

分類Dev

jOOQ + Spring : PSQLException: current transaction is aborted, commands ignored until end of transaction

分類Dev

Spring Boot Thymeleaf Ajax Call

分類Dev

Spring Transactionの伝播の問題

分類Dev

How to create transaction in Spring Boot aop @Around function?

分類Dev

How to manage 2 DAO methods in a single transaction in Java Spring and Hibernate?

分類Dev

Spring native query executed within a transaction taking outdated value

分類Dev

Caused By: javax.persistence.TransactionRequiredException: on Weblogic with Spring JTA Transaction

分類Dev

Spring Declarative Transaction Rollback fails for even after stating to roll back

分類Dev

Cannot configure @Transaction to work with Spring Data Neo4j

分類Dev

Propagate = BIMLのFalse

分類Dev

Spring Transaction Hibernate @ Transactionアノテーションが@Autowiredで機能しない

分類Dev

Spring WebClient Call Two Dependent API

Related 関連記事

  1. 1

    Call a function in a sibling component Reactjs

  2. 2

    Call method from sibling component using ReactJS

  3. 3

    Spring Hibernate Transaction Logging

  4. 4

    Transaction management with Spring Batch

  5. 5

    Spring Transaction Management @Transactional Behavior

  6. 6

    Spring Change Transaction Isolation Mode

  7. 7

    Spring transaction configuration minus Exception (`-Exception`) meaning

  8. 8

    Spring JPA transaction over multiple methods

  9. 9

    Spring + HibernateTemplate + AOP for transaction mamangement not working

  10. 10

    Hibernate Envers Performance and Transaction Management in a Spring Application

  11. 11

    Spring Boot Data Hibernate Transaction Manager

  12. 12

    How to handle nested transaction exception in spring integration

  13. 13

    Spring Declarative transaction management with hibernate using @Transactional

  14. 14

    Creating bitronix transaction manager in spring-boot

  15. 15

    Spring Data JPA: Pageable query rollbacks transaction

  16. 16

    Does .net guarantee to call Transaction.Current.TransactionCompleted?

  17. 17

    Set of sql commands executed in single call considered as under transaction

  18. 18

    jOOQ + Spring : PSQLException: current transaction is aborted, commands ignored until end of transaction

  19. 19

    Spring Boot Thymeleaf Ajax Call

  20. 20

    Spring Transactionの伝播の問題

  21. 21

    How to create transaction in Spring Boot aop @Around function?

  22. 22

    How to manage 2 DAO methods in a single transaction in Java Spring and Hibernate?

  23. 23

    Spring native query executed within a transaction taking outdated value

  24. 24

    Caused By: javax.persistence.TransactionRequiredException: on Weblogic with Spring JTA Transaction

  25. 25

    Spring Declarative Transaction Rollback fails for even after stating to roll back

  26. 26

    Cannot configure @Transaction to work with Spring Data Neo4j

  27. 27

    Propagate = BIMLのFalse

  28. 28

    Spring Transaction Hibernate @ Transactionアノテーションが@Autowiredで機能しない

  29. 29

    Spring WebClient Call Two Dependent API

ホットタグ

アーカイブ