-->
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: hibernate autocommit
PostPosted: Wed Mar 27, 2013 6:12 am 
Newbie

Joined: Wed Mar 27, 2013 6:01 am
Posts: 2
hi,
I’m confused about the autoCommit property in hibernate. it doesn't work
the test code is :
Session session1 = sessionFactory.openSession();
Student st = new Student();
st.setName("mamyami");
session1.save(st);
session1.flush();
session1.close();

Database:Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
the autocommit of data base is off

hibernate version:3.6.5 Final

when I set hibernate autoCommit false and run the test code,the record is still commit to the database.

when I set hibernate autoCommit true and database autoCommit on,delete the flush code. the record is not commit to the database;

it seems whether the record commit to the database only related with session.flush or other function that flush the cache .
the hibernate autocommit and database autocommit doesn't work

why?


Top
 Profile  
 
 Post subject: Re: hibernate autocommit
PostPosted: Wed Mar 27, 2013 9:45 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I believe you have a false understanding to what autoCommit property in hibernate should be.

Following article should help to understand the autoCommit in hibernate:

https://community.jboss.org/wiki/Non-transactionalDataAccessAndTheAuto-commitMode

Quote:
when I set hibernate autoCommit false and run the test code,the record is still commit to the database.


Which transaction demarcation approach do you use? Programmatic or declarative?
In first case the transaction should have been rollbacked, not commited.


Top
 Profile  
 
 Post subject: Re: hibernate autocommit
PostPosted: Wed Mar 27, 2013 10:13 pm 
Newbie

Joined: Wed Mar 27, 2013 6:01 am
Posts: 2
thanks very much!
I have read the link article,but still confused because things in my test is unlike the ariticle mentions:
***********
Session session = getSessionFactory().openSession();
Long generatedId = session.save(item);
session.close();
This code results in an INSERT statement, executed inside a transaction that is never committed or rolled back. On Oracle, this piece of code inserts data permanently
************

but in my test , whether the autocommit is on or off, the record is never insert into the database when I'm not flush the session cache.

About
pb00067 wrote:
Which transaction demarcation approach do you use? Programmatic or declarative?
In first case the transaction should have been rollbacked, not commited.


I just open a session like the code I mention before, doesn't using beginTransation or Spring ,I know this is not a good way using hibernate ,I just want to understand autocommit;

Session session1 = sessionFactory.openSession();
Student st = new Student();
st.setName("mamyami");
session1.save(st);
session1.flush();
session1.close();

I used to think that when I flush the session in the code , the hibernate just take the sql to database,this behaviour likes you connect to your database with an SQL console and that you insert a row without typing begin a transaction and end a transaction with the sql.the record is actually inserted or not depends on autocommit; if you turn it on ,it inserted ;else it not. but if you not flush,hibernate will not take the sql to database,so the record will not inserted no matter autocommit is on or off;

but in my test is if you flush the cache ,no matter autocommit is on or off ,the record in inserted;
if you don't flush,no matter autocommit is on or off ,the record will not inserted;

is my test wrong?


Top
 Profile  
 
 Post subject: Re: hibernate autocommit
PostPosted: Fri Mar 29, 2013 11:16 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you to log all jdbc-activity with a JDBCLogger like p6spy,
so you will see exactly what hibernate does (for example if it does call an commit or a rollback).

That hibernate does not insert the data until you call flush or commit that's normal,
This is called the transactionwrite behind mechanism:
see
http://learningviacode.blogspot.it/2012/02/write-behind-technique-in-hibernate.html


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.