Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a database session?

I understand a general understanding of the concept of a database transaction. We access a database within transaction to ensure ACID properties.

In Hibernate there is a concept called a session. What is the use of a session? When should database access happen in two sessions rather than in the same session?

To explain more, I have seen hibernate code that

  • gets a session from a session factory
  • opens a session
  • begins a transaction
  • commits the transaction
  • closes the session

What I need to know is what is the importance of a session here? Why not have something like a transaction factory, begin transaction and commit transaction?

like image 859
Can't Tell Avatar asked May 09 '12 18:05

Can't Tell


People also ask

What is database session in SQL Server?

A SQL session is an occurrence of a user interacting with a relational database through the use of SQL commands. When a user initially connects to the database, a session is established.

What is a database session in Oracle?

A session consists of every connection to the Oracle database by a user from a specific process. By tracking the number of Oracle sessions, we can track how busy a particular server is. This gives insight on how loaded an Oracle Database is. It also helps understand which users take more system resources.

What is difference between session and connection?

Literally : Connection is Physical Communication Channel and Session is a state of information exchange. A Connection may have multiple sessions . The connection is the physical communication channel between SQL Server and the application: the TCP socket, the named pipe, the shared memory region.

What is a session in MySQL?

From the MySQL manual: With a SESSION modifier, the statement displays the status variable values for the current connection. If a variable has no session value, the global value is displayed.


2 Answers

Session is more than just a transaction, it is an implementation of UnitOfWork pattern. In other words it holds on to the loaded objects, knows which objects has to be persisted etc:

A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.

To better understand relation between Session and Transaction you can take a look at this article.

A single Hibernate Session might have the same scope as a single database transaction.

This is the most common programming model used for the session-per-request implementation pattern. A single Session and a single database transaction implement the processing of a particular request event (for example, a Http request in a web application). Do never use the session-per-operation anti-pattern! (There are extremely rare exceptions when session-per-operation might be appropriate, you will not encounter these if you are just learning Hibernate.)

Another programming model is that of long Conversations, e.g. an application that implements a multi-step dialog, for example a wizard dialog, to interact with the user in several request/response cycles. One way to implement this is the session-per-request-with-detached-objects pattern. Once persistent objects are considered detached during user think-time and have to be reattached to a new Session after they have been modified.

The session-per-conversation pattern is however recommended. In this case a single Session has a bigger scope than a single database transaction and it might span several database transactions. Each request event is processed in a single database transaction, but flushing of the Session would be delayed until the end of the conversation and the last database transaction, to make the conversation atomic. The Session is held in disconnected state, with no open database connection, during user think-time. Hibernate's automatic optimistic concurrency control (with versioning) is used to provide conversation isolation.

like image 51
Dmitry Avatar answered Oct 07 '22 02:10

Dmitry


@Dmitry has answered very nicely.

Another way to look at the session is as Instance of Database Usage. When you create a session, you have a context ready for any database interaction with support services (e.g. transaction, caching, connection etc) required therein. A transaction is an independent service used within the session.

Also the session is the first level cache used by the typical OR mapping tools like hibernate. Session acts as temporary context created upon request to facilitate a DB interaction.

like image 30
Santosh Avatar answered Oct 07 '22 01:10

Santosh