-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 17, 2009 10:35 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hi all,

I am using Spring, Hibernate 3.3.1.GA and PostgreSQL 8.3.7 in my application.

I want to insert many records into a table under heavy load with optimal performance. For that i have decided to use StatelessSession.

I am using HibernateTemplate in all my DAOs but I guess there is no support for StatelessSession inside the hibernateTemplate. But I read in some other forum that HibernateCallback one can get a hold of the native session, get its connection and based on that open a stateless session. But I am very clear on how to do this.

Any help?

Thanks.


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Mon Sep 21, 2009 10:30 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 6:39 am
Posts: 44
Location: Mumbai, India
Just get the SessionFactory from the Hibernate Session passed to the HibernateCallback and from there create the StatelessSession.

Code:
getHibernateTemplate().execute(new HibernateCallback() {

           public Object doInHibernate(Session session) throws
HibernateException, SQLException {
               StatelessSession statelessSession =
session.getSessionFactory().openStatelessSession();

               ScrollableResults customers = statelessSession
.getNamedQuery("GetCustomers").scroll(ScrollMode.FORWARD_ONLY);
               while ( customers.next() ) {
                   Customer customer = (Customer) customers.get(0);
                   customer.updateStuff(...);
                   statelessSession.update(customer);
               }
               return null;
           }
       });

_________________
Thx,
Murugesan.
Web: http://www.murugesanpitchandi.com


Top
 Profile  
 
 Post subject: another problem using stateless session
PostPosted: Wed Sep 23, 2009 12:54 pm 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Thanks Murugesan!

I have one more problem now using stateless session.

I am trying to save my object Quote in database. whcih is as follows:

Code:
Class Quote{
    String source;
    SortedMap bids = new TreeMap();
}


and mapping file for quote is as follows:

Code:
<class name="Quote" table="QUOTE">
      <id name="id" column="QUOTE_ID">
         <generator class="sequence">
            <param name="sequence">seq1</param>
         </generator>
      </id>      
      <property name="source" column="source"/>
      <map name="bids" table="BIDS" sort="natural">
                <key column="quote_id"/>
                 <index column="price"/>   
         <property name="type"/>
               </map>           
</class>   


Now problem is when I am saving my quote, its inserting values only in quote table, QUOTES and no values are inserted in table for map i.e. BIDS.
Any idea why?

Please give me your inputs.

Thanks.


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Wed Sep 23, 2009 11:42 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2008 6:39 am
Posts: 44
Location: Mumbai, India
Pls post your java code which save Quote and etc.

_________________
Thx,
Murugesan.
Web: http://www.murugesanpitchandi.com


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 24, 2009 2:33 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
I don't see any benefit when using stateless session when having INSERT queries,
it has any benefit, I'm not sure!

Stateless Session does not hold references to persisted objects, that's all, if you want to persist the same instance object with different values that's fine, but I'm not sure where it is a good practice or not.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 24, 2009 4:43 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hi Pasha,

Thanks for your reply.

I have a requirement of inserting high rate incoming data into database.

So using stateless session would not fall over with an OutOfMemoryException. It also confirms from the below thread posted by someone.

viewtopic.php?f=1&t=965733&start=0

So any solution to my problem now??

Any input is valuable.

Thanks.


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 24, 2009 4:51 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Oh thanks Murugesan for your reply!

I should hav eposted that in 1st place but never mind. the code is as below:

Code:
void writeQuote(Quote quote) {
      StatelessSession session = sessionFactory.openStatelessSession();
      Transaction tx = session.beginTransaction();      
      session.insert(quote);      
      tx.commit();
      session.close();   
    }


I tried inserting below in the same transaction after inserting the quote just to check but it doesn't work either.
Code:
session.insert(quote.getBidEntries)


Any idea??

Thanks.


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 24, 2009 5:14 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 6:39 am
Posts: 44
Location: Mumbai, India
Try to print show_sql in console and have a look.

_________________
Thx,
Murugesan.
Web: http://www.murugesanpitchandi.com


Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 24, 2009 5:30 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hi,

Thanks for your reply. I googled more about persisting collections when using stateless sessions.
reason for above behavior is:
Quote:
A stateless session does not implement a first-level cache nor interact with any second-level cache, nor does it implement transactional write-behind or automatic dirty checking, nor do operations cascade to associated instances. Collections are ignored by a stateless session. Operations performed via a stateless session bypass Hibernate's event model and interceptors


Is there any work around to do this?

Any idea?

Thanks.


Last edited by shagufta on Fri Sep 25, 2009 9:37 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: How to open StatelessSession using HibernateCallback?
PostPosted: Thu Sep 24, 2009 7:04 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
I wish there was more on stateless session in "Hibernate in Action"..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.