11

I would like to understand the use of having a spring transaction with Propagation Supports. The java docs mention that if the method which has @Transactional(propagation = Propagation.SUPPORTS) is called from within a transaction it supports the transaction but if no transaction exists, the method is executed non-transactionally.

Isn't this already the behavior of spring transactions irrespective of Propagation.SUPPORTS?



public class ServiceBean {

    @Transactional(propagation = Propagation.SUPPORTS)
    public void methodWithSupportsTx() {
        //perform some database operations
    }
}

public class OtherServiceBean {

    @Transactional(propagation = Propagation.REQUIRED)
    public void methodWithRequiredTx() {
        //perform some database operations
        serviceBean.methodWithSupportsTx();
    }
}


In the above code example, irrespective of whether methodWithSupportsTx() has @Transactional(propagation = Propagation.SUPPORTS) annotation it would be executed in a transaction depending on whether methodWithRequiredTx() has @Transactional annotation, right?

So what's the need/use of having a propagation level SUPPORTS?

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113

2 Answers2

5

From javadoc:

Note: For transaction managers with transaction synchronization, PROPAGATION_SUPPORTS is slightly different from no transaction at all, as it defines a transaction scope that synchronization will apply for. As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) will be shared for the entire specified scope. Note that this depends on the actual synchronization configuration of the transaction manager.

So, it means that, for example, multiple invocations of Hibernate's SessionFactory.getCurrentSession() inside methodWithSupportsTx() would return the same session.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1
    Assuming methodWithSupportsTx() does not have a @Transactional annotation are you trying to say that the same session will NOT be returned when methodWithSupportsTx() is called in a transaction? – Andy Dufresne Jun 22 '11 at 10:37
  • @Amit: Actually, without `@Transactional` attempt to call `getCurrentSession()` with typical configuration of `HibernateTransactionManager` would produce a "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" exception. – axtavt Jun 22 '11 at 10:57
  • 1
    No I meant, if there are not @Transactional annotations on the methodWithSupportsTx() method and this method is called from another method which already is in a transaction, won't hibernate return the same session on making multiple invocations of Hibernate's SessionFactory.getCurrentSession(). This is exactly the example I state in my question above - The @Transactional annotation on methodWithRequiredTx() creates a new transaction and then calls serviceBean.methodWithSupportsTx() hence the code executed by methodWithSupportsTx() would already be in a transaction, right? – Andy Dufresne Jun 22 '11 at 11:08
  • @Amit: Yes, if method is called inside an existing transaction, `SUPPORTS` doesn't make any difference. – axtavt Jun 22 '11 at 11:11
  • 6
    Ok. So my question is - in which scenarios is propagation level SUPPORTS useful? – Andy Dufresne Jun 22 '11 at 11:24
-2

A required transaction will create a new transaction if none exists. Therefore a new transaction would be made when you call serviceBean.methodWithSupportsTx(). If your method is truly transactional you will see an error from spring if no transaction exists.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I don't think so. An error is thrown only when the propagation level is NEVER. Also PROPAGATION.SUPPORTS does not create a new transaction. – Andy Dufresne Jun 22 '11 at 10:39