Announcement Announcement Module
No announcement yet.
dao (plain hibernate), singleton? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • dao (plain hibernate), singleton?


    Im working on developing a webapplication with Spring Framework 3.0.5 and Hibernate 3.6.
    I use transaction management from spring and I implemented my DAO based on plain Hibernate 3 API, as described in the documentation of spring framework.

    Now I use dependency injection to inject my dao into different classes (which want to access the database). My dao is a singleton.

    I would like to figure out the advantages of using hibernate together with spring and thats why I got a simple question:

    Is it true that without spring for every class which needs to use a DAO and everytime the class wants to use the dao a new DAO-Instance must be created? Which means that using a singleton-DAO is only possible because of spring?

    I mean, I could use in spring different instances of a DAO too and inject them in different classes, so that each class would work with a different DAO instance, but that seems not to be necessary, true?

    It would be great if someone could tell me something about it, because with google I didnt find out that much and Im a newbie.

    Thank you for sharing your knowledge :-)

  • #2
    The fact that you can use a singleton dao or not has NOTHING to do with the fact that you use spring or not... It simply is a matter of asking the question is my dao thread safe, if you don't keep state you can perfectly create a single instance of the dao yourself and pass the instance around (or use some utility class).


    • #3
      I understand. Sorry for asking, but I found this answer related to this topic:

      With Hibernate / JPA, the dao requires a fresh session/entity manager. But spring handles that by injecting a proxy which locates the fresh session each time. So having a singleton DAO is the proper way to go.
      thats why I thought it was because of spring (and working with proxies) that using a singleton DAO was possible.