Axon Unit of work not working.

Hi, I am using axon version3.0-M5. while calling repository.load, it showing “no unity of work found”. The following is my configuration. Could you please help me.

@EnableAxonAutoConfiguration

@SpringBootApplication

public class BookCqrsApplication {

public static void main(String[] args) {

SpringApplication.run(BookCqrsApplication.class, args);

}

@Bean

public EventStorageEngine eventStorageEngine() {

return new InMemoryEventStorageEngine();

}

@Bean

public Repository jpaRepo(EventBus eventBus) {

return new GenericJpaRepository<>(entityManagerProvider(), Book.class, eventBus);

}

@Bean

public EntityManagerProvider entityManagerProvider() {

return new ContainerManagedEntityManagerProvider();

}

@PersistenceUnit

private EntityManagerFactory entityManagerFactory;

@Autowired

private SpringTransactionManager springTransactionManager;

@Bean

public SpringTransactionManager springTransactionManager() throws Exception {

return new SpringTransactionManager(transactionManager());

}

@Bean

public PlatformTransactionManager transactionManager() throws PropertyVetoException {

return new JpaTransactionManager(entityManagerFactory);

}

@Bean

public TransactionManagerFactoryBean transactionManagerFactoryBean() throws PropertyVetoException {

TransactionManagerFactoryBean factoryBean = new TransactionManagerFactoryBean();

factoryBean.setTransactionManager(transactionManager());

return factoryBean;

}

}

Hi,

I’m unsure what happens here:

UnitOfWork uow = DefaultUnitOfWork.startAndGet(transactionManager);

That method only takes Messages so I’m not sure how the code compiles (or it may be that something went wrong while copy-pasting the code).

Note that the command bus creates a unit of work for you (rarely if ever do you need to create a unit of work yourself). So if you dispatch a command to create a book it shouldn’t complain about a missing unit of work.

Regards,
Rene

Hi

Very Very thanks for your reply. :slight_smile:

I am very new to Axon and CQRS. My intention was to create a BookStore in Mysql DB using JPA+Spring and Axon.
I am getting the following exception while executing the bellow code. (I removed the UnitofWork code for now).
Could you please check it and reply, what the error is.

(I am attaching my complete source code with this mail)

Controller

src.zip (8.74 KB)

Hi,

the problem is in the jpaRepo.load() call (I suppose this is an Axon repository?). You’re not supposed to load the book from the repository yourself. Instead, send a command on the Command Bus and let Axon manage loading the aggregate instance for you. You retrieving the book, but only using the ID from the retrieved object (which is the same as the input, I presume) to create the command.

Check out the Getting Started webinar to see how this works: https://www.youtube.com/watch?v=s2zH7BsqtAk

Hope this helps.

Cheers,

Allard