File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate in a multi-server architecture Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate in a multi-server architecture" Watch "Hibernate in a multi-server architecture" New topic
Author

Hibernate in a multi-server architecture

Max Qua
Greenhorn

Joined: Sep 27, 2008
Posts: 20
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Sep 19, 2007
Posts: 36
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.


Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Sep 27, 2008
Posts: 20
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

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Sep 19, 2007
Posts: 36
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate in a multi-server architecture