I have two separate projects within a solution. They both require creating connections to the same database. However, for some reason the connection opens properly in one project, but doesn't in the other. The code is very similar, and the connection string is identical. This problem is quite mind-boggling, and for the life of me I can't figure this out.
This one works... The configuration is:
<add name="Database.1" connectionString="Data Source=SPFLKD1;User ID=svc_acct;Password=hidden" providerName="Oracle.DataAccess.Client"/>
The code:
public UnitOfWork(String connectionName)
{
Verify.NotNullOrWhiteSpace(connectionName, "connectionName");
connection = CreateConnection(connectionName);
try
{
connection.Open(); // Successfully opens a connection
transaction = connection.BeginTransaction();
}
catch (Exception)
{
connection.Dispose();
throw;
}
}
private static IDbConnection CreateConnection(String connectionName)
{
var configuration = ConfigurationManager.ConnectionStrings[connectionName];
var providerFactory = DbProviderFactories.GetFactory(configuration == null ? String.Empty : configuration.ProviderName);
var connection = providerFactory.CreateConnection() ?? new OdbcConnection();
connection.ConnectionString = configuration == null ? String.Empty : configuration.ConnectionString;
return connection;
}
The above returns a proper Oracle connection. Now the below, in a separate project, doesn't work. It references the same connection string (from a different app.config file).
private static string SPFConnection(int id)
{
var systemId = id + 1;
return "SpfDatabase." + systemId;
}
private IDbConnection CreateConnection(String connectionName)
{
var configuration = ConfigurationManager.ConnectionStrings[connectionName];
var providerFactory = DbProviderFactories.GetFactory(configuration == null ? String.Empty : configuration.ProviderName);
var connection = providerFactory.CreateConnection() ?? new OdbcConnection();
connection.ConnectionString = configuration == null ? String.Empty : configuration.ConnectionString;
return connection;
}
public IList<string> GetUserRoles(string username, int facilityId)
{
var retVal = new List<string>();
var connection = CreateConnection(SPFConnection(facilityId));
try
{
connection.Open(); // Open method throws exception
... other code
catch (Exception e)
{
throw new Exception("Unable to get SPF User Roles for '" + username + "'", e);
}
}
The exception is a NullReferenceException, and stack trace is:
at Oracle.DataAccess.Client.OracleConnection.Open()
at TDNR.Data.SPF.SPFData.GetUserRoles(String username, Int32 facilityId) in c:\DefaultCollection\LLK - TDNR\TDNR-Support-Release 3.x\TDNR.Data\SPF\SPFData.cs:line 57
Please note that the connection object is NOT null. Instead, the exception comes from the OracleConnection.Open() method.
I realize the above code itself might not be the fault why it doesn't work. I have done the debugging, and everything (the connection object, the configuration and connection string) seems to be exactly the same up until the connection.Open() method gets called. The first one succeeds, and the second one fails.
I don't know exactly what to look for, so I don't really know what to post. If anyone can help it would be greatly appreciated.