-->
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.  [ 4 posts ] 
Author Message
 Post subject: Setting FlushMode.COMMIT on Session Creation
PostPosted: Mon Nov 09, 2015 4:09 pm 
Newbie

Joined: Mon Nov 09, 2015 4:00 pm
Posts: 1
Hi,
I'm using Hibernate ORM v4.3.11.Final. I've found that changing the sessions flushMode to FlushMode.COMMIT gives me greatly improved performance when doing batch oriented operations. The problem I'm having is that there does not seem to be any good place to set the flushMode property globally. I figured there might be a way to have it set in the hibernate.cfg.xml file but didn't find anything. Then I thought I could override some method of the SessionFactory or SessionBuilder when the session is created and set the FlushMode but couldn't find any documentation on this. Has anyone else figured out how to accomplish this? Thanks in advance.

-Andrew


Top
 Profile  
 
 Post subject: Re: Setting FlushMode.COMMIT on Session Creation
PostPosted: Mon Dec 07, 2015 5:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There used to be a "org.hibernate.flushMode" property but it's not supported anymore.
You can open an issue if you'd like such an option.
Otherwise, you need to intercept the Session creation in your application code and set the flush mode to COMMIT before returning the Session to the data access layer.


Top
 Profile  
 
 Post subject: Re: Setting FlushMode.COMMIT on Session Creation
PostPosted: Mon Dec 07, 2015 1:28 pm 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Yes, you can intercept it like this:

(1) Intercept hibernate API
Code:
public static SessionFactory wrapSessionFactory(final SessionFactory sessionFactory) {

     return (SessionFactory)Proxy.newInstacne(
          SessionFactory.class.getClassLoader(),
          sessionFactory.getClass().getInterfaces(),
          new InvocationHandler(Object proxy, Method method, Object[] args) throws Throwable {
              Object retval = method.invoke(sessionFactory, args);
              if (retval instanceof Session && method.getName.equals("openSession") {
                    Session session = (Session)retval;
                    session.setFlushMode(FlushMode.COMMIT);
              }
              return retval;
          }
     );
}


(2) Intercept JPA
Code:
public static EntityManagerFactory wrapEntityManagerFactory(final EntiyManagerFactory entityManagerFactory) {

     return (EntityManagerFactory)Proxy.newInstacne(
          EntiyManagerFactory.class.getClassLoader(),
          entityManagerFactory.getClass().getInterfaces(),
          new InvocationHandler(Object proxy, Method method, Object[] args) throws Throwable {
              Object retval = method.invoke(entityManagerFactory, args);
              if (retval instanceof EntiyManager && method.getName.equals("createEntityManager") {
                    EntityManager entityManager = (EntityManager)retval;
                    entityManager.unwrap(Session.class).setFlushMode(FlushMode.COMMIT);
              }
              return retval;
          }
     );
}


Top
 Profile  
 
 Post subject: Re: Setting FlushMode.COMMIT on Session Creation
PostPosted: Tue Dec 08, 2015 2:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.