36

I'm a bit confused by MySQL concepts: session vs connection.

When talking about connecting to MySQL, we use connection terminology, connection pool, etc.

However in the MySQL online doc: http://dev.mysql.com/doc/refman/4.1/en/server-system-variables.html, it talks about session variables.

They are quite similar. How to distinguish them?

Thanks in advance.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Xiaofeng Tang
  • 476
  • 1
  • 6
  • 8

2 Answers2

42

A session is just a result of a successful connection. Any MySQL client requires some connection settings to establish a connection and after the connection has been established it acquires a connection id (thread id) and some context which is called session.

newtover
  • 31,286
  • 11
  • 84
  • 89
  • 3
    if connection is idle too long, client try to reestablish it. a new session is initialized with a new set of session variables, right? – Xiaofeng Tang Jan 10 '12 at 09:43
  • 7
    @xiaofeng-tang, correct, all user-defined variables will be NULLed, session variables will reset to defaults and temporary tables will be lost. – newtover Jan 10 '12 at 10:21
4

Connection is the relationship between a client and a MySQL database.

Session is the period of time between a client logging in (connecting to) a MySQL database and the client logging out (exiting) the MySQL database.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129