Problem: Executing Example Using Middleware Services

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

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

When executing the application, I get an exception:

Caused by: com.haulmont.cuba.core.global.RemoteException: No active transaction
        at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:129) ~[na:na]
        at sun.reflect.GeneratedMethodAccessor108.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[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(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
        at com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.invoke(LocalServiceInvokerImpl.java:94) ~[na:na]
        at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke(LocalServiceProxy.java:148) ~[cuba-web-6.7.4.jar:6.7.4]
        at com.sun.proxy.$Proxy58.calculateDiscount(Unknown Source) ~[na:na]
        at com.company.sales.web.customer.CustomerBrowse.onCalcDiscount(CustomerBrowse.java:42) ~[app-web-0.1-SNAPSHOT.jar:na]
        ... 60 common frames omitted

…any idea what might be wrong?

Regards,
Peter

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

public class DiscountCalculator {

@Inject
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 e.customer.id = :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!

Peter

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.

Regards,
Aleksey

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?
Regards,
Peter

Peter,

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 :).

Regards,
Aleksey

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

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

1 Like