How to Update an Entity from a Thread in Dropwizar

2020-03-30 06:06发布

问题:

I have a thread that waits until a process is complete and then needs to update an entity. But I get

Exception in thread "Thread-22" org.hibernate.HibernateException: No session currently bound to execution context

I have tried opening a new session (see the code below). Session management is normally handled by @UnitOfWork, but this annotation seems to apply only to resources.

public class ConfigDAO extends AbstractDAO<Config>
{
    private final SessionFactory sessionFactory;

    public ConfigDAO(SessionFactory factory) {
        super(factory);
        this.sessionFactory = factory;
    }

    public Config updateFromNewSession(Config config) {
        System.out.print("current session: ");
        Session session = sessionFactory.openSession();
        System.out.println(session);
        session.persist(config);
        session.close();
        return config;
    }