Transaction is not rolled back when run time exception programmatically fired

46 Views Asked by At

I couldn't understand why the after() function of DBConnectionFilter is called when the runtime exception is thrown. I was expecting onException() function to be called and a rollback of the transaction. But what's really happening is the after() function will be called to commit the transaction and close database connection and after that onException function from CatchAllFilter/kind of global filter/ is called.

AppControllerConfig class:

public class AppControllerConfig extends AbstractControllerConfig{

    public void init(AppContext context) {
        add(new CatchAllFilter(), new DBConnectionFilter("default",true));
    }
}

PeopleController create function:

@POST
public void create() throws IOException {
    String incomingPost = Util.read(getRequestInputStream());

    Map[] people = JsonHelper.toMaps(incomingPost);
    Person newPerson = new Person();
    newPerson.fromMap(people[0]);
    boolean response = newPerson.save();

    //Making sure person info is persisted so that it can be Rolled back   
    if (response == true) {
        throw new InitException("Rollback Transaction");
    }
    render("/system/RestIndex").noLayout().contentType("application/json");
    view("jsonResponse", Person.findAll().toJson(true));

}
1

There are 1 best solutions below

0
On BEST ANSWER

So, looking at the code of ActiveWeb, I could see that the transaction was first rolled back, then committed. I would assume that once the transaction is rolled back, the driver would discard the data that was not committed yet, but it did not happen with MariaDB/MySQL in my tests. In other words, I replicated this issue on my laptop with a simple ActiveWeb project. As such, this bug was filed: https://github.com/javalite/activeweb/issues/389 and already fixed. You can pull a new 2.1-SNAPSHOT version from: http://repo.javalite.io/org/javalite/activeweb/, it should work.