-->
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: couldn't turn off auto-commit
PostPosted: Thu Mar 18, 2010 4:16 pm 
Newbie

Joined: Thu Feb 04, 2010 12:50 pm
Posts: 6
I'm using hibernate annotations, my database in is MySQL.

I tried to persist a bunch of objects and commit at the end, which means insert all or insert none of them.
For example, I want to insert 10 objects one time, if exception happens at the 9th object, because I didn't do commit, there should be no new data in database. However, I do find the 8 datas. Anyone has experience/solution for this?

BTW, I didn't use Spring.

Code:
       try{               
           for (Student student : students)
              {      
                 studentDAO.save(student);         
              }//end loop      
         studentDAO.commitTransaction(); //insert to do as one atomic transaction, if failes - DO NOT DO ANY INSERT      
      }catch(Exception e)
      {
         studentDAO.rollback();      
      }finally
      {
         studentDAO.close();
      }


Here's some of my hibernation configuration:

Code:
   config.setProperty("hibernate.connection.pool_size", "10");
            config.setProperty("hibernate.connection.autocommit", "false");
            config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");                 
            config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
            config.setProperty("hibernate.current_session_context_class", "thread");


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Thu Mar 18, 2010 6:19 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Which table type are you using? Not all table types in MySQL (eg. MyISAM) support transactions.


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Fri Mar 19, 2010 2:35 pm 
Newbie

Joined: Thu Feb 04, 2010 12:50 pm
Posts: 6
nordborg wrote:
Which table type are you using? Not all table types in MySQL (eg. MyISAM) support transactions.


Thanks for reply. But how to check table type?

BTW, We have another project which has exactly the same setting as this one. However, autocommit is off on that project, even if I turn it on.

In other words, in project A, autocommit is on regardless of my setting config.setProperty("hibernate.connection.autocommit", "false");

in project B, autocommit is off regardless of my setting config.setProperty("hibernate.connection.autocommit", "true");

It seems config.setProperty("hibernate.connection.autocommit", "false/true"); doesn't do its job at all.


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Fri Mar 19, 2010 3:04 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If you have a graphical tool like MySQL Administrator it is displayed when you list all the tables of a database. Otherwise you can just execute SQL as described here: http://stackoverflow.com/questions/2135 ... ific-table


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Fri Mar 19, 2010 3:07 pm 
Newbie

Joined: Thu Feb 04, 2010 12:50 pm
Posts: 6
nordborg wrote:
If you have a graphical tool like MySQL Administrator it is displayed when you list all the tables of a database. Otherwise you can just execute SQL as described here: http://stackoverflow.com/questions/2135 ... ific-table


The type is "MyISAM". Is this the reason? How can I change this type? My table was created by hibernate pojo automatically.


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Fri Mar 19, 2010 3:20 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes, MyISAM doesn't support transactions and it is not possible to rollback what has already been sent to the database. You need to configure Hibernate to use the MySQL InnoDB dialect instead: org.hibernate.dialect.MySQLInnoDBDialect

I don't know if it is possible to change an existing database. You may have to drop and re-create it. But check MySQL documentation for more information about converting.


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Fri Mar 19, 2010 10:52 pm 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
you can alter the existing table using
Code:
ALTER TABLE table_name ENGINE = InnoDB;


Top
 Profile  
 
 Post subject: Re: couldn't turn off auto-commit
PostPosted: Tue Apr 13, 2010 3:11 pm 
Newbie

Joined: Thu Feb 04, 2010 12:50 pm
Posts: 6
SIau_Tie wrote:
you can alter the existing table using
Code:
ALTER TABLE table_name ENGINE = InnoDB;


works, thanks!


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.