从Dropwizard-Hibernate中的后台运行线程将数据读取/更新到数据库中

时间:2019-05-30 06:29:38

标签: java hibernate dropwizard

在我的dropwizard项目中,我有一个一直在运行的后台,用于处理队列中的数据。 在该线程中,我将需要从数据库读取数据,然后它将根据执行的过程来更新数据。 现在,当我尝试从线程中的数据库读取数据时,我面临着“ org.hibernate.HibernateException:当前没有会话绑定到执行上下文”的问题。

请帮助我了解如何使用休眠从dropwizard项目中的后台运行线程将数据读写到数据库中。

这是完整的错误堆栈跟踪-

org.hibernate.HibernateException: No session currently bound to execution context
    at org.hibernate.context.internal.ManagedSessionContext.currentSession(ManagedSessionContext.java:58)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:464)
    at io.dropwizard.hibernate.AbstractDAO.currentSession(AbstractDAO.java:44)
    at com.test.kmt.db.SmsDAO.findById(SmsDAO.java:19)
    at com.test.kmt.client.SmsConsumer.sendSms(SmsConsumer.java:59)
    at com.test.kmt.client.SmsConsumer.run(SmsConsumer.java:50)
    at com.test.kmt.client.SmsConsumer_$$_jvst4ac_7._d7run(SmsConsumer_$$_jvst4ac_7.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at io.dropwizard.hibernate.UnitOfWorkAwareProxyFactory.lambda$create$0(UnitOfWorkAwareProxyFactory.java:81)
    at com.test.kmt.client.SmsConsumer_$$_jvst4ac_7.run(SmsConsumer_$$_jvst4ac_7.java)
    at java.lang.Thread.run(Thread.java:748)

使用TestDropwizardApplication.java中的UnitOfWorkAwareProxyFactory初始化SmsConsumer实例

MessageSendingService htMessagingClientService = new MessageSendingServiceImpl(accountDAO);
SmsConsumer smsConsumer = new UnitOfWorkAwareProxyFactory(hibernate).create(MessageSendingService.class, new Class[] { SmsDAO.class, MessageSendingService.class }, new Object[] { smsDAO, messageSendingService });

SmsConsumer.java

public class SmsConsumer implements Runnable {
private SmsDAO smsDAO;
private MessageSendingService messageSendingService;

public SmsConsumer(SmsDAO smsDAO, MessageSendingService messageSendingService) {
    this.smsDAO = smsDAO;
    messageSendingService = messageSendingService;
}

@Override
public void run() {
    while (true) {
        Sms smsDto = RequestSubmitter.getInstance().getOtpQueue().poll();
        sendSms(smsDto);
    }
}

@UnitOfWork
private void sendSms(SmsDto smsDto) {
    try {
        Sms sms = smsDAO.findById(smsDto.getMsgId());
        Response response = (Response) messageSendingService.sendSMS(sms);
        if (response != null) {
                Sms latestSms = smsDAO.findById(sms.getMsgId());
                latestSms.setStatus("sent");
                smsDAO.update(latestSms);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

SmsDAO.java

public class SmsDAO extends AbstractDAO<Sms> {

public SmsDAO(SessionFactory sessionFactory) {
    super(sessionFactory);
}

public Sms findById(String msgId) {
    return currentSession().get(Sms.class, msgId);
}

public String create(Sms sms) {
    sms.setCreateDate(new Date());
    sms.setUpdateDate(new Date());
    return persist(sms).getMsgId();
}

public void update(Sms sms) {
    sms.setUpdateDate(new Date());
    currentSession().saveOrUpdate(sms);
}

}

0 个答案:

没有答案