1

I have a problem when testing JTA1.2 @Transactional annotation on Glassfish 4.1 application server. If I run execute() method of this bean:


@Named
@RequestScoped
public class IndexController {

    @Resource(name = "ds")
    private DataSource ds;

    @Transactional
    public void execute() throws SQLException, SystemException {
        try (Connection con = ds.getConnection();) {
            try (PreparedStatement ps = con.prepareStatement(
                    "INSERT INTO test(id) VALUES(1)"
            );) {
                ps.executeUpdate();
                throw new IllegalArgumentException();
            }
        }
    }
}

I get expected error:

    Caused by: javax.transaction.RollbackException: Transaction marked for rollback.

but when I execute select statement:

SELECT * FROM test;

I see that row was inserted. What's wrong?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Have you checked this? http://stackoverflow.com/questions/16301315/spring-transactional-and-jdbc-autocommit – Multisync Oct 22 '14 at 14:19

1 Answers1

0

What DB you are using and in which mode it is? Maybe I don't see it, but where are you doing a rollback? So the entry will still be temporarily written to DB until you rollback. Try this:

@Transactional(rollbackOn={Exception.class})

And normaly in a catch block you will call the rollback method. Because the transaction is only marked to be rolled back and you be responsable to roll it back.

aw-think
  • 4,723
  • 2
  • 21
  • 42
  • Always try to add the relevant code into your answers instead of just giving links and references. It will be much more helpful to the person asking the question. – Stewartside Oct 22 '14 at 14:08