Question

I have a question related to Hibernate Connection, Session and SessionFactory. The queries are as below:

1) Is it a good practice to open up a connection on every request and close it after that?

2) There is a single sessionfactory which opens up the sessions for each request, What is the correlation of it with Connection? I know that on connection the sessionfactory opens sessions for different requests, When should open up and close a connection and when should we just open up a session and close it? Is it like, we should open a new connection on every login and close that connection on logout or hibernate does it itself?

Thanks in advance for your kind review

Regards Rahul

Was it helpful?

Solution

Is it a good practice to open up a connection on every request and close it after that?

No this is not good practice.see this with Hibernate use connection pooling

There is a single sessionfactory which opens up the sessions for each request, What is the correlation of it with Connection?

Hibernate uses the connection pool when we get the session from SessoinFactory from one of the connections are allocated to session

When should open up and close a connection

With hibernate you don't worry about the JDBC Connection as long as you are thinking about the performance

when should we just open up a session and close it

For all the CRUD operations

Is it like, we should open a new connection on every login and close that connection on logout or hibernate does it itself?

No User login and JDBC Connection has no relation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top