users@glassfish.java.net

Re: Lock wait timeout exceeded problem

From: <glassfish_at_javadesktop.org>
Date: Mon, 01 Sep 2008 07:29:33 PDT

Sorry for the delay and thank you for your interest. I've attached some stripped code, hope it is enough to demonstrate.

@MessageDriven(mappedName = "jms/ConcurrentUpdates", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class ConcurrentUpdatesMessageBean implements MessageListener {
    @EJB
    private InstrumentsManagerLocal instrumentsManagerBean;

    public void onMessage(Message message) {
            instrumentInformation = instrumentsManagerBean.getInstrument(instrumentSourceDescription);
            instrument = new StandardInstrument(instrumentInformation);
            updateInstrument(instrument, endDate.getTime());
    }

    private void updateInstrument(Instrument instrument, Date date) {
            instrument.goToDate(date))
    }


}

@Stateless(mappedName = "InstrumentsManagerBean")
public class InstrumentsManagerBean implements InstrumentsManagerRemote, InstrumentsManagerLocal {
    public InstrumentInformation getInstrument(String description) {
        InstrumentInformation res = null;

        try {
            Query query = this.em.createQuery("SELECT i FROM InstrumentInformation i WHERE i.sourceDescription = :description");
            query.setParameter("description", description);
            res = (InstrumentInformation) query.getSingleResult();
        } catch (NoResultException e) {
            e.printStackTrace();
            System.out.println("Error on description: " + description);
        }
        em.flush();

        return res;

    }
}

public class StandardInstrument extends Instrument {

    private LogicSourcesManagerRemote sourcesManager;
    private LogicSource source;

    public StandardInstrument(InstrumentInformation instrument) {
        this.sourcesManager = this.lookupLogicSourcesManagerBean();
        source = this.sourcesManager.getLogicSource(this.instrumentInformation.getSourceInformation());
        
    }

    public boolean goToDate(Date date) {
                        this.sourcesManager.updateSource(instrumentInformation.getSourceInformation(), price);

    }

}


@Stateless(mappedName = "LogicSourcesManagerBean")
public class LogicSourcesManagerBean implements LogicSourcesManagerLocal, LogicSourcesManagerRemote {
    @PersistenceContext
    private EntityManager em;

    public LogicSource getLogicSource(SourceInformation sourceInformation) {
        LogicSource res = null;
        try {
            Query query = em.createQuery("SELECT s FROM LogicSource s WHERE s.sourceIdentifier =:si and s.instrumentIdentifier = :ii and s.instrumentTicker = :it");
            query.setParameter("si", sourceInformation.getSourceDescription());
            query.setParameter("ii", sourceInformation.getInstrumentDescription());
            query.setParameter("it", sourceInformation.getInstrumentTicker());

            res = (LogicSource) query.getSingleResult();
        } catch (NoResultException e) {
            e.printStackTrace();
            logger.warning("Logic source was not found. Creating...");
        } catch (NonUniqueResultException e) {
            e.printStackTrace();
            logger.warning("More sources with source information were found");
        }

        if (res == null) {
            LogicSource source = new LogicSource();
            source.setInstrumentIdentifier(sourceInformation.getInstrumentDescription());
            source.setInstrumentTicker(sourceInformation.getInstrumentTicker());
            source.setSourceIdentifier(sourceInformation.getSourceDescription());

            em.persist(source);

            try {
                Query query = em.createQuery("SELECT s FROM LogicSource s WHERE s.sourceIdentifier =:si and s.instrumentIdentifier = :ii and s.instrumentTicker = :it");
                query.setParameter("si", sourceInformation.getSourceDescription());
                query.setParameter("ii", sourceInformation.getInstrumentDescription());
                query.setParameter("it", sourceInformation.getInstrumentTicker());

                res = (LogicSource) query.getSingleResult();
                res = source;
            } catch (NoResultException e) {
                e.printStackTrace();
                logger.warning("Logic source was not found after creation");
            } catch (NonUniqueResultException e) {
                e.printStackTrace();
                logger.warning("More sources with source information were found after creation");
            }

            em.flush();

        }

        Vector<Price> prices = (Vector<Price>) res.getPrices();
        for (Price price : prices) {
        }

        return res;
    }

    public void updateSource(final Object sourceInformation, Object currentPrice) {
        LogicSource source = this.getLogicSource((SourceInformation) sourceInformation);
        source.getPrices().add((Price) currentPrice);
    }
}

Thank you,
[Message sent by forum member 'coboo' (coboo)]

http://forums.java.net/jive/thread.jspa?messageID=296555