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

Generated by cgit