Spring: Annotation-based Transactions and DAOs

I’ve used Spring’s Transaction Manager and Transaction Proxy Factory classes before, but I hadn’t noticed before today that there’s an option for Annotation-based Transaction demarcation.

Here’s how it works.

First create a DAO interface:

import static org.springframework.transaction.annotation.Isolation.SERIALIZABLE;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

import org.springframework.transaction.annotation.Transactional;

public interface AccountDao {
  public Account load(Integer accountNumber);
  public void save(Account account);

Use the special “Transactional” annotation.

Then implement this DAO as normal.

In your Spring configuration file, include the following:

<bean id="ca.intelliware.example.AccoutDao"
  <property name="proxyInterfaces">
  <property name="target">
  <bean class="ca.intelliware.example.AccountDaoImpl">
    <property name="sessionFactory" ref="org.hibernate.SessionFactory" />
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributeSource">
    <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />

And, voila! You can now define your transactionality based on the annotations on your interface.

Unit Testing Transactionality

I wanted to prove to myself that this really worked, so I wrote a quick unit test to prove it. I often use an in-memory database (HSQLDB) for unit tests. So the first trick was to tweak the JDBC Driver to make it possible to interrogate the transaction settings.

First, I overrode the getConnection methods, so that I could wrap the connections in a Proxy interface.

public class HSQLDBDataSourceWrapper extends jdbcDataSource {

  public Connection getConnection() throws SQLException {
    return (Connection) Proxy.newProxyInstance(
      null, new Class[] { Connection.class },
      new ConnectionHandlerImpl( super.getConnection()));

  public Connection getConnection(String user, String password)
      throws SQLException {
    return (Connection) Proxy.newProxyInstance(
      null, new Class[] { Connection.class },
      new ConnectionHandlerImpl( super.getConnection(
         user, password)));

Next, I created an InvocationHandler to track specific events. This handler was an inner class of my wrapper:

public class ConnectionHandlerImpl implements InvocationHandler {
  private final Connection connection;
  public ConnectionHandlerImpl(Connection connection) {
    this.connection = connection;
  public Object invoke(Object proxy, Method method, Object[] args)
      throws Throwable {
    if ("setAutoCommit".equals(method.getName())) {
      if (Boolean.FALSE.equals(args[0])) {
    } else if ("commit".equals(method.getName())) {

    return method.invoke(this.connection, args);

And I added some static final members to my wrapper to keep track of the most recent state of the connection.

Now I wrote my JUnit test:

public void testTransactionality() throws Exception {
  Account account = new Account();
  accountsetName("Kermit the Frog's account");

  assertTrue("write transaction started",
  assertTrue("write transaction committed",


This seemed a lot easier (to me, anyway) than messing with jMock or anything like that.

I even evolved my wrapper so that I could set some switches that allowed me to throw an SQLException when Hibernate issued an executeUpdate on a prepared statement, then assert that the transaction was rolled back.

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply