When the transaction will be committed?










2















I need some clarification regarding Spring transation.
In ClassA I am creating a declaritive transaction. From ClassA I am calling method1() of ClassB.
ClassB is having @Transactional(propagation = Propagation.REQUIRED) annotation in classs level.
In method1() I am doing a save operation.



My question is, when that data will be committed ? Will it be committed while control will come out from
method1()? or in ClassA where we are committing the transaction manually ?.



Code:



ClassA



class ClassA 

void myMethod()
TransactionStatus status = null;
DefaultTransactionDefinition def =
new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
def.setTimeout(contentFileProcessingConfigBean.getFileCopyTransactionTimeout());
status = transactionManager.getTransaction(def);

b.method1();

if (status != null)
transactionManager.commit(status);






ClassB



@Transactional(propagation = Propagation.REQUIRED)
class ClassB

void method1()

//doing save operation












share|improve this question


























    2















    I need some clarification regarding Spring transation.
    In ClassA I am creating a declaritive transaction. From ClassA I am calling method1() of ClassB.
    ClassB is having @Transactional(propagation = Propagation.REQUIRED) annotation in classs level.
    In method1() I am doing a save operation.



    My question is, when that data will be committed ? Will it be committed while control will come out from
    method1()? or in ClassA where we are committing the transaction manually ?.



    Code:



    ClassA



    class ClassA 

    void myMethod()
    TransactionStatus status = null;
    DefaultTransactionDefinition def =
    new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    def.setTimeout(contentFileProcessingConfigBean.getFileCopyTransactionTimeout());
    status = transactionManager.getTransaction(def);

    b.method1();

    if (status != null)
    transactionManager.commit(status);






    ClassB



    @Transactional(propagation = Propagation.REQUIRED)
    class ClassB

    void method1()

    //doing save operation












    share|improve this question
























      2












      2








      2


      0






      I need some clarification regarding Spring transation.
      In ClassA I am creating a declaritive transaction. From ClassA I am calling method1() of ClassB.
      ClassB is having @Transactional(propagation = Propagation.REQUIRED) annotation in classs level.
      In method1() I am doing a save operation.



      My question is, when that data will be committed ? Will it be committed while control will come out from
      method1()? or in ClassA where we are committing the transaction manually ?.



      Code:



      ClassA



      class ClassA 

      void myMethod()
      TransactionStatus status = null;
      DefaultTransactionDefinition def =
      new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
      def.setTimeout(contentFileProcessingConfigBean.getFileCopyTransactionTimeout());
      status = transactionManager.getTransaction(def);

      b.method1();

      if (status != null)
      transactionManager.commit(status);






      ClassB



      @Transactional(propagation = Propagation.REQUIRED)
      class ClassB

      void method1()

      //doing save operation












      share|improve this question














      I need some clarification regarding Spring transation.
      In ClassA I am creating a declaritive transaction. From ClassA I am calling method1() of ClassB.
      ClassB is having @Transactional(propagation = Propagation.REQUIRED) annotation in classs level.
      In method1() I am doing a save operation.



      My question is, when that data will be committed ? Will it be committed while control will come out from
      method1()? or in ClassA where we are committing the transaction manually ?.



      Code:



      ClassA



      class ClassA 

      void myMethod()
      TransactionStatus status = null;
      DefaultTransactionDefinition def =
      new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
      def.setTimeout(contentFileProcessingConfigBean.getFileCopyTransactionTimeout());
      status = transactionManager.getTransaction(def);

      b.method1();

      if (status != null)
      transactionManager.commit(status);






      ClassB



      @Transactional(propagation = Propagation.REQUIRED)
      class ClassB

      void method1()

      //doing save operation









      java spring spring-transactions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 9:56









      RehmanRehman

      2,82021226




      2,82021226






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Probably after B.method1() finishes the aspect which is wrapping the call to @Transactional (visible in the stack trace) will commit the transaction. However this will depend on:



          • Are A and B are both Spring beans?

          • Is the same transactionManager bean used by transactions in A and B?

          • Maybe if A is written using TransactionTemplate instead of plain TransactionManager object.

          Mixing the manual and declarative transaction management is not recommended as you have to guess. I guess it will come to transactionManager which you are not showing.






          share|improve this answer

























          • So u r telling that it will be committed n following line ? transactionManager.commit(status);

            – Rehman
            Nov 14 '18 at 10:10











          • @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

            – Karol Dowbecki
            Nov 14 '18 at 10:11












          • A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

            – Rehman
            Nov 14 '18 at 10:22











          • B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

            – Karol Dowbecki
            Nov 14 '18 at 10:24












          • Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

            – Rehman
            Nov 14 '18 at 10:29











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297380%2fwhen-the-transaction-will-be-committed%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Probably after B.method1() finishes the aspect which is wrapping the call to @Transactional (visible in the stack trace) will commit the transaction. However this will depend on:



          • Are A and B are both Spring beans?

          • Is the same transactionManager bean used by transactions in A and B?

          • Maybe if A is written using TransactionTemplate instead of plain TransactionManager object.

          Mixing the manual and declarative transaction management is not recommended as you have to guess. I guess it will come to transactionManager which you are not showing.






          share|improve this answer

























          • So u r telling that it will be committed n following line ? transactionManager.commit(status);

            – Rehman
            Nov 14 '18 at 10:10











          • @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

            – Karol Dowbecki
            Nov 14 '18 at 10:11












          • A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

            – Rehman
            Nov 14 '18 at 10:22











          • B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

            – Karol Dowbecki
            Nov 14 '18 at 10:24












          • Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

            – Rehman
            Nov 14 '18 at 10:29
















          0














          Probably after B.method1() finishes the aspect which is wrapping the call to @Transactional (visible in the stack trace) will commit the transaction. However this will depend on:



          • Are A and B are both Spring beans?

          • Is the same transactionManager bean used by transactions in A and B?

          • Maybe if A is written using TransactionTemplate instead of plain TransactionManager object.

          Mixing the manual and declarative transaction management is not recommended as you have to guess. I guess it will come to transactionManager which you are not showing.






          share|improve this answer

























          • So u r telling that it will be committed n following line ? transactionManager.commit(status);

            – Rehman
            Nov 14 '18 at 10:10











          • @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

            – Karol Dowbecki
            Nov 14 '18 at 10:11












          • A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

            – Rehman
            Nov 14 '18 at 10:22











          • B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

            – Karol Dowbecki
            Nov 14 '18 at 10:24












          • Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

            – Rehman
            Nov 14 '18 at 10:29














          0












          0








          0







          Probably after B.method1() finishes the aspect which is wrapping the call to @Transactional (visible in the stack trace) will commit the transaction. However this will depend on:



          • Are A and B are both Spring beans?

          • Is the same transactionManager bean used by transactions in A and B?

          • Maybe if A is written using TransactionTemplate instead of plain TransactionManager object.

          Mixing the manual and declarative transaction management is not recommended as you have to guess. I guess it will come to transactionManager which you are not showing.






          share|improve this answer















          Probably after B.method1() finishes the aspect which is wrapping the call to @Transactional (visible in the stack trace) will commit the transaction. However this will depend on:



          • Are A and B are both Spring beans?

          • Is the same transactionManager bean used by transactions in A and B?

          • Maybe if A is written using TransactionTemplate instead of plain TransactionManager object.

          Mixing the manual and declarative transaction management is not recommended as you have to guess. I guess it will come to transactionManager which you are not showing.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 10:16

























          answered Nov 14 '18 at 10:06









          Karol DowbeckiKarol Dowbecki

          23.2k93556




          23.2k93556












          • So u r telling that it will be committed n following line ? transactionManager.commit(status);

            – Rehman
            Nov 14 '18 at 10:10











          • @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

            – Karol Dowbecki
            Nov 14 '18 at 10:11












          • A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

            – Rehman
            Nov 14 '18 at 10:22











          • B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

            – Karol Dowbecki
            Nov 14 '18 at 10:24












          • Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

            – Rehman
            Nov 14 '18 at 10:29


















          • So u r telling that it will be committed n following line ? transactionManager.commit(status);

            – Rehman
            Nov 14 '18 at 10:10











          • @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

            – Karol Dowbecki
            Nov 14 '18 at 10:11












          • A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

            – Rehman
            Nov 14 '18 at 10:22











          • B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

            – Karol Dowbecki
            Nov 14 '18 at 10:24












          • Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

            – Rehman
            Nov 14 '18 at 10:29

















          So u r telling that it will be committed n following line ? transactionManager.commit(status);

          – Rehman
          Nov 14 '18 at 10:10





          So u r telling that it will be committed n following line ? transactionManager.commit(status);

          – Rehman
          Nov 14 '18 at 10:10













          @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

          – Karol Dowbecki
          Nov 14 '18 at 10:11






          @Rehman I'd guess it will be just after b.method1(); finishes execution. It's a guess though, I'd be happy if you can prove me wrong with a test. You have omitted a lot of code e.g. are A and B spring beans?

          – Karol Dowbecki
          Nov 14 '18 at 10:11














          A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

          – Rehman
          Nov 14 '18 at 10:22





          A and B both are spring beans. B is not using any transactionManager. Its having class level Transactional annotation.

          – Rehman
          Nov 14 '18 at 10:22













          B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

          – Karol Dowbecki
          Nov 14 '18 at 10:24






          B is using @Transactional with default transactionManager attribute value (check the annotation source code). There is always a transaction manager for declarative transactions but it's impossible to tell what it is without seeing your full Spring context.

          – Karol Dowbecki
          Nov 14 '18 at 10:24














          Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

          – Rehman
          Nov 14 '18 at 10:29






          Hmm. I will have a look. My understanding was also in the same line as you have mentioned in your answer. But when I see the log this theory is not fitting into that. In my application one thread updating the value and coming out from that method method1(). Later when other thread is fetching the data its getting old record, not the latest one.

          – Rehman
          Nov 14 '18 at 10:29




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297380%2fwhen-the-transaction-will-be-committed%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo