aspose file tools*
The moose likes Object Relational Mapping and the fly likes Delete Query problem: Failure to Commit HQL Delete Transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Delete Query problem: Failure to Commit HQL Delete Transaction" Watch "Delete Query problem: Failure to Commit HQL Delete Transaction" New topic
Author

Delete Query problem: Failure to Commit HQL Delete Transaction

Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my class for delete


after run this class it shows out as


it says one row is deleted
but when i run this query it shows both rows


Out put is here


where is the error?
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
You didn't commit so your transaction did a rollback when you closed the session.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
where is the Transaction. ?
there is no any transaction.
can't we create a session without transaction?
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
session.getTransaction() gives you the transaction. No, you cannot do any database operation outside a transaction (apart from some nontransactional "databases"). Even in autocommit-mode there is a transaction inside the database.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
oho.
then you mean this is wrong
http://www.roseindia.net/hibernate/firstexample.shtml

check is it wrong or not?
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
It's wrong. If data isn't commited it must be wrong. I don't know MySQL that much but wasn't there a storage engine that didn't support transactions and another one that did? If you used the one that supported transactions the sample from roseindia cannot work. (Despite using autocommit-mode I think.)
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Agreed you need to have a transaction to update the database, but it does not have to explicitly coded. You can configure to have Application Server manage the transaction on its own in some cases. (CMT i think.)

Dont know if it helps but how about trying a session.flush() before you close the session?
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
but this view query is working.


there is no any transaction in this code.
Tomasz Szymanski
Greenhorn

Joined: Jan 11, 2010
Posts: 23
It depends on what you mean by "it's working"...
Not using a transaction will not get you an exception - in that sense it's working. The problem is that whatever you do, it's cancelled as soon as you close the session (unless you have a non-transactional DB, as Christian mentioned, or you're working in the autocommit mode). Although, to be precise, according to this article, it's not 100% sure.
It seems that's exactly what's happening in your DeleteQuery,
Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
Your view query is working because it is just selecting something from a table....

and your delete is not working because you have changed the table and you didnt say commit.

One more question how are you building your session factory is it being managed by an App Server, if yes then call session.flush() as mentioned above...

If no check whether you db is transaction or non transactional, i feel its transactional so get the Transaction from session and do a commit.

Hope this helps.


Thanks,
Prabhat
SCJP 1.5, SCWCD 1.5, SCBCD 1.5
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
I am using NEtbeans 6.8 IDE
Server is GlassFish which comes with Built in with Netbeans
DB is Java DB wich comes with Netbeans
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
The code you showed above is not inside Glassfish! Thats plain old java without any application server, transaction manager or DI voodoo. No commit no database change. If you call flush you make it even worse because Hibernate prints the query and you think everything is okay, but then you don't see the rollback.
 
Don't get me started about those stupid light bulbs.
 
subject: Delete Query problem: Failure to Commit HQL Delete Transaction