/ SPRING, TRANSACTION

A Spring hard fact about transaction management

In my Hibernate hard facts article serie, I tackled some misconceptions about Hibernate: there are plenty of developers using Hibernate (myself including) that do not use it correctly, sometimes from a lack of knowledge. The same can be said about many complex products, but I was dumbfounded this week when I was faced with such a thing in the Spring framework. Surely, something as pragmatic as Spring couldn’t have shadowy areas in some corner of its API.

About Spring’s declarative transaction boundaries

Well, I’ve found at least one, in regard to declarative transaction boundaries:

Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.

— Spring's documentation

Guess what? Even though it would be nice to have transactional behavior as part of the contract, it’s sadly not the case as it depends on your context configuration, as stated in the documentation! To be sure, I tried and it’s (sadly) true.

Consider the following contract and implementation :

public interface Service {

  void forcedTransactionalMethod();

  @Transactional
  void questionableTransactionalMethod();
}

 public class ImplementedService implements Service {

  private DummyDao dao;

  public void setDao(DummyDao dao) {
    this.dao = dao;
  }

  @Transactional
  public void forcedTransactionalMethod() {
    dao.getJdbcTemplate().update("INSERT INTO PERSON (NAME) VALUES ('ME')");
  }

  public void questionableTransactionalMethod() {
    dao.getJdbcTemplate().update("INSERT INTO PERSON (NAME) VALUES ('YOU')");
  }
}

Now, depending on whether we activate the CGLIB proxy, the questionableTransactionMethod behaves differently, committing in one case and not in the other.

For Eclipse users - even without Spring IDE, this is shown as a help popup when CTRL-spacing for a new attribute (it doesn’t show when the attribute already exists in the XML though).

Additional facts for proxy mode

Spring’s documentation also documents two other fine points that shouldn’t be lost on developers when using proxies (as opposed to AspectJ weaving):

  • Only annotate public methods. Annotating methods with other visibilities will do nothing - truly nothing - as no errors will be raised to warn you something is wrong
  • Be wary of self-invocation. Since the transactional behavior is based on proxys, a method of the target object calling another of its method won’t lead to transactional behavior even though the latter method is marked as transactional

Both those limitations can be removed by weaving the transactional behavior inside the bytecode instead of using proxies (but not the first limitation regarding annotations on interfaces).

You can found the sources for this article in Eclipse/Maven format here (but you’ll have to configure a MySQL database instance).

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
A Spring hard fact about transaction management
Share this