Spring Transaction Management with Hibernate and MySQL, Global and Local

孤街浪徒 提交于 2021-02-20 19:18:08

问题


Im working on developing a webapplication with MySQL Server 5.1, Spring 3.0.5 and Hibernate 3.6. I use Springs Transaction Management. Im a newbie, so please be patient with me if I ask a question which is easy to answer. :-)

1) I read about global (xa) and local transactions. Is it correct, that global transactions mean transactions, which execute dataoperations on different resources (like different databases). And that local transactions execute dataoperations on only one resource (database).?

2) Is it possible to have global transactions with Hibernate? In the Spring Reference Documentation I read this: "You can also use Hibernate local transactions easily, as shown in the following examples. In this case, you need to define a Hibernate LocalSessionFactoryBean, which your application code will use to obtain Hibernate Session instances." Thats why I think that Hibernate transactions are maybe always local and I did not find anything else about it.

3) Why is it possible to use MyISAM tables with Hibernate? They dont support Transactions and I think Hibernate requires Transactions? I really don't understand this. At first I thought it wouldn't be possible - but why is it then possible to create MyISAM Tables with Hibernate and use a MyISAM Dialect? How does this work??? Does Hibernate require transaction or doesnt it? I thought for using Hibernate you need to use InnoDB.

Thanks for answering! :-)


回答1:


  1. Yes, JTA, the API for XA transactions in Java, uses a two-phase commit protocol to perform global transactions. If you need to ensure transaction consistency over multiple resources, JTA is the proper tool in Java.

  2. Hibernate does work with JTA. It is quite a bit harder to set up than plain vanilla resource local transactions, generally uses more resources due to the nature of two-phase commit, and the implementation quality varies between database vendors and drivers, so only use it if you have to. (For example, JTA kinda sorta works with MS SQL Server, but this functionality is only glued on through a set of stored procedures. Other resources may not support all of the standard, for example not allow a transaction to be paused and later resumed. If you don't need it, save yourself the trouble.)

  3. You can use MyISAM with Hibernate, but transactions won't work. Hibernate will start and commit transactions normally, which the MyISAM storage engine will ignore silently, so everything will go straight to disk. Use the default storage engine, InnoDB, unless you have a reason not to.



来源:https://stackoverflow.com/questions/5717935/spring-transaction-management-with-hibernate-and-mysql-global-and-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!