Paul Sturrock wrote:Why do you need to lock the table to do what you are doing? And why do they need to be "in one go"?
Paul,
I want a lock on table because i want to first perform update and then insert.
And it should be in one go because if some error occurs then every change should be roll back and only one user can modify at a time because if multiple user modify same data then error occured.
It's going to turn your database into a real bottleneck (effectively a single user repository) if you use pessimistic locking where you would normally use a transaction. Any reason you can't use a transaction?
I followed transaction way for implementation but i have one issue.
Below is my code - snippet :
In the above code snippet, i rolled back the transaction in case exception occured.But rolled back is not working because changes done by update statement is present in database table.
Can any one suggest me what is the error in the code and why roll-back is not working??
Thanks for all suggestion.
This message was edited 1 time. Last update was at by Paul Sturrock
But rolled back is not working because changes done by update statement is present in database table.
In which case you are not running in a transaction, or your rollback statement is not being run.
Hey Paul,
If error occurs during some transaction for e.g. if any statement fails then i want to roll back the transaction.
In the code snippet, if you see there is an IF-ELSE block. Intentionally , i am forcing my transaction to throw error and if error comes then it goes to catch block.
In catch block, i am rolling back the transaction but when i check database table, i observed that changes made by UPDATE statement is there.
I want the table to show data which is before UPDATE statement.
Your transaction will only rollback if it's not null, and the exception thrown is a HibernateException. If either of these things are not true the transaction will not rollback.
But rolled back is not working because changes done by update statement is present in database table.
In which case you are not running in a transaction, or your rollback statement is not being run.
Hey Paul,
If error occurs during some transaction for e.g. if any statement fails then i want to roll back the transaction.
In the code snippet, if you see there is an IF-ELSE block. Intentionally , i am forcing my transaction to throw error and if error comes then it goes to catch block.
In catch block, i am rolling back the transaction but when i check database table, i observed that changes made by UPDATE statement is there.
I want the table to show data which is before UPDATE statement.
Please, print the resulting stack trace... May be the exception thrown is not yours but another Exception, in that case, since your catch (Exception) is not rolling back the transaction, changes in DB aren't reverted.
Rollback is not working due to DB engine.
In MySQL, MYISAM engine does not support transaction and thats why rollback is not working.
After changing it to InnoDB, it is working fine.
Abhishek Purwar wrote:Rollback is not working due to DB engine.
In MySQL, MYISAM engine does not support transaction and thats why rollback is not working.
After changing it to InnoDB, it is working fine.
Yeah - the MyISAM engine is not really worth using unless all you need something like a single table repository. Its non-transactional and it doesn't honour constraints (i.e. its not really a relational database).