Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails - how to save a domain object inside a Service?
    primarykey
    data
    text
    <p>I have a service and inside one of the functions i'm creating a domain object and trying to save it.<br> when it gets to the save part, i get the error</p> <blockquote> <p>No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here</p> </blockquote> <p>What do i need to do in order to save a domain object inside of a service. everything on the internet makes it look like this should just work....</p> <p><strong>Edit:</strong><br> additional details: I stumbled across this post<br> <a href="https://stackoverflow.com/questions/4278090/grails-hibernate-session-in-threads">Hibernate session in threads</a> </p> <p>which is a similar scenario. My service is getting called by a 3rd party API.</p> <p><strong>Edit:</strong><br> I'm not explaining this very well. Here is more complete code</p> <pre><code>import org.springframework.beans.factory.InitializingBean import com.ib.client.EWrapper; class BrokerService implements InitializingBean, EWrapper{ static transactional = true private EClientSocket m_client private boolean m_disconnectInProgress = false void afterPropertiesSet(){ // this.setting = grailsApplication1.config.setting m_client = new EClientSocket(this) m_disconnectInProgress = false connect() } def boolean connect() { m_client.eConnect() if (m_client.isConnected()) return true return false } def void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) { HistoricalContractData.withNewSession{session-&gt; println ' just before object create' def hcd = new sbi.investments.HistoricalContractData() hcd.hc_id = reqId hcd.data_date = new Date().parse('yyyyMMdd', date.replace('finished-', '')) hcd.open = open hcd.high = high hcd.low = low hcd.close = close hcd.volume =volume hcd.trade_count =count hcd.wap = WAP hcd.has_gaps = hasGaps.toString() println ' just before save' hcd.save() if(hcd.hasErrors()){ println '=========== ERROR! ============' println hcd.errors } } } } </code></pre> <p>the 3rd party API is calling historicalData several times. With the above code, it is saving the first record, but then on the 2nd record i'm getting the error:</p> <blockquote> <p>Could not open Hibernate Session; nested exception is org.hibernate.SessionException: Session is closed!</p> </blockquote> <p><strong>Edit:</strong><br> so reading up on this more i think i understand what is happening.<br> a hibernate session is usually injected into the Service when called from the controller.<br> Because historicalData is being called from a 3rd party app and not via a controller, no hibernate session is getting injected into the service so it complains that the Session is closed. </p> <p>So I think the real question may be, if a service is not called from a controller, how do i create a new hibernate session in order to save a grails domain model object (i.e. HistoricalContractData).<br> As can be seen above, withNewSession is not working. Should i be using a SessionFactory like so ?<br> (can't post link to source because stack overflow doesn't like it)</p> <pre><code>import org.hibernate.Session; import org.hibernate.SessionFactory; public class YourService { SessionFactory sessionFactory // set by Dependency Injection public void yourMethod() { Session session = sessionFactory.getCurrentSession(); // do something with session } } </code></pre> <p>I kind of tried this but don't understand how to use the session object in order to save the HistoricalContractData object.</p>
    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.
 

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