Disconnect database connections

2892
6
Jump to solution
01-31-2013 09:54 AM
CameronMcCormick
Occasional Contributor
Hello All,
We have an issue with users leaving their PCs on over night (or longer) with connections to our enterprise database.  Unfortunately, this has become quite the issue lately with us running out of connections to the database during the middle of the day, when in reality, there's 5 or more connections left over from a previous day.
I've attempted at using the arcpy.Disconnectuser function, but it seems to only like the ID, from arcpy.ListUsers.
We really need the disconnect function to be based on date.
I'm probably totally overthinking how to get this to work and have scrapped several attempts so far.
Does anyone have a python script that disconnects users based on the ConnectionTime that is generated from arcpy.ListUsers?
Is there a better way?

Thanks for any help!!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RussellBrennan
Esri Contributor
Here is a script that I have used with a little bit more flexibility than using the 'All' keyword.
# Import necessary modules. import arcpy, datetime   # Set the admin connection -> this user needs to have privilegs to disconnect users. adminConnection = r'Database Connections\dbo@testDB@testServer.sde'   # Get a list of connected users. uList = arcpy.ListUsers(adminConnection)   # Print the number of connected users count = len(uList) print 'There are currently {0} users connected\n'.format(count)   # Exception lists -> these are used to determine machine names or users # which will not be disconnected. Leave blank if there are no exceptions machines = ['devinci','rocky'] users = ['samhill', 'stevepeat', 'stevesmith']   # Get current time. now = datetime.datetime.now()   # Get time a week ago weekAgo = now - arcpy.time.EsriTimeDelta(7, 'days')   # Iterate through users for u in uList:     if u.ConnectionTime < weekAgo:         if u.ClientName.lower() in machines:             print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName)             print "Machine name is on exception list.\n"         elif u.Name.lower() in users:             print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName)             print "User's name is on exception list.\n"         else:             try:                 arcpy.DisconnectUser(adminConnection, u.ID)                 print "Successfully disconnected user: {0}\n".format(u.Name)             except:                 print arcpy.GetMessages()     else:         print "User ({0}) connection time did not exceed the set limit.\n".format(u.Name)   # Get a list of connected users. uList = arcpy.ListUsers(adminConnection)   # Print the number of connected users count = len(uList) print 'There are currently {0} users connected'.format(count)   print 'Done'

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
Why don't you just run a scheduled task at night that disconnects all users?
0 Kudos
CameronMcCormick
Occasional Contributor
Why don't you just run a scheduled task at night that disconnects all users?


Thanks!  I knew I was overthinking this whole situation.  The best part was that the ArcGIS Server services reconnected by themselves, which was my initial fear with using "ALL" when this started.
0 Kudos
RussellBrennan
Esri Contributor
Here is a script that I have used with a little bit more flexibility than using the 'All' keyword.
# Import necessary modules. import arcpy, datetime   # Set the admin connection -> this user needs to have privilegs to disconnect users. adminConnection = r'Database Connections\dbo@testDB@testServer.sde'   # Get a list of connected users. uList = arcpy.ListUsers(adminConnection)   # Print the number of connected users count = len(uList) print 'There are currently {0} users connected\n'.format(count)   # Exception lists -> these are used to determine machine names or users # which will not be disconnected. Leave blank if there are no exceptions machines = ['devinci','rocky'] users = ['samhill', 'stevepeat', 'stevesmith']   # Get current time. now = datetime.datetime.now()   # Get time a week ago weekAgo = now - arcpy.time.EsriTimeDelta(7, 'days')   # Iterate through users for u in uList:     if u.ConnectionTime < weekAgo:         if u.ClientName.lower() in machines:             print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName)             print "Machine name is on exception list.\n"         elif u.Name.lower() in users:             print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName)             print "User's name is on exception list.\n"         else:             try:                 arcpy.DisconnectUser(adminConnection, u.ID)                 print "Successfully disconnected user: {0}\n".format(u.Name)             except:                 print arcpy.GetMessages()     else:         print "User ({0}) connection time did not exceed the set limit.\n".format(u.Name)   # Get a list of connected users. uList = arcpy.ListUsers(adminConnection)   # Print the number of connected users count = len(uList) print 'There are currently {0} users connected'.format(count)   print 'Done'
0 Kudos
BenVan_Kesteren1
Occasional Contributor III

Hi, the website seems to have killed your code formatting, I have just tidied it up for use myself, thought I would re-post for anyone else that may stumble upon this thread:

# Import necessary modules. 
import arcpy, datetime   

# Set the admin connection -> this user needs to have privilegs to disconnect users. 
adminConnection = r'Database Connections\dbo@testDB@testServer.sde'  

# Get a list of connected users. 
uList = arcpy.ListUsers(adminConnection) 

# Print the number of connected users 
count = len(uList) 
print 'There are currently {0} users connected\n'.format(count)   

# Exception lists -> these are used to determine machine names or users 
# which will not be disconnected. Leave blank if there are no exceptions 
machines = ['devinci','rocky'] 
users = ['samhill', 'stevepeat', 'stevesmith']   

# Get current time. 
now = datetime.datetime.now()  

# Get time a week ago 
weekAgo = now - arcpy.time.EsriTimeDelta(7, 'days') 

# Iterate through users for u in uList:     
if u.ConnectionTime < weekAgo:    
     if u.ClientName.lower() in machines:   
          print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName)       
          print "Machine name is on exception list.\n"    
     elif u.Name.lower() in users:        
          print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName)        
          print "User's name is on exception list.\n"      
     else:
          try:
               arcpy.DisconnectUser(adminConnection, u.ID)
               print "Successfully disconnected user: {0}\n".format(u.Name)
          except:                 
               print arcpy.GetMessages()     
          else:         
               print "User ({0}) connection time did not exceed the set limit.\n".format(u.Name)

# Get a list of connected users. 
uList = arcpy.ListUsers(adminConnection)

# Print the number of connected users 
count = len(uList) 
print 'There are currently {0} users connected'.format(count)   
print 'Done'  ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
CameronMcCormick
Occasional Contributor
Here is a script that I have used with a little bit more flexibility than using the 'All' keyword.


That is exactly what I was trying to do.
Oddly, my original code looked eerily similar, barring the "arcpy.time.EsriTimeDelta," which is what was giving me the hardest time.
I really need to read through all the modules

Thanks much!!
0 Kudos
NickJacob
New Contributor II
Thanks!  Very helpful and nice use of pro downhiller names 😃
0 Kudos