Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with threads and hibernate sessions
    primarykey
    data
    text
    <p>Im using hibernate 3 and spring. When I start a thread an exception occurred:</p> <pre><code>org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions </code></pre> <p>I dont know how to detach entities or close session with this architecture. I appreciate some help.</p> <p><code>CommunicationService.sendCommunications()</code> code:</p> <pre><code>public void sendCommunications(HibernateMessageToSendRepository messageToSendRepository) { Long messageId = new Long(41); //this is only for test. the idea is get a list of id and generate a thread group. MessageSender sender = SmsSender(messageId, messageToSendRepository); sender.start(); } </code></pre> <p>Invoking <code>sendCommunications</code> code:</p> <pre><code>ApplicationContext appCont = new ClassPathXmlApplicationContext("appContext.xml"); ServiceLocator serviceLocator = ServiceLocator.getInstance(); HibernateMessageToSendRepository messageToSendRepository = (HibernateMessageToSendRepository) appCont.getBean("messageToSendRepository"); CommunicationService communication = serviceLocator.getCommunicationService(); communication.sendCommunications(messageToSendRepository); </code></pre> <p>SmsSender (extends from MessageSender (thread)) code:</p> <pre><code>public class SmsSender extends MessageSender { public SmsSender(Long messageToSendId, HibernateMessageToSendRepository messageToSendRepository) { super(messageToSendRepository); MessageToSend messageToSendNew = this.messageToSendRepository.getById(messageToSendId); this.messageToSend = messageToSendNew; } public void run() { try { MessageToSendSms messageToSendSms = (MessageToSendSms) this.messageToSend; Iterator&lt;CustomerByMessage&gt; itCbmsgs = messageToSendSms.getCustomerByMessage().iterator(); while (itCbmsgs.hasNext()) { CustomerByMessage cbm = (CustomerByMessage) itCbmsgs.next(); //sms sending this.getGateway().sendSMS(cbm.getBody(), cbm.getCellphone()); cbm.setStatus(CustomerByMessageStatus.SENT_OK); cbm.setSendingDate(Calendar.getInstance().getTime()); } messageToSendSms.getMessage().setStatus(messageToSendStatus.ALL_MESSAGES_SENT); this.messageToSendRepository.update(messageToSendSms); } catch (Exception e) { this.log.error("Error en sms sender " + e.getMessage()); } } } </code></pre> <p><code>MessageToSendRepository</code> code:</p> <pre><code>public void update(MessageToSend messageToSend) { try { this.getSession().update(messageToSend); } catch (HibernateException e) { this.log.error(e.getMessage(), e); throw e; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload