Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-14148

SimpleTransactionScope does not suspend and resume its scoped objects

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.2.5
    • Fix Version/s: 4.2.6, 4.3 RC2
    • Component/s: Transaction
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      The idea is to define a bean in transaction scope, so that each transaction can use fresh instance of such bean.
      However it's possible to call methods on such bean outside transaction and it's causing side effects.

      Try following test case:

      1. register transaction scope
      2. define bean in this scope
      3. call method on this bean
      4. start transaction and use bean inside it.

      Point 4) will not behave as expected. Only one instance of transaction scoped bean will be created and it will be reused between following transactions.

      The problem is, that we are calling method on our bean before transaction starts. SimpleTransactionScope registers CleanupSynchronization on current thread. Now we are starting transaction and trying to access our bean. The problem it, that AbstractPlatformTransactionManager suspends current transaction, well there is none, but it suspends all synchronizers registered by TransactionSynchronizationManager#registerSynchronization(...).
      Once the transaction is finished the method SimpleTransactionScope$CleanupSynchronization#afterCompletion will not be executed, because synchronizer is suspended.

      Simple solution to this problem would be adding assert to SimpleTransactionScope#get(...)

          @Override
          public Object get(String name, ObjectFactory<?> objectFactory) {
      	if (!TransactionSynchronizationManager.isActualTransactionActive()) {
      	    throw new .....
      	}
          }
      

      I think, that using transaction scoped bean outside transaction should not be allowed, because it's behavior is undefined.

        Attachments

          Activity

            People

            Assignee:
            juergen.hoeller Juergen Hoeller
            Reporter:
            Maciej Miklas Maciej Miklas
            Last updater:
            Spring Issuemaster
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              1 year, 28 weeks, 6 days ago