Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Transaction Management with Hibernate and MySQL, Global and Local

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! :-)

like image 375
nano7 Avatar asked Feb 24 '23 11:02

nano7


1 Answers

  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.

like image 59
Henning Avatar answered Apr 27 '23 09:04

Henning