nik the lion
nik the lion

Reputation: 458

JPA Change default Flushmode from AUTO to COMMIT

I've tried several ways to change the FlushMode to the complete application. Is this right or is there another way to do it?

I don't want to do this pragmmatically.

This is was what i find as property but it isn't work.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
    <persistence-unit name="myPU">
        <properties>
            ...
            <property name="hibernate.connection.autocommit" value="false"/>
            <!-- Also tried this: -->

            <property name="org.hibernate.FlushMode" value="commit"/>
            ...
        </properties>
    </persistence-unit>
</persistence>

Update:

I've created the class as described in the link by zxcf, but i can't figure out how to add this construct in my persistence.xml.

<property name="jpaDialect">
    <bean class="test.jpa.vendor.HibernateJpaDialect">
        <property name="flushMode" value="MANUAL"/>
    </bean>
</property>

Upvotes: 11

Views: 30101

Answers (2)

Vadzim
Vadzim

Reputation: 26160

Ancient Hibernate 3.2.4.sp1 from JBoss 4.2.3.GA supports only the following notation:

<property name="flush-mode" value="commit"/>

Upvotes: 0

Shailendra
Shailendra

Reputation: 9102

Try this

<property name="org.hibernate.flushMode" value="COMMIT"/>

Testing this on a standalone program I can see the changed value of underlying Hibernate Session / EntityManager from AUTO to COMMIT

Here is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
  <persistence-unit name="JPATest" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>  

  <class>com.test.TestEntity</class>  
  <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> 
  <properties> 
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.use_sql_comments" value="true"/>
      <property name="hibernate.cache.use_query_cache" value="true"/>
      <property name="hibernate.archive.autodetection" value="class, hbm"/>     
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.connection.username" value="xxx"/>
      <property name="hibernate.connection.password" value="xxx"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true"/>      
       <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
       <property name="hibernate.cache.provider_configuration" value="classpath:ehcache.xml"></property>
      <property name="hibernate.use.second.level.cache" value="true"/>
      <property name="hibernate.cache.region_prefix" value="neutrino.jpa.cache"/>
      <property name="hibernate.cache.use_query_cache" value="false"/>
      <property name="hibernate.generate_statistics" value="true"/>
      <property name="hibernate.jdbc.batch_size" value="10"/>
      <property name="hibernate.order_updates" value="true"/>
      <property name="hibernate.order_inserts" value="true"/>
    <property name="org.hibernate.flushMode" value="COMMIT"/>    
  </persistence-unit>
</persistence>

And here is how I test it

 EntityManagerFactory emf  = Persistence.createEntityManagerFactory("JPATest");
 EntityManager em = emf.createEntityManager();
 Session session = em.unwrap(Session.class);
 System.out.println("Underlying Hibernate session flushmode #######         "+session.getFlushMode());
 System.out.println("EntityManager flushmode                #######         "+em.getFlushMode());

This gives me

Underlying Hibernate session flushmode #######         COMMIT
EntityManager flushmode                #######         COMMIT

If I omit the property in presistence.xml, I get this

Underlying Hibernate session flushmode #######         AUTO
EntityManager flushmode                #######         AUTO

Upvotes: 11

Related Questions