Spring & Spring Data JPA: Managing Transactions

Поділитися
Вставка
  • Опубліковано 24 лип 2024
  • Spring Boot and Spring Data JPA make the handling of transactions extremely simple. They enable you to declare your preferred transaction handling and provide seamless integration with Hibernate and JPA.
    The only thing you need to do is to annotate one of your methods with @Transactional. But what does that actually do? Which method(s) should you annotate with @Transactional? And why can you set different propagation levels?
    I will answer all of these questions in this video. To make that a little bit easier to understand, I will focus on local transactions. These are transactions between your application and 1 external system, e.g., your database.
    Like my channel? Subscribe!
    ➜ bit.ly/2cUsid8
    Join the free Member Library:
    goo.gl/dtyIIC
    Read the accompanying post: thorben-janssen.com/transacti...
    Want to connect with me?
    Blog: thorben-janssen.com/
    Twitter: / thjanssen123
    Facebook: / thoughtsonjava
    #Spring #SpringDataJPA #Transactions
  • Наука та технологія

КОМЕНТАРІ • 40

  • @mrabbas9
    @mrabbas9 Рік тому +1

    Commenting only to appreciate how well you have organized this video.

  • @keremkarademir6101
    @keremkarademir6101 2 роки тому

    Been struggling for a few days to figure out how this annotations help with our backend applications and your video made it so clear to me now. God bless you,man!

  • @heribertoalortadeo7529
    @heribertoalortadeo7529 4 роки тому +5

    Top tier content, thank you for your work.

  • @MrLoyalNguyen
    @MrLoyalNguyen 4 роки тому +1

    Great video Thorben, thank you!

  • @swapnakumari1884
    @swapnakumari1884 2 роки тому

    Gave me very good clear knowledge on @Transactional attribute. Thank you.

  • @kamboj65
    @kamboj65 2 роки тому

    Very clear explanation! Thank you!

  • @yuriyk8002
    @yuriyk8002 3 роки тому +1

    Very informative and well explained. Thank you!

  • @israelduarte1326
    @israelduarte1326 2 роки тому

    Thanks for the video. It helped me a lot to understand more about Phanton Read and how to deal with it.

  • @balasahebnimse8473
    @balasahebnimse8473 3 роки тому +2

    Thank you for your great work

  • @bboysistou
    @bboysistou 2 роки тому

    your course is very clear I like it

  • @mehmetbilgin2101
    @mehmetbilgin2101 2 роки тому

    good explanation, thank you for the video

  • @satishchitimoju7308
    @satishchitimoju7308 3 роки тому +1

    Great work. Keep going ..

  • @dmytro-busyhin
    @dmytro-busyhin Рік тому

    Thank you, beneficial video!

  • @fullnaoufal
    @fullnaoufal 3 роки тому

    Hi janssen,
    Thanks for excellently explaining transaction management in Spring Data JPA.
    Is there any resource on your blog expalining the use of @Lock and @Version please?
    Regads

  • @kratikothari7664
    @kratikothari7664 Рік тому

    Thank you for another great video . Can you also cover the need of optimistic locking and when do we need to use that in spring data jpa .

  • @SushilKumarBhaskar
    @SushilKumarBhaskar 4 роки тому +1

    excellently explained ee+, Please make more videos on this.

  • @hyperborean72
    @hyperborean72 2 роки тому +3

    Thank you for another great explanation. By the way why didn't you mention 'isolation' attribute?

  • @ginoallisonrasoanaivo4484
    @ginoallisonrasoanaivo4484 Рік тому

    Thank you, a very clear video, just a question: does spring support both declarative and programmatic transaction management ?

  • @jasper5016
    @jasper5016 2 роки тому

    Thanks for the great video. If there are 3 different table updates. I have written them in different methods. I used @Transaction annotation for all of them but when 3rd fails, it does not rollback 1st two method transactions. How can I solve this?

  • @park2348190
    @park2348190 2 роки тому

    didn't know that readonly done by not doing dirty checking! thanks

  • @OracleOfSages
    @OracleOfSages 8 місяців тому

    Best video about this subject! Thanks so much!!

  • @stanleyizturriaga1251
    @stanleyizturriaga1251 2 роки тому

    Excellent video, please could you talk about Hibernate caching? thanks...

  • @shomer2009
    @shomer2009 4 роки тому

    Thanks for sharing all this knowledge. I m confused on why we need to add @Transaction without customizing it in the service if it's by default in repository.

    • @wilsonchiviti6970
      @wilsonchiviti6970 4 роки тому

      Its because in your service class you may have many operations involved to call different repository classes, more or less like an aggregator, thus you will need to put all the operations in one transactions, to enable rollback when something goes wrong down the line of execution in your service class

  • @dipakpatil6636
    @dipakpatil6636 4 роки тому

    Nice 👍

  • @jaideeph6985
    @jaideeph6985 2 роки тому

    Sometimes the rollback for Exception.class won't work when we use multiple entities for multiple tables and make more than one CRUD operations in a service class . Any suggestions for this .. ??

  • @fernandoavf7
    @fernandoavf7 Рік тому

    Thank so much!, I have a question, what happen if I have a springboot app to read/write on database without @Transactional annotations and another springboot app that process data and call to that app to write data? (and this service effectively has the @Transactional annotation), if an exception occurs, there will be a roll-back or not?, @Transactional its necessary in both services?, or only in the service connected to database?

  • @christophe_agoero
    @christophe_agoero 4 роки тому

    It is possible to add the annotation @Transactional on the class. On each of my services I put on the class level @Transactional(readOnly = true) like that by default I do not open a transaction and if my method modify the base then I add an @Transactional on the méthode.

    • @nagendrakumar3500
      @nagendrakumar3500 4 роки тому +3

      Hi, readOnly=true doesn't mean no transaction is used. it means that dirty checks are not performed and performance is increased. Transaction is still be used. Method level annotation overrides the class / inteface level annotation

  • @ketantank11
    @ketantank11 3 роки тому

    Sir your video are very informative..but one question arises,How to avoid cocurrent read and insert if your java application is deployed on different servers Or jvm and connect with one db how transaction work in that scenario..

  • @manojkumarpadarthi4805
    @manojkumarpadarthi4805 3 роки тому

    So @ Transactional is not required in spring boot ?

  • @alphabravo9999
    @alphabravo9999 2 роки тому

    My transaction executes an update statement even tho i did not explicitly call it. Why does this happen when transaction is about to end. Im confused help

    • @ThoughtsOnJava
      @ThoughtsOnJava  2 роки тому

      All JPA entities follow a managed life cycle. At the end of the transaction, your JPA implementation performs a dirty check to find all changed, managed entity objects and automatically executes an SQL UPDATE for it.
      That's entirely independent of the any calls of save method on your repository. That method is only needed to persist new entities or merge detached ones.

  • @giisoft8797
    @giisoft8797 3 роки тому

    kya hal chal bhai

  • @masoodahmad6623
    @masoodahmad6623 3 роки тому

    Hi How are you, I've learned a lot from you I have a problem, I want to add two records in one table with different values but facing problems kindly solve my problem and oblige.
    EXCEPTION
    Thu Sep 10 13:56:01 PDT 2020
    There was an unexpected error (type=Internal Server Error, status=500).
    could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
    I've created the following repository, service, controller and entity
    @Repository
    public interface LedgerRepo extends JpaRepository {
    @Transactional
    @Modifying
    @Query(value = "insert into ledger (account_id, trans_date, credit, debit, balance) values (:debit_account, :trans_date, :debit, 0, 0)", nativeQuery = true )
    void debitTransaction(@Param("debit_account") Long debit_account,
    @Param("trans_date") Date trans_date,
    @Param("debit") Integer debit);
    @Transactional
    @Modifying
    @Query(value = "insert into ledger (account_id, trans_date, credit, debit, balance) values (:credit_account, :trans_date, 0, :credit, 0)", nativeQuery = true )
    void creditTransaction(@Param("credit_account") Long credit_account,
    @Param("trans_date") Date trans_date,
    @Param("credit") Integer credit);
    }
    SERVICE
    @Autowired
    private LedgerRepo ledgerRepo;
    public void addLedger (Ledger ledger) {
    ledgerRepo.save(ledger);
    }
    public void debitTransaction (Long debit_account, Date trans_date, Integer debit) {
    ledgerRepo.debitTransaction(debit_account, trans_date, debit);
    }
    public void creditTransaction (Long credit_account, Date trans_date, Integer credit) {
    ledgerRepo.creditTransaction(credit_account, trans_date, credit);
    }
    CONTROLLER
    @PostMapping("ledger/addtransaction")
    @ResponseBody
    public String addTransaction (@RequestParam(name="credit_account") Long credit_account,
    @RequestParam(name="debit_account") Long debit_account,
    @RequestParam(name = "trans_date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date trans_date,
    @RequestParam(name = "amount") Integer debit,
    @RequestParam(name = "amount") Integer credit) {
    ledgerService.creditTransaction(credit_account, trans_date, credit);
    ledgerService.debitTransaction(debit_account, trans_date, debit);
    return "credit account "+credit_account+" debit_account "+debit_account+" date "
    +trans_date+" credit "+credit+" debit "+debit;
    //return "redirect:/ledger";
    }
    ENTITY
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity
    @Data
    public class Ledger {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long trans_id;
    @ManyToOne
    @JoinColumn(name="account_id", insertable = false, updatable = false)
    private ChartOfAccounts chartOfAccounts;
    private Long account_id;
    @Temporal(TemporalType.DATE)
    private Date trans_date;
    private Double debit;
    private Double credit;
    private Double balance;
    when I run the app the following exception is arising

  • @anison1111
    @anison1111 2 роки тому

    misuse of phrase "extremely simple" - in the first line of this video - I really feel BMT is the way to go - so many features offered in hibernate are just so complex and only apply to very specific situation - hibernate is undesirable if you ask me - for what it offers at the cost it offers.

  • @ruixue6955
    @ruixue6955 5 місяців тому

    3:31
    5:52 @Transactional
    6:06 propagation enum
    6:22 REQUIRED - default
    6:54 NOT_SUPPORTED

  • @1q34w
    @1q34w 2 роки тому

    It looks like you skipped the mandatory annotation. 7:10

  • @anison1111
    @anison1111 2 роки тому

    I tend to feel - companies promoted hibernate because they wanted developers to spend their entire life learning it so that companies dont have to pay developers what they should - which is custom written JDBC code, a developer is not paid to study for years a complex and changing framework like hibernate and this effort is tremendous - hibernate has features and until reader find good videos and reads like you they will keep ending up using such framework incorrectly and also failing interviews a lot more - until a framework provides correct and effectively detailed documentation - its a curse to technical community - spring is no exception to this rule. Configuration heavy development is good for companies who dont have to pay what they should to technical people like us and they force us to spend our entire life reading and learning so called ready to use building blocks such as annotations and frameworks such as spring boot and hibernate - its a curse - think to yourself what I just said. Although I can't undermine your effort sharing your hard earned knowledge here Thorben, I just wanted to provided the insight of what we dont look at, why do we have to create such curse in name of innovation.