• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate in a multi-server architecture

 
Max Qua
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to use Hibernate in an application with several servers that need access to the same DB. Do I have to instanciate hibernate on each server or is it possible to use only one hibernate instance on one server only?
Is there any example on how to do it?

My application needs to use several servers because the number of users can grow and so for balancing the load I need to scale.

Thanks

/Max
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the key to any multi-server architecture is going to be the co-ordination of a transaction. Every J2EE/JEE application server has an implementation of the Java Transaction API, which co-ordinates two phase commits across potentially multiple databases. So, the JTA and JTS (Transaction Service) will really be the key to co-ordinating your transactions.

Of course, data does get locked, so if another server tries to grab locked data, there may be a data deadlock or problem, but that's just the nature of the beast. I think the key to your situation is tapping into the native JTS implementation on your server.

-Cameron McKenzie
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily use hibernate with the same database on multiple servers. In most cases you dont need real distributed transactions (thats what you described cameron?), normal database transactions are enough. Therefore the data doesnt get locked. It depends on the transaction isolation level how concurrent transactions are handled by your database.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats what you described cameron?


Yup, and your point is well taken. There's no need getting a giant hammer out to hit a small nail. The simple solution is usually the best one.

-Cameron McKenzie
 
Max Qua
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,
but I am not familiar with JTA and in general with multi-server environment.
I have tomcat 6 + hibernate and a JSF application which uses the MySQL DB. Another servlet application should have access to the same database. My idea was to deploy JSF web site on a single server and more instances of servlet application on several servers because several users could use them for longer time.
Now I have not clear if the java code I have written for Hibernate could be instantiated only once in a separate server or it must be deployed together with the JSF application and the servlet application in all servers, and so replicated.

Thanks

/Max
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now I have not clear if the java code I have written for Hibernate could be instantiated only once in a separate server or it must be deployed together with the JSF application and the servlet application in all servers, and so replicated.

Having a seperate "DAO" server is not necessary.
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could encapsulate your persistence layer into a seperate web project with an additional service facade delegating to the dao methods. These services can be made accessible to other web projects (jsf) not matter if they are deployed on the same or another server.

There are many ways to provide those services to other projects. I prefer using the Spring Framework. In this case you can use Springs HTTP Invoker protocol to make the services accessible via HTTP. But you can easily switch to Webservices, RMI, Hession or whatever you wish.

Another simple approach is to put your persistence code into a separate java project. Your web projects can than be dependant to this project. They will instantiate their own database connections, daos etc on runtime.

I would prefer the first approach. You can use one database connection pool instead of many, you can use a second level cache to improve performance. But on the other side its a single point of failure.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my own curiosity Ben because I don't know Spring very well (and sorry Max for the slight hijack) if you use an HTTP provided DAO layer does Spring propagate transactions across it?
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:For my own curiosity Ben because I don't know Spring very well (and sorry Max for the slight hijack) if you use an HTTP provided DAO layer does Spring propagate transactions across it?


Good point Paul. My fault, this will not work unless it runs on a JEE container with JTA support. You would need to not only put the dao layer to this web project but the whole service layer. So you would end up with an EJB like service facade which encapsulates the whole persistence layer in one project. Spring makes this project run without an EJB container on servlet container.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic