This discussion is archived
6 Replies Latest reply: Nov 5, 2010 12:14 PM by jschellSomeoneStoleMyAlias RSS

Efficiency VS simplicity in design patterns

269490 Newbie
Currently Being Moderated
The topic came up in a previous posting of mine about how you shouldn't design a system based on the efficiency of execution alone but then I thought of how Hibernate expects us to use a singleton session factory because creating factories takes up too much memory or CPU time (ie. it is a heavy object).
Someone in my previous posting said creating objects on today's computers takes so little time that it is really a non-issue.

What other reason is there for using a singleton session factory for Hibernate?
  • 1. Re: Efficiency VS simplicity in design patterns
    796440 Guru
    Currently Being Moderated
    The correct reason to use a Singleton Pattern is because your design calls for a single instance to exists, not for performance reasons.

    Where exactly did you get the idea that, "Hibernate expects us to use a singleton session factory because creating factories takes up too much memory or CPU time"?
  • 2. Re: Efficiency VS simplicity in design patterns
    269490 Newbie
    Currently Being Moderated
    jverd wrote:
    The correct reason to use a Singleton Pattern is because your design calls for a single instance to exists, not for performance reasons.

    Where exactly did you get the idea that, "Hibernate expects us to use a singleton session factory because creating factories takes up too much memory or CPU time"?
    Well Hibernate doesn't but those who create it and use it say the session factory should be a singleton. I looked and looked for a reason why it had to be and the only thing i read was that the factory is heavy and requires a lot of overhead to create while the actual session itself is light so you can create them to your hearts content.
    It is stupid but that is what they are saying.

    Can you give me a reason why everyone uses a singleton session factory?
  • 3. Re: Efficiency VS simplicity in design patterns
    796440 Guru
    Currently Being Moderated
    It sounds like you're paraphrasing and using non-standard and imprecise terminology. I can't really address your question unless you can cite your sources and be more clear and precise in what you're asking.
  • 4. Re: Efficiency VS simplicity in design patterns
    269490 Newbie
    Currently Being Moderated
    OK, yes my terminology is imprecise to say the least.

    Every source that offers Hibernate tutorials says that the session factory should be a singleton. It seems to only be creating the equivalent of a connection in JDBC.

    Can you give me a reason why? The following snippet seems to indicate that it is considered bad form to use anything other than a singleton session factory.


    http://docs.jboss.org/hibernate/core/3.5/reference/en/html/session-configuration.html

    3.2. Obtaining a SessionFactory
    When all mappings have been parsed by the org.hibernate.cfg.Configuration, the application must obtain a factory for org.hibernate.Session instances. This factory is intended to be shared by all application threads:

    SessionFactory sessions = cfg.buildSessionFactory();

    Hibernate does allow your application to instantiate more than one org.hibernate.SessionFactory. This is useful if you are using more than one database.
  • 5. Re: Efficiency VS simplicity in design patterns
    269490 Newbie
    Currently Being Moderated
    http://www.javaworld.com/javaworld/jw-10-2004/jw-1018-hibernate.html?page=4


    SessionFactory interface
    The application obtains Session instances from a SessionFactory. Compared to the Session interface, this object is much less exciting.

    The SessionFactory is certainly not lightweight! It's intended to be shared among many application threads. There is typically a single SessionFactory for the whole application—created during application initialization, for example. However, if your application accesses multiple databases using Hibernate, you'll need a SessionFactory for each database.
  • 6. Re: Efficiency VS simplicity in design patterns
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    ttb999 wrote:
    OK, yes my terminology is imprecise to say the least.

    Every source that offers Hibernate tutorials says that the session factory should be a singleton. It seems to only be creating the equivalent of a connection in JDBC.

    Can you give me a reason why? The following snippet seems to indicate that it is considered bad form to use anything other than a singleton session factory.


    http://docs.jboss.org/hibernate/core/3.5/reference/en/html/session-configuration.html
    First that document is referring to a very specific factory. It isn't a reference to all factories.

    Second the point of a factory instance is to create other classes. It is not logical to consider that one should use other instances although as a general statement the factory itself would define whether that was applicable or not.

    Third the specific factory that you state seems to be implicitly tied to jdbc connections and pooling. A pool by its very nature requires a single instance of the pool. If there is more than one instance there is more than one pool.

    Fourth notice that in the above there is nothing that says anything about singletons. You use a singleton because you want a single instance. The usage of the factory, by its nature and requirements, might require or suggest a single instance. Those factors lead to a singleton. Just as would be the case for any other class (non-factories) that required a single instance.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points