Im working on a webapplication with Spring Framework 3 and Hibernate 3.6 and I try to understand how transaction management works. I use mySQL Server 5.1 with InnoDB tables. I wrote my DAO based on plain Hibernate 3 API.
1)开发者_JS百科 Is it correct, that mySQL Server itself specifies how transactions are handled? That means it decides when records need to be locked or not?
2) When I declare a method in spring as @transactional(readOnly=true) or @transactional(readOnly=false) does this affect if the datarecords are locked during the transaction? This means, when readOnly=true, no datarecord is locked and when readOnly=false all used datarecords are locked?
3) What happens when I got readOnly=true and I read various data records. Lets assume in the middle of reading they are changed by another transaction, so that I get some old records and some new records. Is that possible?
4) When does a commit happen? After a successful transaction or when a session is closed?
5) when does a hibernate-session start? for each session (between server and client) or for each transaction?
6) in the end, who has the responsibility for transaction management? spring or mysql or both?
Thanks for answering! :-)
1) Is it correct, that mySQL Server itself specifies how transactions are handled? That means it decides when records need to be locked or not?
In essense - yes, implementation of transactions is a responsibility of DBMS
2), 3) ...
readOnly
has nothing to do with transactions, it's a hint for Hibernate which says that Hibernate shouldn't propagate changes made to the loaded object to the database (i.e. you can specify it as a performance hint if your transaction isn't intended to write changes to the database)
4) When does a commit happen? After a successful transaction or when a session is closed?
Commit is an action that terminates a transaction (another such action is rollback), therefore it happens at the end of transaction.
5) when does a hibernate-session start? for each session (between server and client) or for each transaction?
By default - for each transaction, unless you configured Open Session in View support.
6) in the end, who has the responsibility for transaction management? spring or mysql or both?
Database provides implementation of transactional behaviour. Spring manages transactions by defining their boundaries and triggering commit/rollback.
精彩评论