How to identify processes spawned by a .Net app connecting to POSTGRESQL - c#

I have a C# .net application that connects to a postgresql database.
Experimentation shows that every connection made from the application creates a new postgres.exe process visible in task-manager and traceable in the database.
The C# app has a PID, the postgres connections it spawns also have PIDs, how can the app establish the PID it just created?
I'd like to establish which C# connection results in which database process. I naively assumed that the parent PID of any database connection would match the PID of the application that created the connection. This doesn't appear to be the case.
The best bodge I've managed is to list the postgres.exe processes immediately prior to creating the connection
Process[] processBefore = Process.GetProcessesByName("postgres");
Then make the connection
NpgsqlConnection myDatabaseConnection= new NpgsqlConnection(myConnectionString);
Then list all the postgres processes present afterwards
Process[] processAfter = Process.GetProcessesByName("postgres");
Then compare the two lists and assume that the new process is the one established by the connection.
In practice this seems to work most of the time, but other processes can create (and remove) postgres.exe instances, so there's scope for cock-up.
Is there a better/easier/more reliable way of doing this?
UPDATE - Why do I need to do this? - Fault-finding. I have several different instances of the C# app all making multiple connections to the database. I can monitor all the database sessions, (and their operating system PIDs) from the postgres dashboard. Some of those sessions are running dodgy SQL that's causing problems (conflicts/locks/etc).
Since I don't know the PID of the connection, I can't link the dodgy SQL back to it's source in the app. The basic problem seems to be that although I know the PID of the app, and the app creates the connections, the connection does NOT have a parentPID that matches the app.

Related

SQLServer AlwaysOn and .net SqlConnection reset

In our enviroment we use SQLServer Always on cluster with two servers.
One of them is for write, second for reading. In application services SqlConnection opens and closes every second for short query execution. But after switching or turning off one of the servers in Always On cluster I began get an exceptions from my application services.
This tells that it can not insert any data in read only database. I suppose that main reason for this is connection pool inside SqlConnection implementation.
So the question is how to reset that connection pool manually. Or if there another kind of problem - let me know what do you think about this behavior.
You need to set MultiSubnetFailover = True in connection string and implement retry logic:
If a SqlClient application is connected to an AlwaysOn database that
fails over, the original connection is broken and the application must
open a new connection to continue work after the failover.
SqlClient Support for High Availability, Disaster Recovery
Also:
Setting MultiSubnetFailover to true isn't required with .NET Framework
4.6.1 or later versions.

VS application can't connect to database after attaching it in SSMS

I have a windows form application (developed in VS Express 2013) that connects through an instance of SQL Server 2012 to a database. Both the application and database are on my local system; I just needed a GUI to more easily interface with this very large database that stores my research data. When I initially compile and deploy the application, it works fine and has no connection problems with the database. However, if I then attach the database in SQL Server Management Studio (which I sometimes want to do) I get an error the next time I try to use the application - "Cannot open database ..... requested by the login, the login failed". I get this error even if I take the database offline and detach it before quitting SSMS. And just to be clear - I'm not making any changes to the database in SSMS, I'm just looking at the data. The connection string used by VS is Data Source (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CollectionMetricsDatabase.mdf;Integrated Security=True.
Any idea what the problem is?
Maybe the instance of SQL Server 2012 has a max number of concurrent connections and you exceed that with your two connections at once (SSMS connection and application's connection). Check the settings in SSMS.
Another thing to try - Based on your connection string, you are using Integrated Security, which in my experience means the application connects using Windows credentials. That can be fragile, because it means that whatever 'user' runs the application (if hosted in IIS, this will be the App Pool user) is the one whose Windows credentials are used. I would suggest creating a SQL login user (in SSMS) for the database, with db_owner access, and then changing the application's connection to be username/password based instead. You may need to enable Mixed Authentication for the SQL Server instance in SSMS if it doesn't already allow it.
You need to connect in SSMS with the localDB connection string. Attaching it will prevent it from working with localDB.
In SSMS, create a connection to (LocalDB)\v11.0. You should see your DB there already.
The problem is that you are connecting to the database file directly not through SQL Server.
Only one user can have a lock on the file at the time.
You need to recreate a connection string for your app.
That is why you were needing to detach the database.
Since this is only a personal project you could live with things as they are, at least you know the server is only running when you need it.
create connection msdn

Issue with Microsoft Access

We have 2 legacy systems - One in C++ and other in C# that connect to the same access database. The access database is not password protected and we use the following connection string to connect to the databases
Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=False ;Data Source= AlarmHistory.mdb
C# application polls a table in the database every 10 seconds. Once in while , the C# application crashes with the error message -
System.Data.OleDb.OleDbException: Cannot start your application. The workgroup information file is missing or opened exclusively by another user.
The log analysis revealed that both the applications are not accessing the database simultaneously ... Any idea on what could cause such a situation... Please Help
They might not be accessing the database at exactly the same time, but that error message indicates that they (or something else) is accessing the lock file at the same time. Is the poll opening and closing the connection each time? You could move the open and close to outside the timer so that the connection is opened and remains open for the life of the application.

C#: How to graciously manage errors on the application DataBase?

I have a C# application that uses a localhost DB (MySQL).
Now, when I create the executable I´m assuming that the receptor computer MUST have the exact DB with the the same name and tables, also, must have running WAMP or XAMPP.
If one of this conditions is not accomplished the program will crash horribly, with the errors of Windows/C#.
I could put exceptions for every case, but I´m fearful that I would hide other errors putting exceptions for everything!
With production software, how do you manage this? With exceptions? Writing a manual for the user? etc?
During bootstrapping, I recommend check to see if a DB Connection can be created (in my case, SQL Server), given the database connection string defined in an app.config. Initially, you should do some version checking on the database. If the database can't be found, attempt to create it. if i'ts out of date, attempt to upgrade it. If this process fails, then your database engine instance isn't installed or is unresponsive. For my application case, I exit the program, as there's nothing else to do if the DB can't be accessed.
Once past this point, I generally assume that the DB connection is active.

Spring.net Drop all adotemplate connections?

I have an application which is connected to a database through a spring.net AdoTemplate. I am charged with creating a restore database method which keeps the app running but drops the network connections so as to drop the old database and bring up the new one. My question is how do I drop all the current connections that this application has to this AdoTemplate? I do not see any public method in spring 1.1 to drop the network connections.
there is no physical "connection" between AdoTemplate and the SQL database. Leaving transactions aside, AdoTemplate creates a new SqlConnection object for each method that is executed from ADO.NET, executes a command and disposes the SqlConnection object after that.
Under the hoods, ADO.NET caches physical connections to the database in a pool. When you create a new SqlConnection object, 1 of those cached physical connections is obtained from the pool to serve that SqlConnection.
This means, that you will need a different strategy for solving your problem. One strategy coming to my mind is to obtain the list of active connections from the sysprocesses database and execute the KILL statement on them. Short googling brought up this article. Note, that this article refers to mssql 2000. I'm pretty sure, that you need to google a bit more to find a solution for 2005. Since 2005 it isn't allowed to access system tables anymore asfair.
hth,
Erich
What ended up working great was:
SqlConnection.ClearAllPools();
// if any connections were being used at the time of the clear, hopefully waiting
// 3 seconds will give them time to be released and we can now close them as well
Thread.Sleep(3000);
//clear again
SqlConnection.ClearAllPools();

Categories

Resources