-->
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.  [ 6 posts ] 
Author Message
 Post subject: Data is not inserting without transaction commit option!!!
PostPosted: Mon Jan 18, 2010 11:35 am 
Newbie

Joined: Fri Feb 15, 2008 12:27 am
Posts: 3
Location: Jaipur
Hello All,

I created a simple hibernate project where I am inserting a row into database table. But it insert the data only when I use org.hibernate.Transaction commit. If I dont use transaction commit It neither insert any thing in to database nor show any error on console.

My hibernate config file is

Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="connection.url">
      jdbc:mysql://localhost:3306/chat?autoReconnect=true
   </property>
   <property name="connection.username">root</property>
   <property name="connection.password">root</property>
   <property name="connection.driver_class">
      com.mysql.jdbc.Driver
   </property>
    <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="format_sql">true</property>
   <property name="hibernate.connection.pool_size">10</property>
   
      <property name="show_sql">true</property>
     
   <!-- <property name="myeclipse.connection.profile"></property> -->
   <mapping resource="code/contact.hbm.xml" />
</session-factory>

</hibernate-configuration>


My code is
Code:
package code;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
  public static void main(String[] args) {
    Session session = null;
    org.hibernate.Transaction tx = null;
   
    try{
      // This step will read hibernate.cfg.xml


      SessionFactory sessionFactory = new

Configuration().configure().buildSessionFactory();
       session =sessionFactory.openSession();
        //Create new instance of Contact and set
       tx =  session.beginTransaction();

         System.out.println("Inserting Record");
        Contact contact = new Contact();
        //contact.setId(67);
        contact.setFirstname("Deepak");
        contact.setLastname("Kumar");
        contact.setEmail("deepak_38@yahoo.com");
       
        session.save(contact);
        System.out.println(contact.getId());
        System.out.println("Done");
       
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      // Actual contact insertion will happen at this step
       tx.commit();
       session.flush();
        session.close();

      }
   
  }
}


Please tell me what may be the reason.

Regards,
Prateek


Top
 Profile  
 
 Post subject: Re: Data is not inserting without transaction commit option!!!
PostPosted: Mon Jan 18, 2010 12:07 pm 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
Hello Prateek,

that's exptected afaik. If you do not commit your transaction it won't be written to the database. This is how databases handle reading and writing from it. Your code is fine but you should probably call Transaction.rollback() if the transaction did not succeed, like so:

Code:
           
            t.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (t != null) {
                t.rollback();
            }
        }


Top
 Profile  
 
 Post subject: Re: Data is not inserting without transaction commit option!!!
PostPosted: Wed Jan 20, 2010 6:16 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
This is due to the so called write-behind mechanism of hibernate, which performs DML-statements as latest as possible.
If you want force the DML-statements to be executed before the commit, then you can call flush explicitly.


Top
 Profile  
 
 Post subject: Re: Data is not inserting without transaction commit option!!!
PostPosted: Fri Mar 12, 2010 6:42 am 
Newbie

Joined: Fri Mar 12, 2010 6:22 am
Posts: 1
Hi,
I also have taken the same sample program. As per the specification, by flushing a Session at the end of a unit of work, to execute the SQL DML operations (UPDATE, INSERT, DELETE) that synchronize the in-memory Session state with the database.
In that case, by using session.flush() should insert a record in the database. I am not sure why we need to open the Transaction API for the commit
Can someone help me out to understand this


Top
 Profile  
 
 Post subject: Re: Data is not inserting without transaction commit option!!!
PostPosted: Fri Mar 12, 2010 7:18 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I am not sure why we need to open the Transaction API for the commit
Can someone help me out to understand this


The flush operation is not the same as commit as it does not terminate the transaction!
Each transaction must be terminated either by commit or a rollback call.
If you never call commit or rollback then sooner or later (it depends on your db-config)
your transaction with go in timeout and your database will rollback the transaction implicitly.
Only with the commit call the updated/changed data gets definitely consolidated on the db.
Furthermore with ReadCommited isolation level (or higher) the commit call is also the moment where
the changed data gets visible to other transactions.


Top
 Profile  
 
 Post subject: Re: Data is not inserting without transaction commit option!!!
PostPosted: Fri Mar 12, 2010 9:12 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
that's just how java Transactions are specified. You can change the Transaction to auto-commit, by default it is disabled.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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