使用 Spring、Hibernate 和 mySQL 进行事务管理
我正在使用 Spring Framework 3 和 Hibernate 3.6 开发一个 Web 应用程序,我尝试了解事务管理的工作原理。 我将 mySQL Server 5.1 与 InnoDB 表一起使用。我基于简单的 Hibernate 3 API 编写了 DAO。
1) mySQL Server 本身指定了如何处理事务,这是否正确?这意味着它决定何时需要锁定记录?
2) 当我在 spring 中将一个方法声明为 @transactional(readOnly=true) 或 @transactional(readOnly=false) 时,这是否会影响数据记录在事务期间被锁定?这意味着,当 readOnly=true 时,没有数据记录被锁定,而当 readOnly=false 时,所有使用的数据记录都被锁定?
3) 当我获得 readOnly=true 并读取各种数据记录时会发生什么。让我们假设在读取过程中它们被另一个事务更改,这样我就得到了一些旧记录和一些新记录。这可能吗?
4) 提交何时发生?交易成功后或会话关闭时?
5) 休眠会话何时开始?对于每个会话(服务器和客户端之间)还是对于每个事务?
6)到底谁来负责事务管理? spring 或 mysql 或两者都有?
感谢您的回答! :-)
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) 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! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从本质上讲 - 是的,事务的实现是 DBMS 的责任
readOnly
与事务无关,它是 Hibernate 的一个提示,表示 Hibernate 不应将对加载对象所做的更改传播到数据库(即您可以指定如果您的事务不打算将更改写入数据库,则将其作为性能提示)提交是终止事务的操作(另一个此类操作是回滚),因此它发生在事务结束时。
默认情况下 - 对于每个事务,除非您在视图支持中配置了“打开会话”。
数据库提供事务行为的实现。 Spring 通过定义事务边界并触发提交/回滚来管理事务。
In essense - yes, implementation of transactions is a responsibility of DBMS
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)Commit is an action that terminates a transaction (another such action is rollback), therefore it happens at the end of transaction.
By default - for each transaction, unless you configured Open Session in View support.
Database provides implementation of transactional behaviour. Spring manages transactions by defining their boundaries and triggering commit/rollback.