Newbie: Could not clear this UnitOfWork. It is not the active one

Why do you get this error in general terms?

I am working on replacing my PoC InMemoryEventStore and query models stored in HashMap to JPA versions, so my program can survive restart :slight_smile:
In the debugger it looks like Axon finds the Spring TransactionManager and gets the UoW bound to a transaction.

I get this exception when dispatching the command:

java.lang.IllegalStateException: Could not clear this UnitOfWork. It is not the active one.
at org.axonframework.messaging.unitofwork.CurrentUnitOfWork.clear(
at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.commit(
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(
at org.axonframework.commandhandling.SimpleCommandBus.handle(
at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(
at org.axonframework.commandhandling.SimpleCommandBus.dispatch(
at org.axonframework.commandhandling.gateway.AbstractCommandGateway.send(
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.sendAndWait(

are you doing anything “manually” with Unit of Work anywhere? Like committing it, or rolling one back?

This error means that a Unit of Work is being committed, but it is not currently the active one. It might be because there is Unit of Work nesting and the inner unit of work hasn’t been committed or rolled back, yet. Generally, you should not have to bother with Unit of Work, and you should also not see this message, unless you start managing your own unit of work.



I didn’t even know about the Unit of Work concept before I got this error :slight_smile:

So I don’t actively try to manage my unit of works manually.

It turned out to be a bad JPA mapping that caused the problem. A OneToMany/ManyToOne problem.
Turning on sql statement logging revealed it.

Thank you.

Glad you found it. I am curious why you would get this exception (and not something Hibernate related), though. Will investigate that.

Not knowing about the Unit of Work is a good thing (mostly), so forget everything I said :wink:



I investigated a bit more and the error I got when I get the Unit of Work exception is actually a stackoverflow.

You can get that by making two @Entity classes with a simple parent / child relationship with JPA annotations, and then use the @Data annotation from Lombok, instead of getters and setters.
Apparently that combination produces an infinite loop because hibernate will call the generated lombok tostring/hashcode methods, that call each other…

But I also wonder why the error got swallowed by axon. It was hard enough to find as it was :wink: