Another good benefit of using Application Name in a connection string
I’ve seen a plethora of posts like this one which mention the benefits of using “Application Name” in your connection string. I thought I’d add on to this another neat item that people could use. While it isn’t exactly reinventing the wheel, the technique is commonly forgotten about. We use it in WayPoint since it can run multiple imports into a CaseLogistix library (SQL database) at the same time.
So, first we add the “Application Name” to the SQL connection string but we append a GUID to the end of it (to make sure it’s unique). Then, if our application needs to be aware of multiple instances connected to the same SQL database, we simply run this query:
select count(*) from master.dbo.sysprocesses JOIN master.dbo.sysdatabases ON master.dbo.sysprocesses.dbid = master.dbo.sysdatabases.dbid where IsNull(program_name, '') <> '' AND program_name like 'WayPoint-%' AND program_name <> 'WayPoint-<%% Our current GUID here %%>' AND master.dbo.sysdatabases.name = '<%% Our current database name %%>'
This tells us the count of processes of other WayPoints running. Even better, we can dive further into this query to get info about the other processes and even kill them off if needed or do whatever with them. Not rocket science but a good thing to have in your back pocket if you need it.
blog comments powered by Disqus