Use entity manager in scheduled service call


I’m having trouble getting a service called through the scheduler and have it working.

The service (still work in progress) aims to send out reminders via email on a number of objects that have reached their deadline. Obviously, I want the function to run once a day through the scheduler.

This is my service code (stripped).

 * Copyright (c) 2017, Axxemble
package com.axxemble.base27.service;

import com.axxemble.base27.entity.CounterMeasure;
import com.axxemble.base27.entity.Staff;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.EntityManager;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.Query;
import com.haulmont.cuba.core.entity.Entity;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReminderServiceBean implements ReminderService {
    private EmailService emailService;

    private Persistence persistence;

    private GlobalConfig globalConfig;

    private Messages messages;

    public void sendReminders(){
        // Get objects of type that have date set to today, for these send emails to owners
        EntityManager em = persistence.getEntityManager();

        // CMs
        Query query = em.createQuery("select from base$CounterMeasure o where o.status < 4 "+
                                                       // "and @between(o.createTs, now, now+1, day) "+
                                                        "order by o.owner");
        List<Object[]> list = query.getResultList();
        list.forEach((record) -> {
            CounterMeasure cm = em.reload((CounterMeasure) record[0], View.LOCAL);
            sendMail(cm.getTitle(), cm.getOwner());


    private void sendMail(String body, Staff owner) {
        // Get owner's email
        owner = persistence.getEntityManager().reload(owner, View.LOCAL);
        if (owner != null && owner.getEmail() != null) {
            // Build email
            Map<String, Serializable> params = new HashMap<>();
            params.put("body", body);
            params.put("link", globalConfig.getWebAppUrl());

            EmailInfo emailInfo = new EmailInfo(
                    owner.getEmail(), // recipients
                    "Base27: " + messages.getMessage(getClass(), "subjectReminder"), // subject
                    "", // the "from" address will be taken from the "" app property
                    "com/axxemble/base27/templates/email.txt", // body template
                    params // template parameters


When called through the scheduler I get this error:

ERROR c.h.cuba.core.sys.ServiceInterceptor - Exception:
java.lang.IllegalStateException: No active transaction
        at com.haulmont.cuba.core.sys.PersistenceImpl.getEntityManager( ~[cuba-core-6.4.5.jar:6.4.5]
        at com.haulmont.cuba.core.sys.PersistenceImpl.getEntityManager( ~[cuba-core-6.4.5.jar:6.4.5]
        at com.axxemble.base27.service.ReminderServiceBean.sendReminders( ~[app-core-0.1-SNAPSHOT.jar:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke( ~[na:1.8.0_121]

The ‘at com.axxemble.base27.service.ReminderServiceBean.sendReminders(’ refers to this line in the service bean:

EntityManager em = persistence.getEntityManager();

Somehow there is no option to work with the entity manager when called through the scheduler?

Any help appreciated.

1 Like


First of all, you have to start DB transaction Persistence.getEntityManager() can be invoked only if there is an active transaction in the current thread.


Thanks Yuriy, worked as expected following the indicated topic.
Regards, Berend