How does session per request work?

5

There was an idea among developers to use the Session per request pattern - Session per request.

Researching the subject, I found some topics in the OS that generally said that the indication was for ORM frameworks.

Little practical example

//GET Controller/Teste

public ActionResult Teste()
{
     //abrir conexão com o banco

     var model = new TesteViewModel 
                 {
                      ListaClientes = _servicoCliente.ObterClientes(),
                      ListaProdutos = _servicoProduto.ObterProdutos()
                 };

     //fechar conexão com o banco
     return View(model);
}

Without session per request :

//GET Controller/Teste

public ActionResult Teste()
{
  var model = new TesteViewModel 
    {
       ListaClientes = _servicoCliente.ObterClientes(), // Abro e fecho a conexão com o banco no inicio e fim do método, respectivamente.
       ListaProdutos = _servicoProduto.ObterProdutos() // Abro e fecho a conexão com o banco no inicio e fim do método, respectivamente.
    };
  return View(model);
}

Questions:

  • To contextualize, how does session per request consist?
  • Is it a good solution?
  • What would be the best way to implement it? Open the same web connection?
  • Is it recommended for projects with complex queries / operations?
  • Is there a chance of giving a competition problem when there are operations / transactions involved?
  • asked by anonymous 02.02.2017 / 14:06

    1 answer

    1
      
  • To contextualize, how does session per request consist?
  •   

    To make a request to a database, it is necessary to open a connection to the same database.

    You may consider that there is a session while this connection is open. That is, you can make any requests you want until you close your connection (for the duration of the session).

    Thus, the concept of "session per request" supports the ideology that should only make a request to the database, for each time the connection is established.

      
  • Is it a good solution?
  •   

    Answer 1

    It depends on your application, at least this is the answer that all users here would accept without any controversy.

    Answer 2

    In principle not. In my perspective it is much better to establish a session for each business process. The reason is simple. It may be just speculation, but I would say that most applications need to hold atomic possessions, for example using transactions (which make one or more requests to the database).

    I had to test if it was possible to do multiple operations within a transaction on different bindings and this did not work. Even if it did, it would not make sense to do a bite-taking process.

      

    What would be the best way to implement it? Open the same web connection?

    I do not know what you are talking about, the web application or the database? The most common communication protocol with database is TCP, also the use of named pipes.

      

    Is it recommended for projects with complex queries / operations?

    I think I explained what I would do in my second answer to question 2.

      

    Is there a chance of giving a competition problem when there are transactions / transactions involved?

    This is exactly one of the problems I mentioned.

    I wanted to add that the given example might still have another problem. The AP did not give details about the table schema, so it can not be confirmed. But figuring that each client has a list of products would have less cost to bring both at the same time (with a join operation, this could be done in a request and consequently in a connection to the database)

        
    02.02.2017 / 16:44