Problem: Executing Example Using Middleware Services

I added the example (from dev manual), 4.1.3 Using Middleware Services

  • class
    -> New method onCalcDiscount(Component)…
  • In the service classes (DiscounService, *Bean, DiscountCalculator)

When executing the application, I get an exception:

Caused by: No active transaction
        at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke( ~[na:na]
        at sun.reflect.GeneratedMethodAccessor108.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke( ~[na:1.8.0_102]
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at com.sun.proxy.$Proxy253.calculateDiscount(Unknown Source) ~[na:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke( ~[na:1.8.0_102]
        at com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.invoke( ~[na:na]
        at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke( ~[cuba-web-6.7.4.jar:6.7.4]
        at com.sun.proxy.$Proxy58.calculateDiscount(Unknown Source) ~[na:na]
        at ~[app-web-0.1-SNAPSHOT.jar:na]
        ... 60 common frames omitted

…any idea what might be wrong?


I have debuged and get the error in the line “Query query = …”:

public class DiscountCalculator {

private Persistence persistence;

 * Calculates discount for a given customer. Expects that an active transaction exists at the moment.
 * @param customerId customer id
 * @return discount value
public BigDecimal calculateDiscount(UUID customerId) {
    // Calculate the total amount on the database level - the fastest possible way
    **Query query = persistence.getEntityManager().createQuery(**

** “select sum(e.amount) from sample$Order e where = :custId”);**
query.setParameter(“custId”, customerId);
BigDecimal sum = (BigDecimal) query.getSingleResult();
if (sum == null)
sum = BigDecimal.ZERO;

I have changed the code …

public BigDecimal calculateDiscount(UUID customerId) {

	EntityManager entityManager =  persistence.getEntityManager();

The method invocation, getEntityManager(), fails …

Any idea!


Hi @peter.w.mundt,

The problem is about no active transaction exists, just wrap your code into
try (Transaction tx = persistence.createTransaction()) { ... tx.commit(); }.

Find an example in the documentation, in the EntityManager chapter.


Hi Aleksey
thank you for your answer!
What I do not understand,
that the example code does not need this additional try{}
so there must be a difference anywhere!
In the configuration?


You are right there is no try, the difference is that the calculateDiscount method of the DiscountServiceBean class is marked as @Transactional.

You may follow the same way :).


Hi Aleksey,
Thanks for your answer
Now it works as expected!
I will continue my journey to discover CUBA!

Here is my solution,
Feel free to use
CUBA_UsingMiddlewareServices.docx (28.4 KB)

1 Like