问题描述:

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?

网友答案:

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.

相关阅读:
Top