blob: 130147ad94e2cbcd3be2cd40b523f5213626334d (
plain)
1 Kill All Connections to SQL Database
2 ====================================
3 :author: Aaron Ball
4 :email: nullspoon@iohq.net
5
6
7 == {doctitle}
8
9 I recently had to help one of our testers test an application. One of the tests
10 to perform was to see how the web application handled losing its connection to
11 the database in the middle of various operations.
12
13 My first thought on how to do this was to simply take the database offline
14 during a session. Unfortunately however, SQL Server Management Studio won't
15 kill current connections when this operation is attempted, rather it will error
16 out.
17
18 After searching around I found a query that in essence kills all connections to
19 the database but one (single-user mode).
20
21 *The query for this.*
22
23 ----
24 -QUERY NUMERO UNO
25 ALTER DATABASE [DATABASE-NAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
26 -And let's put this DB back in multi-user mode
27 ALTER DATABASE [DATABASE-NAME] SET MULTI_USER
28 ----
29
30 In this query, database-name is switched to single_user mode. The second query
31 sets the database back to multi_user mode.
32
33 Ah such simplicity.
34
35 Category:MySQL
36
37
38 // vim: set syntax=asciidoc:
|