开发者

Web app with multiple projects using Java EE and Hibernate

I have a general design question that's bothering me for some time now.

Goal:

  • Create a web application using Vaadin for the Front End
  • User must be able to create projects during runtime. Every project uses the same (Domain) Model, but different data
  • Data for every project must be separated (into different databases)
  • User can login, select project and get access to the specific Database
  • Different user can work on different projects at the same time
  • Application uses Hibernate with Annotations and Programmatic Configuration

Current Situation

  • Static HibernateUtil use in many places (getSessionFactory.getCurrentSession)
  • Partially Implementation of Session-Per-View Pattern with ThreadLocal to get current Application instance, that works as Session-(and Transaction) Manager by using HTTPServletRequestListener to open and close session and transaction before and after view request
  • Logic layer with no direct access to View Layer:
    • In some parts, Current Session is get by using the static HibernateUtil
    • in other places, by setting the Session Manager via the constructor. This session manager then is passed to a DAO to provide session for Data Retrieval an manipulation
    • When project is created, Hibernates create option is used to let the database be created (in future this could be switched to static ddl import at creation)

Problems

As one can imagine, there are various problems:

  • Static access to sessionfactory to get session cannot work for multiple projects, as static HibernateUtil can only carry one (db)url at the same time and so sessions get mixed
  • Access to SessionManager from Logic Layer is not nice (need to provide it through several constructors)
  • mixture of different approaches is ugly, but goal is to straighten this

Thoughts

  • Implement Dependency injection to provide correct DAOFactory even in Logic Layer. Tried it with Guice, but get Null Pointer Exceptions if Instances are not created via Injector (see my other question). Therefore it does not work.

I wish i could just inject/use the correct Instance of DAO Factory (which has access to the correct session/db) anywhere in the code without e.g. the logic layer knowing which current database is loaded. So the logic layer should not care about session handling or anything. I just want to make a call do xyDAO.find(id) or similar from anywhere and it gets the correct database. I also think that its not nice to pass the SessionManager through several classes by constructor, if only one class deep down 开发者_如何学JAVAin the logic needs it.

What approach would you take to meet the goals stated?

I would really appreciate if someone could help. If you need additional info, please let me know and I will provide!


Have you have confused requirements and solutions?

Data for every project must be separated (into different databases)

Is there really requirement that you have different databases, or is it just that Data for every project must be separated?

If different databases are not an absolute requirement, just add a user_id column to enough tables so you can use the same tables for all users but still achieve this requirement.

You could call this approach partitioning the data vertically, rather than horizontally.

The benefit should be immediately obvious: Suddenly your solution is a lot simpler, which is a good thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜