Sessions
Enable the Database Tools and SQL plugin
This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in IntelliJ IDEA by default. If the relevant features are not available, make sure that you did not disable the plugin.
Press Ctrl+Alt+S to open settings and then select
.Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.
In IntelliJ IDEA, connections to the database are established in special wrappers – sessions. Each session is a wrapper over a single connection, and it stores the connection's information (for example, whether it is active or not, transaction control mode, and other settings).
Within a session, connections can open and close when required and in accordance with the defined settings.
Sessions can have clients – files, whose queries are sent by using the connection that the session holds. Data editor can also be a client for a session.
You can view data sources, sessions, and session clients in the Services tool window. The green dot in the corner of a session's icon indicates a connected session.
In the picture above, the Services tool window displays the following nodes:
Data source.
Disconnected session.
Connected session.
Session clients.
For more information about the session managing options and toolbar controls, refer to the Services tool window topic.
If you close IntelliJ IDEA and open it again, all your sessions will be available in the Services tool window, but they will be empty.
New session
Depending on the way you create a new session, it can be connected either automatically or after a certain action. The green dot in the corner of a session's icon indicates the connected status.
You can create a new session by doing either of the following:
Open a query console, view database object's data in data editor, or attach an SQL file to a data source.
As a result, under the data source node in the Services tool window, the new session node appears with a client node under it.
For a query console, the session will be connected once you perform an action that requires interaction with the database. For example, once you run a query.
For a table, the session is connected automatically, as IntelliJ IDEA requires an active connection to request the table data from the database, receive it, and display it in the data editor.
For an SQL file, the session is connected automatically. To run queries against either of the data source databases or schemas, you have to attach your file to them by selecting them in the <schema> list.
Perform an action that requires interaction with the database. For example, run a stored procedure or run a script by using run configurations.
As a result, the new connected session node appears under the data source node in the Services tool window.
Manage sessions
Switch sessions
In case you need to run your queries or SQL file in a different session, you can switch the session to another one.
To switch the session, in the Services tool window, right-click the file that you want to switch the session for, click Switch Session and select the session that you want to attach the file to.
Work in a single session
You can run queries from multiple SQL files and receive multiple database objects data in a single session. For example, if each new query requires an MFA approval.
To work in a single session, enable the single session mode by doing the following:
Open the Data Sources and Drivers dialog (Ctrl+Alt+Shift+S) and select your data source.
On the right pane of the dialog, open the Options tab.
In the Connection settings section, select the Single session mode checkbox.
Click OK to apply the changes.
(Optional) If the Apply Changes dialog appears, select the way you want to proceed.
No: Leaves the connected sessions intact.
Yes: Disconnects all the connected sessions.
Run your queries.
All new queries will run in the default session.
The following table includes other basic actions that you can use to work with sessions.
Action | Description |
---|---|
Rename a session | In the Services tool window, right-click a session and select Rename Session. |
Move a session or a data source to a separate tab | In the Services tool window, right-click a session or a data source and select Open in New Tab. To revert this action, close the separated tab. |
Close a session | In the Services tool window, right-click a session and select Close Session. The session will be detached from its session clients, it will also disappear from the list. |
Close all sessions | In the Services tool window, right-click a data source and select Close All Sessions. |
Services tool window reference
For more information about tool window controls and tabs, refer to the the Services tool window topic.