Question

The use of FBConnection is giving me some trouble. In examples of the Firebird Ole-Db I find only examples using a static main method, but I'm not sure how to implement the use of an FbConnection in instance methods.

Right now I'm initializing and using the connection as shown in the code example below. Every now and then I get the error "connectionstring is not initialized". The connection object is not null, but the connectionstring seems to be null. What causes this behaviour? Should I reinitialize the FbConnect object every time I access the method (making it a local variable), or is this performance-wise a very bad idea?

 public class MyUserStore<TUser> : IUserPasswordStore<TUser, int>, IUserStore<TUser, int>, IDisposable where TUser : ApplicationUser, new()
                    { 
                private FbConnection Connection =  new FbConnection("User=-----;" +
                                                 "Password=-------;" +
                                                  "Database=C:\\------\\Testing.GDB;" +
                                                  "DataSource=localhost;" +
                                                  "Dialect=3;Charset=NONE;");
             public Task<TUser> FindByIdAsync(int userId)
                    {
                        if (userId == 0)
                        {
                            throw new ArgumentNullException("userId");
                        }
                        TUser User = null;

                        if (Connection  != null)
                        {
                            FbTransaction transaction = null;     
                            FbDataReader Reader = null;
                            using (Connection)
                            {
                                try
                                {
                                    Connection.Open();

                                    FbCommand Command = new FbCommand(GetByIdQuery, Connection);  
                                    Command.Parameters.AddWithValue("id", userId);
                                    Reader = Command.ExecuteReader();  
                                catch (Exception e)
                                {
                                   if (transaction != null)
                                   {
                                    transaction.Rollback();
                                   }
                                   System.Diagnostics.Debug.WriteLine(e.StackTrace);
                                   return Task.FromResult<TUser>(null);
                               }
                               finally
                               {
                                if (Reader != null)
                                {
                                    Reader.Close();
                                }
                                Connection.Close();
                               }
                        }
                    }
        }
Was it helpful?

Solution

Mark Rotteveel is right in his comment. Apparently, the using clause means the resource is being disposed of right at the end of the block. This means I would need to create a new connection every time I use the "using" block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top