3

I have an existing client-server project which uses plain JDBC to communicate with a single MySQL instance. Now I would like to extend it to support multiple (sharded) MySQL instances using any JPA implementation and with the help of JTA implementation that supports XA transactions.

My question is how should I proceed? After several hours of Googling what I understand I may not able to use JTA implementations without any application server container, is it true?

For my purpose I would be happy if the application logics could be abstracted using any ORM layer - a JPA implementation (like EclipseLink Partition or OpenJPA Slice) and then using a transaction manager (like BTM) to execute the XA transactions providing ACID guarantees over the underlying set of MySQL instances.

So far I tried the video tutorial in YouTube (https://www.youtube.com/watch?v=Vmr6GAlbG10) experimenting with EclipseLink and a single db instance. Now I'm planning to extend it over multiple instances but not sure how.

Any kind suggestions, links and guidelines would be very helpful. I'm kind of new to this so accept any inconvenience if I misunderstood anything. Thanks.

Joarder Kamal
  • 1,387
  • 1
  • 20
  • 28

2 Answers2

1

You can use standalone JTA transaction manager like Atomikos http://www.atomikos.com/ or SimpleJTA http://simplejta.sourceforge.net/ completely outside of any container.

More about standalone JTA managers: What is a good open source Java SE JTA TransactionManager implementation?

Also it's possible to use popular JPA implementations as standalone with combination with standalone JTA (what you asked). Here is sample for Atomikos and Hibernate http://www.atomikos.com/Documentation/HibernateThreeStandalone

But this is not great way to use such technologies and you have any chances and time - move to normal three-tier architecture with application server.

Community
  • 1
  • 1
Alex Chernyshev
  • 1,719
  • 9
  • 11
  • @jmkam: I don't know about the OP, but I think this answer is worth the bounty, especially the example with Hibernate + JTA in a standalone environment, which could as well be used within Java EE. Thanks as well for Andrei's answer, but since it specializes on Spring and the OP doesn't have it in the tags or question, I'd go for this one. – Alexander Rühl Jul 28 '14 at 11:02
0

It is also possible to use JTA in the context of a Spring application.

Besides, you COULD use it in the context of your own application without any frameworks, but it is pretty complicated, as you will have to control what/when/where is injected and how the called method returned (with Exceptions or without). But of course, it really depends on what exactly you want to use from JTA: transaction propagation or simply using different databases in the same JTA transaction.

So, my suggestion is to use Spring, if you do not want to use an application container (Java EE).

V G
  • 18,822
  • 6
  • 51
  • 89
  • Could you provide some more details concerning your proposal, e.g. a tutorial or example? – Alexander Rühl Jul 25 '14 at 13:39
  • My proposal with Spring? Simply search on internet for "spring jta" and there you will find many examples. For example check [Spring's article on JTA](http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/). – V G Jul 25 '14 at 13:59
  • @Geziefer and because you already have experience with Java EE: you simply configure the transaction manager, and tell your EntityManagers that they are JTA (not resource-local), and every where they are injected by Spring, they will join automatically the JTA transaction. The annotations are very similar like those in Java EE, but only the ones for the transaction propagations are different. (and probably you must annotate the public methods with `@Transactional`, compared to EJB, where that is default) – V G Jul 25 '14 at 14:18
  • @AndreiI I was looking at the Spring article just a moment ago before seeking your post !! But could you able to give any more specific and recent example with Spring+JTA+EclipseLink? – Joarder Kamal Jul 25 '14 at 23:46
  • @jmkam all those are different components, that you could glue together in a Spring application. So simply search for "JTA+Spring" and "EclipseLink+Spring" and you will find enough examples on how to configure Spring. – V G Jul 28 '14 at 08:24