Axon Unit of work not working.

333 views
Skip to first unread message

Prasanth P Menon

unread,
Oct 20, 2016, 10:33:44 PM10/20/16
to Axon Framework Users
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<Book> 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;

}


}


=========================================================================================


@RestController

public class BooksController {


private CommandBus commandBus;

private Repository<Book> bookRepository;

@Autowired

PlatformTransactionManager transactionManager;

public BooksController(CommandBus commandBus,Repository<Book> bookRepository,PlatformTransactionManager transactionManager) {

this.commandBus=commandBus;

this.bookRepository=bookRepository;

this.transactionManager=transactionManager;

}

@RequestMapping(value="/create",method=RequestMethod.GET)

@Transactional

public void createBook(){


UnitOfWork uow = DefaultUnitOfWork.startAndGet(transactionManager);


}

}

René de Waele

unread,
Oct 20, 2016, 11:42:34 PM10/20/16
to axonfr...@googlegroups.com
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
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Prasanth P Menon

unread,
Oct 21, 2016, 2:01:55 AM10/21/16
to Axon Framework Users

Prasanth P

unread,
Oct 21, 2016, 2:08:01 AM10/21/16
to axonfr...@googlegroups.com

Hi

Very Very thanks for your reply. :) 

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
###########
@RestController
public class BooksController {

private CommandBus commandBus;
private Repository<Book> jpaRepo;

public BooksController(CommandBus commandBus, Repository<Book> jpaRepo) {
this.commandBus = commandBus;
this.jpaRepo = jpaRepo;
}

@RequestMapping(value = "/create", method = RequestMethod.GET)
@Transactional
public void createBook() {
Book book = (Book) jpaRepo.load("1");
commandBus.dispatch(asCommandMessage(new UpdateBookCommand(book.getBookId(), "test123", 10)));
}

}


Spring Boot
##############
Exception
##################
java.lang.IllegalStateException: No UnitOfWork is currently started for this thread.
at org.axonframework.messaging.unitofwork.CurrentUnitOfWork.get(CurrentUnitOfWork.java:85) ~[axon-core-3.0-M5.jar:3.0-M5]
at org.axonframework.commandhandling.model.AbstractRepository.load(AbstractRepository.java:97) ~[axon-core-3.0-M5.jar:3.0-M5]
at org.axonframework.commandhandling.model.AbstractRepository.load(AbstractRepository.java:110) ~[axon-core-3.0-M5.jar:3.0-M5]
at com.book.controller.BooksController.createBook(BooksController.java:29) ~[classes/:na]
at com.book.controller.BooksController$$FastClassBySpringCGLIB$$8396cd2c.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) ~[spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at com.book.controller.BooksController$$EnhancerBySpringCGLIB$$f72bf3c4.createBook(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:89) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410) [tomcat-embed-core-8.5.5.jar:8.5.5]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.5.jar:8.5.5]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_66]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.5.jar:8.5.5]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]


To unsubscribe from this group and stop receiving emails from it, send an email to axonframework+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Axon Framework Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/axonframework/hb0Ez5JXdws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to axonframework+unsubscribe@googlegroups.com.
src.zip

Allard Buijze

unread,
Oct 24, 2016, 9:57:57 AM10/24/16
to axonfr...@googlegroups.com
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

To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Axon Framework Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/axonframework/hb0Ez5JXdws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to axonframewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages