-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to really controll the flush for batch process?
PostPosted: Mon Mar 27, 2006 3:12 pm 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
Dear members,

I am using Hibernate + Spring for the following problem (batch process):

1. Reading the objects from a flat file.

2. Processing the information (validation, checking, etc, some queries are inviked in order to verify some information)

3. Saving into database.

I just want to flush the data just on step 3, becase on step 1, 2 the data probably is are not complete. On the step 2 I have to make some queries in order to verify the data already exist on the data base, etc (before step 3).

So I crearly don't need any synchronization with the input data into data base until I get into step 3, but hibernate makes some flushing in the middle of step 2. I would like to avoid this situation. Do you have any idea how to configure hibernate in order to force the flush will be done only by the user?

The domain objets representing my problem where generated using hbm2java, so they are mapped into the corresponding database tables.

For the step 3 I am using a Dao's extending HibernateDaoSupport Spring class and invoking the method:


Code:
getHibernateTemplate().saveOrUpdate(contract);


for each element to save. I have added some safeguard for flushing the data after a lot of insertions/updates on the Dao Hibernate implementation:

Code:
if (counter % getMaxBatchSize() == 0) {
  session.flush();
  session.clear();
}


the whole process is embeded on a transaction proxy, so the batch method is transactional.

Do you have any idea about how to control this flush process by the user only, because before to the step 3 Hibernate made some automatic flush operations.

Thanks in advance,

_________________
David Leal


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 3:26 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Have you tried using Session.setFlushMode(FlushMode) ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 7:47 pm 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
Thanks the-gtm, about your comment:

the-gtm wrote:
Have you tried using Session.setFlushMode(FlushMode) ?


I want to implement this idea, but I don't know how to setup it with Spring. With Spring you can define on the configuration file the SessionFactory bean, but I don't see the say to setup the flush mode from the SessionFactory definition. On spring such definition could be:

Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
  <ref bean="dataSource"/></property>
<property name="mappingResources">
<list>
  <value>file1.hbm.xml</value>
  <value>file2.hbm.xml</value>
  ...
</list>         
<property name="hibernateProperties">
  <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    ...
  </props>
</property>
</bean>


I know it is more specific Spring question, but becuase the combination Spring + Hibernate is more popular, I hope some forum member will give me some input abut this configuration.

Thanks in advance,

David

_________________
David Leal


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 11:46 am 
Newbie

Joined: Tue Mar 28, 2006 11:37 am
Posts: 3
Location: Moscow
I have similar problem.

I use spring LocalSessionFactoryBean with hibernate 3.x (dont remember versions and haven't code sniplets now)

My web-application need to store some information in MySQL database after user entered it in form. Spring's controller invokes method of my DAO that invokes hibernate related logic to access database. Aplication logic seems to work correctly but when i look to database nothing was stored there.

I suppose hibernate caches objects and not storing tham to database. How can i turn off all hibernate caches?

_________________
Nothing is as easy as it looks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 2:15 pm 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
Dear dohque,

I am asking for the same problem on the spring forum, look at:

http://forum.springframework.org/showth ... #post56184

Costin says that when your are running on a transaction the flush mode is automatically set to NEVER.

_________________
David Leal


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 2:41 pm 
Newbie

Joined: Tue Mar 28, 2006 11:37 am
Posts: 3
Location: Moscow
i have code:

HibernateTemplate ht = new HibernateTemplate(sessionFactory);
ht.save(idea);
ht.flush();

i added flush to be sure that everything is saved. But nothing happens.
I suppose that transaction is not commited by spring.

Hibernate 3.0.5
Spring 1.2.6
MySQL 5.0.17

mapping:

<hibernate-mapping>
<class name="net.bindingsite.Idea" table="IDEAS">
<id name="name"/>
<property name="content" type="text"/>
<property name="creationTs" type="timestamp" column="CREATION_TS"/>
<property name="rank" type="double"/>
<property name="author"/>
<property name="owner"/>
</class>
</hibernate-mapping>

no exceptions

spring's bean xml

<bean id="ideaDao" class="net.bindingsite.HibernateIdeaDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:/hibernate.cfg.xml"/>
</bean>

Hibernate config:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/binder?useUnicode=true&amp;characterEncoding=utf-8</property>
<property name="connection.username">binder</property>
<property name="connection.password">binder</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>

<!-- Drop and re-create the database schema on startup -->
<!-- property name="hbm2ddl.auto">create</property -->

<mapping resource="net/bindingsite/Idea.hbm.xml"/>
</session-factory>
</hibernate-configuration>

_________________
Nothing is as easy as it looks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 2:54 pm 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
Dohque,

I think you should define a transaction in your problem. I have done the following:

Code:
   <!-- TransactionInterceptor, we define here the selection criteria to
        be used in order to detect the method that are going to be considered
    -->
    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <description>
            Defines the interception criteria for transactions, that is
            where the transaction will be activated and characteristics
            of such transaction.
        </description>
        <property name="transactionManager">
            <ref bean ="transactionManager"/>
        </property>
        <property name="transactionAttributes">
            <props>
                <!--Put here methods will be transactional, you can use
                    wild cards-->
                <!--prop key="process">PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE,timeout_60,-ApplicationException</prop-->
                <prop key="process">PROPAGATION_REQUIRED</prop>
                <prop key="batch">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
   
    <!--
        We define here the interfaces will be considered have transactional methods.
    -->
   
    <bean id="autoProxyCreator"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <value>transactionInterceptor</value>
        </property>
        <property name="beanNames">
            <!--Add here the bean that should have transactional methods-->
            <list>
                <idref local="positionBatch"/>
                <idref local="holderBatch"/>
                <idref local="movementBatch"/>
            </list>
        </property>
    </bean>


so, the methods: batch and process are transactional, such method are defined on the following bean:

Code:
<bean id = "positionBatch"
        class="com.schinvest.lra.business.batch.position.PositionBatchImpl"
        singleton = "true" parent ="baseService">
        <description>
            Carry out the Position Batch process, here we define the
            dependence implementation, although we refer to other beans.
        </description>
        <property name="companyDao">
            <ref bean="companyDao"/>
        </property>
        <property name="parser">
            <ref local="positionInputParser"/>
        </property>
    </bean>


for example positionBatch bean implements the interface PositionBatch, for example.

Now check the log file with DEBUG level in order to see that reall your method is beyond a transaction.

Now on the applicationContext-hibernate.xml file I have definedt the hibernate beans:

Code:
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <description>
         Definition of the Hibernate Session Factory, you can add more
         properties in order to get fine tuning of the Hibernate Session.
         Here we provide the list of hibernate mapping files.
      </description>
      <property name="dataSource"><ref bean="dataSource"/></property>
      <!--Directory location of the *.hbm.xml files-->
      <property name="mappingDirectoryLocations">
         <value>${hibernate.mappingDirectoryLocations}</value>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
            <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
         </props>
      </property>
   </bean>

   <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
   <bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <description>
         Transaction manager for a single Hibernate SessionFactory
         (alternative to JTA)
      </description>
      <property name="dataSource">
         <ref bean="dataSource"/>
      </property>
      <property name="sessionFactory">
         <ref local="sessionFactory"/>
      </property>
   </bean>
   
   <!--Parent bean for all hibernate implementation dao's-->
   <bean id="hibernateBase" depends-on="propertiesManager" abstract="true">
      <description>
         Parent bean for all hibernate implementation dao's
      </description>
      <property name="sessionFactory">
         <ref local="sessionFactory"/>
      </property>
   </bean>

   <!--Hibernate implementation of lraDao bean-->
   <bean id="companyDao" class="com.schinvest.lra.data.hibernate.HibernateCompanyDao"
       parent="hibernateBase">
      <description>
         Hibernate implementation of DAO interface for Position Batch Process.
      </description>   
      <property name="properties"><ref bean="propertiesManager"/></property>   
   </bean>


let me know if this would help you,

Bye,

David

_________________
David Leal


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 1:49 pm 
Newbie

Joined: Tue Mar 28, 2006 11:37 am
Posts: 3
Location: Moscow
I'd fixed this problem by turning on auto commit in hibernate. Dont remeber what option it was. It can be found in manual. I know that it is not good but enaugh for now. I'll create spring's transaction manager like in above when i'll need it.

_________________
Nothing is as easy as it looks.


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