I have a scenario where if a transaction starts for the database then no reading and updation operations should be allowed until the started transaction got committed or rolledback.
For Example:
if I try to insert a row then there are some specific changes to be done to already existing rows in the database,if any other transaction or database query reads the data before the changes are complete,then we’ll end up having bad values in the database,so how to control the queries.
What I exactly want to do is when ever a transaction starts I want to put a lock to database,How to do this?
I’m using Hibernate transaction(declarative transaction) @Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW) can I add any other attributes to the transaction that suffices my requirement.
You should not have to do that. Transactions run in isolation from each other. In the default isolation level (READ_COMMITTED, most of the time), each transaction only sees what other transactions have already committed. So if you have one parallel transaction reading from the database while your transaction is running, this parallel transaction won’t see the changes of your main transaction until it has committed. Each transaction will see a coherent snapshot of the database.
This is sufficient in most of the cases. If it’s not sufficient, you might want to use a higher isolation level (like REPEATABLE_READ or even SERIALIZABLE, if your database supports them), but completely locking the database is impossible, and not desirable.
Read http://en.wikipedia.org/wiki/Isolation_%28database_systems%29 for more details about what each isolation level guarantees, and read your database documentation to see which levels it supports.
Side note: @Transactional is a Spring annotation, not a Hibernate one.