1

I'm confused about the difference between DataSource and SessionFactory.

I think SessionFactory is a manager for retrieve Sessions (which are in fact connections to a database I guess).

DataSource has the method getConnection()

"Attempts to establish a connection with the data source that this DataSource object represents."

It means a DataSource object always works directly with the database?

If I have a pool of connections, DataSource will ask to the pool for a connection? Or will it try to get another connection to the database.

Are in fact DataSource and SessionFactory the same? Both try to give sessions/connections?

5
  • What SessionFactory class are you talking about?
    – user207421
    Dec 9, 2014 at 19:19
  • I'm talking about SessionFactoryof Hibernate.
    – Ignasi
    Dec 9, 2014 at 19:58
  • 2
    So the SessionFactory gives you a Hibernate session and the DataSource gives you a JDBC connection. What's the question?
    – user207421
    Dec 9, 2014 at 20:12
  • If I have a pool of connections, DataSource will ask to the pool for a connection?
    – Ignasi
    Dec 9, 2014 at 20:43
  • That's a completely different question, and the answer is that you have it back to front. Normally in that situation the DataSource is the pool.
    – user207421
    Dec 9, 2014 at 21:39

1 Answer 1

1

Datasource maintains the pool of connections, establishes the connection, knows how to connect etc.,

User configures data source either in the server(like websphere/weblogic) or in the spring/hibernate configuration file. Either way you give the handle of this data source to the sessionfactory during the app startup.

SessionFActory is an interface which hides all the details like opening the connection / closing the connections. Through out your development time you dont worry about these small things like loading driver/closing connection/opening etc., so you can concentrate on business logic/other important stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.