ODBC connection in Windows Service fails on second connection attempt

0

I wrote a .Net Windows service to copy data from a 4D database (v12) to a SQL Server database every 4 hours. It is running on Windows 7 32-bit and running as Local System. I am using version 12.02 of the 4D ODBC driver.

When the service 1st runs everything works OK. On its second run the connection to SQL Server still works, but the ODBC connection to 4D gets the following error:

System.Data.Odbc.OdbcException (0x80131937): ERROR [08001] Client unable to establish connection: Is the SQL Se at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle) at System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions) at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.Odbc.OdbcConnection.Open() at DataWarehouseLoader.DAL.DWGenerator.Run() at DataWarehouseSvc.DataWarehouseSvc.OnTimerElapsed(Object sender, ElapsedEventArgs e)

The code is careful to dispose of the connection to both SQL Server and 4D after each run in the following way:

using (_connSQL = new SqlConnection(DataWarehouseCS))
{
   _connSQL.Open();

   using (_conn4D = new OdbcConnection(4DCS))
   {
      _conn4D.Open();
      Load4DTables();
   }   
}

Once the error occurs the ODBC connection never works again until I stop/start the service. This will allow it to work again one time. I have tried connecting with a System DSN as well as a DSN-less connection, but the results are the same. I believe if I have the service run more frequently, say once every 10 minutes, it will work more than once, but something must happen when enough time goes by that stops it from working.

I'm very confused about what the problem is. I'd greatly appreciate any help or suggestions to try.

.net
service
ado.net
odbc
4d-database
asked on Stack Overflow Jan 20, 2013 by CHS • edited Jun 26, 2014 by Noctis Skytower

1 Answer

0

I've not been able to find what's wrong. I believe this is a bug in the 4D ODBC driver. My work around is to create a separate console app to handle the 4D part.

I can launch this console app as a new process from the service when needed. This way when it finishes the console app that was connected to 4D unloads from memory but the service can remain running.

Unloading the program from memory seems to clean up whatever the connection problem is w/ 4D and allows subsequent runs to still work.

answered on Stack Overflow Jan 22, 2013 by CHS

User contributions licensed under CC BY-SA 3.0