You're running a legacy line-of-business app that talks to SQL Server through COM+. Everything's been fine for months. Then one afternoon, the database server fails over to its replica—maybe a network blip, maybe a patch reboot. Suddenly your app throws CONTEXT_E_OLDREF (0X8004E007) on every new transaction. Users see it as a generic error, but the event log shows the COM+ component can't get a fresh transaction context.
The root cause is simple: COM+ caches a reference to the transaction context object. When the database connection drops mid-transaction, that reference goes stale. The next call tries to use the old context, and COM+ refuses because the transaction is no longer valid. It's like having a phone number saved for someone who just moved—you dial, but you get a dead line.
The fix is to force COM+ to release that stale reference and start clean. You don't need to reboot the server (unless you want to, but that's overkill). You just need to reset the COM+ application that hosts your component. Here's how to do it on Windows Server 2016 or 2019.
- Open the Component Services snap-in. Press Win + R, type
dcomcnfg, and hit Enter. You'll see the Component Services tree. - Expand Component Services > Computers > My Computer > COM+ Applications. You'll see a list of applications. Find the one that matches your component—usually named after your app, like "MyAppServer".
- Right-click that application and select Shut down. This stops all running components in that app. You should see the application icon change to a stopped state. If it's already stopped, skip this step.
- Right-click the same application and select Start. This restarts the COM+ app and forces it to create fresh transaction context objects. Wait a few seconds—the icon should go back to normal.
- Now test your app again. Try a simple transaction that previously failed. If the error is gone, you're done. If not, continue to the next step.
Sometimes shutting down the app isn't enough because the old reference sits in the Microsoft Distributed Transaction Coordinator (MSDTC) cache. You might need to restart that service too. Here's how:
- Open an elevated Command Prompt (right-click and select Run as administrator).
- Type
net stop msdtcand press Enter. The service will stop. Wait a few seconds. - Then type
net start msdtcand press Enter. This restarts MSDTC and clears any lingering transaction state. - Go back to Component Services and start the COM+ application again if it stopped (right-click > Start).
- Test your transaction again. This usually does the trick.
If you're still hitting 0x8004E007 after these steps, you need to look at your connection pooling. COM+ components often pool database connections, and if the pool holds a connection to the old database server, you'll keep getting stale contexts. The real fix is to clear that pool.
Here's a quick way to clear it without restarting the app:
sqlcmd -S your_database_server -E -Q "DBCC FREEPROCCACHE"
That clears the procedure cache on the SQL Server side, which forces new connections to be created. But if your component uses a specific connection string, you might need to update it to point to the new server if the failover changed the IP or hostname. Check your application's config file (like web.config or an XML config in the COM+ package) for the database server name.
Also, consider bumping up the transaction timeout. If your database failover takes longer than the default 60 seconds, COM+ will give up and mark the context as stale. You can increase it in the COM+ application properties:
- Right-click your COM+ application and select Properties.
- Go to the Transactions tab.
- Under Transaction Timeout, set it to a higher value, like 120 seconds. Click OK.
- Shut down and restart the application again to apply the change.
After you've done all this, the error should be gone. But here's the thing: this will happen again after the next failover. So plan ahead. Set up a scheduled task that runs at 3 AM and resets the COM+ app. Or better, update your code to handle the stale context gracefully—catch the CONTEXT_E_OLDREF HRESULT and retry the transaction once. That's the real long-term fix.
If you try everything and still see the error, check the Windows Event Log under Applications and Services Logs > Microsoft > Windows > COM+. Look for event ID 4096 or similar—it might point to a specific component that's not releasing references properly. Sometimes the issue is a bug in the component itself, and you'll need to contact the vendor for a patch.
One last thought: don't ignore this error. It's not just a nuisance. It can cause silent data corruption if you don't restart the transaction properly. So when you see it, stop what you're doing and reset the COM+ app—every time.