hibernate 事务安全性(有、没有 JBoss)
我目前只使用 Hibernate 和 tomcat (没有 JBoss),并且有 hibernate 事务,我不清楚我实际使用的事务安全级别,特别是对于那些读取和获取值然后更新它们的事务。因此我可能会得到脏读?
因此,我将开始研究需要非脏读的事务,并确保 (1) hibernate 正确控制这些事务的事务安全级别,并且 (2) 仍然能够让这些事务处于脏读状态读取没问题。
我是否需要安装 Hibernate 和 JBoss 来控制事务安全级别?如果是这样,那么在不显着更改我的应用程序以使用 J2EE api 的情况下执行此操作的最简单方法是什么,因为我当前正在使用基本的 Hibernate api。或者更好的是,我可以在不使用 JBoss 的情况下通过 Hibernate 获得 JTA 控制吗?
安迪
I am currently using just Hibernate and tomcat (no JBoss), and have hibernate transactions which I'm not clear on what transaction safety level I'm actually using, especially for those which read and get values and then update them). Thus I might be getting dirty reads?
So I'm going to start having to study my transactions that require non-dirty reads, and make sure that (1) hibernate controls the transaction safety level of those transactions properly, and (2) be able to still have those transactions where dirty reads are ok.
Do I need to install Hibernate with JBoss to control transaction safety levels? If so, what's the easiest way to do this without dramatically changing my application to use the J2EE apis, as I am currently using the basic Hibernate apis. Or better, can I get JTA control with Hibernate without using JBoss?
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否使用 em.getTransaction().begin() 和 commit() 明确标记您的事务?如果是这样,Hibernate 会将事务管理委托给 JDBC 驱动程序,并且您可以使用其他属性进行配置(在驱动程序级别和 Hibernate 级别,例如隔离级别)。
如果您决定使用应用程序服务器,您只需将属性 hibernate.transaction.factory_class 设置为 org.hibernate.transaction.JTATransactionFactory,Hibernate 将根据您的指令加入/创建事务 (em.getTransaction().begin( ) / 犯罪())。
如果您有两个或更多“启用事务”组件,JTA 将特别有用,因为 JTA 会根据总体结果来协调事件并命令提交/回滚。例如,如果您有一个 EJB 事务,它将某些内容保存到数据库并发送一封电子邮件,则这两个操作都可以在事务内,并且如果其中任何一个操作失败,则可以回滚。但是,如果您仅对数据库事务感兴趣,那么您实际上并不需要 JTA。
回答您的最后一个问题:如果您确实需要 JTA 但不想使用 EE 应用程序服务器,您可以寻找独立的 JTA 提供程序,例如 JOTM。但要小心:如果您发现自己为不同的服务添加了多个“独立”提供商,您最终可能会得到一个“自制 EE 服务器”,这不是一件好事;-)
Are you explicitly marking your transactions with em.getTransaction().begin() and commit()? If so, Hibernate will delegate the transaction management to the JDBC driver, and that you can configure with other properties (both at driver level and Hibernate level, like isolation levels).
If you then decide to use an Application Server, you can just set the property hibernate.transaction.factory_class to org.hibernate.transaction.JTATransactionFactory and Hibernate will join/create a transaction based on your instructions (em.getTransaction().begin() / commit()).
JTA is specially useful if you have two or more "transaction enabled" components, as JTA orchestrates the events and orders commits/rollbacks depending on the overall outcome. For instance, if you have one EJB transaction which persists something to the database and sends an email, both actions can be inside a transaction and it can be rolled back if any of them fails. But if you are interested only in database transactions, you don't really need JTA.
And to answer your last question: if you do need JTA but you don't want to use a EE Application Server, you can look for standalone JTA providers, like JOTM. But be careful: if you find yourself adding several "standalone" providers for different services, you may end up with a "home built EE server", which is not a good thing ;-)
这称为事务隔离级别,下面是实现的方法在休眠状态下配置它们
This are known as transaction isolation levels and below is the way to configure them in hibernate