Getting/opening hibernate stateless session in play?

1,522 views
Skip to first unread message

David González

unread,
Jan 11, 2011, 3:45:18 PM1/11/11
to play-fr...@googlegroups.com
I need to do some insertions batches around 9,000 inserts per file so I want to use open an stateless session in play. Is there a way to do this?

--
Best wishes,
David

Olivier Refalo

unread,
Jan 11, 2011, 5:11:53 PM1/11/11
to play-fr...@googlegroups.com
Put another way:
"How can I gain access to the hibernate sessionFactory?"


And I don't have the answer...

David González

unread,
Jan 11, 2011, 5:20:24 PM1/11/11
to play-fr...@googlegroups.com
Yes, I'm looking around in Play! javadocs.. I can't find anything :( the session is already opened is what I guess, but grabbing a hold of it, closing it to open stateless is well.. what I want =/

can I still open the session even if another one is already opened? (i just wonder how hibernate will react, going to try later on to see if I can find a solution)
Else I'll just settle with the current session play has opened.
Any suggestions are welcome :)

Cheers

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.



--
Best wishes,
David

David González

unread,
Jan 11, 2011, 6:10:42 PM1/11/11
to play-fr...@googlegroups.com
Here's my dirty way of doing it, probably not the correct way so I'm waiting for someone like Guillaume or any Play! dev to show the correct way

Session session = (Session) User.em().getDelegate();
StatelessSession stateless = session.getSessionFactory().openStatelessSession();

   stateless.beginTransaction();

for ( int i=0; i<100000; i++ ) {
            Role role = new Role(i + "");
            stateless.insert(role);

}

stateless.getTransaction().commit();
stateless.close();

Kinda impressed, I expected CPU and memory consuming more, consumed less on this laptop.. I guess I worried for no reason. According to some posts in StackOverflow with stateless session you don't need to flush or clear. 

Cheers
--
Best wishes,
David

Guillaume Bort

unread,
Jan 12, 2011, 4:55:25 AM1/12/11
to play-fr...@googlegroups.com
Yes I think that it is ok.

You can open and use as many Hibernate session you want. However the
built-in methods provided by jpa.Model will always use the one created
by the JPAPlugin.

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

David González

unread,
Jan 12, 2011, 8:04:50 AM1/12/11
to play-fr...@googlegroups.com
Ah ha! I was looking forward to know where the heck Play! created the session, weird I opened all the files except JPAPlugin =/

Anyway, a cleaner code with imports you need:

import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

Session session = (Session) JPA.em().getDelegate();
StatelessSession stateless = session.getSessionFactory().openStatelessSession();

   stateless.beginTransaction();

for ( int i=0; i<100000; i++ ) {

            MODEL modelVar = new MODEL(i + "");

            stateless.insert(modelVar);

}

stateless.getTransaction().commit();
stateless.close();

I should let you know that I didn't keep the JCDB batch size perspective.

Cheers, hope this help anyone looking for something similar
Reply all
Reply to author
Forward
0 new messages