Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track database connection leaks

We have an app which seems to have connection leaks (SQL Server says that the max pool size has been reached). I am alone on my dev machine (obviously), and just by navigating the app, I trigger this error. The SQL Server Activity monitor shows a great number of processes using my database.

I want to find which files open connections but do not use it. I was thinking of using something like grep to, for each file, count the number of ".Open()" and the number of ".Close()", and get the file for which the numbers are not equal. Is it realistic?

Bonus question: do the processes found in SQL Server Activity Monitor correspond to the connections? If not, how do I find out how many connections are open on my database?

The app is in asp.net (vb) 3.5, with SQL Server 2005. We currently do not use LINQ (yet) or anything like that.

Thanks

like image 321
thomasb Avatar asked Apr 21 '11 08:04

thomasb


People also ask

How do I check for DB connection leaks?

The database connection leaks should be identified and fixed in the code. It can be accomplished by using the dbconnection watchdog logger. The log files can indicate where the connection leaks might be.

What causes database connection leak?

A connection leak means some of the database request/transaction are not getting closed properly or are not getting committed and finally those connections are getting abondoned and closed permanently.

What is DB leak?

A data leak is when sensitive data is accidentally exposed physically, on the Internet or any other form including lost hard drives or laptops. This means a cyber criminal can gain unauthorized access to the sensitive data without effort.

How do I check database connection pool?

From the JDBC Connection Pool—>Monitoring tab, you can view information about the state of each deployed instance of the selected connection pool. That is, for each server on which the connection pool is deployed, you can see current status information about the connection pool.


1 Answers

When looking at the code from the SQL Server side you can run the following query to get a view on which queries are last run on sleeping connections. (open connections which are doing nothing)

SELECT ec.session_id, last_read, last_write, text, client_net_address, program_name, host_process_id, login_name
FROM sys.dm_exec_connections  ec
JOIN sys.dm_exec_sessions es
  ON ec.session_id = es.session_id
CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) AS dest
where es.status = 'sleeping'

From the application side you can debug with sos.dll as described in the following articles:

  • How to troubleshoot leaked SqlConnection Objects Part 1
  • How to troubleshoot leaked SqlConnection Objects Part 2

If you need more information on how to use windbg, these articles are a good intro:

  • Getting Started with WinDBG Part 1
  • Getting Started with WinDBG Part 2
like image 198
Filip De Vos Avatar answered Sep 20 '22 06:09

Filip De Vos