Performing a DML database operation and sending a JMS message in a single transaction

239 Views Asked by At

Please take a look at this example:

@Component
public class TransactionTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Transactional
    public void doTest() {
        jdbcTemplate.execute("INSERT INTO some_table VALUES ('some_value')");
        jmsTemplate.convertAndSend("some_queue", "some_value");
        ...
        throw new SomeException();
    }

    ...

}

For consistency purposes I want to perform both operations (inserting a database row and sending a JMS message) atomically so when SomeException will be raised then both operations will be rolled back (no new row will be in a database and no new JMS message will be in a queue).
Are there any mechanisms to handle this? I'm using Spring Framework, PostgreSQL, and Apache ActiveMQ for now.

0

There are 0 best solutions below