How does the request session work?

Asked

Viewed 137 times

5

An idea arose among the developers to use the standard of Session per request - Session by request.

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

Small 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 the 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:

  1. To contextualize, as the request session consists?
  2. It’s a good solution?
  3. What would be the best way to implement? Open the connection via web even?
  4. Recommended for projects with complex queries/operations?
  5. There is a chance of a competition problem when there are transactions/transactions involved?
  • Questions 2, 3 and 4 give a lot of room for opinion.

1 answer

1

  1. To contextualize, as the request session consists?

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

You may consider that there is a session while this connection is open. That is, you can make as many requests as you want while not closing your call (for the duration of the session).

Thus, the concept of "session by request" supports the ideology that should only make a request to the database, for each time establishing the link.

  1. It’s a good solution?

Answer 1

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

Answer 2

Not in principle. From 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 carry out atomic possessions, using for example transactions (which make one or more requests to the database).

I had to test if it was possible to do several operations within a transaction on different links and that didn’t work. Even if it did, it wouldn’t make sense to make a lawsuit.

What would be the best way to implement? Open the connection via web even?

I don’t know what you’re talking about, the web app or the database? The most common database communication protocol is eventually TCP, it is also common to use named Pipes.

Recommended for projects with complex queries/operations?

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

There is a chance of a competition problem when there are transactions/transactions involved?

For this is exactly one of the problems I mentioned.


I would like to add that the example given may also have another problem. AP did not give details about the table scheme, so it is not possible to confirm. But imagining that each customer has a list of products then it would cost less to bring both at the same time (with a Join operation, this could be done in a request and consequently in a link to the database)

  • So, the example with the lists was merely "illustrative". In this example, there were only two queries in the bank. And in the case of 10 queries? Go 10 times in the bank or go one?

  • @It depends on what process you want to make. In my professional career I have never found any process that you can do with just one request instead of 10. At the most, I’ve managed to put three or four requisicoes in one. Generally less is usually better, but if the data volume is large you have to come up with strategies like batching or paging.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.