#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Firebird locking database file


    I have the following code to create a database:

    Code:
    public class FirebirdDatabase
    {
        FbConnection connection;
    
        public void CreateDatabase()
        {
            try
            {
                FbConnectionStringBuilder cs = new FbConnectionStringBuilder();
                cs.Database = "C:\TestDb.GDB";
                cs.UserID = "SYSDBA";
                cs.Password = "masterkey";
    
                FbConnection.CreateDatabase(cs.ToString(), true);
                this.connection = new FbConnection(cs.ToString());
                this.connection.Open();
                //...
                //create tables etc.
                //...
                this.connection.Close();    //doesn't release DB file
                this.connection.Dispose();  //doesn't release DB file
            }
            catch
            {
            }
        }
    }
    The first time I use this class to create a database everything works fine. But the second time I call "CreateDatabase()" I get an error. It turns out that even when "this.connection.Close();" has been called, "C:\TestDb.GDB" is still locked for editing by my application. Closing the application unlocks the file again.

    How can I release this file within my application?

    Thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0
    An even more simplified example of the problem...

    Code:
    public class FirebirdDatabase
    {
        FbConnection connection;
    
        public void CreateDatabase()
        {
            try
            {
                FbConnectionStringBuilder cs = new FbConnectionStringBuilder();
                cs.Database = "C:\TestDb.GDB";
                cs.UserID = "SYSDBA";
                cs.Password = "masterkey";
    
                FbConnection.CreateDatabase(cs.ToString(), true);
                this.connection = new FbConnection(cs.ToString());
                this.connection.Open();
                this.connection.Close();    //doesn't release DB file
                this.connection.Dispose();  //doesn't release DB file
                File.Delete(cs.Database);   //fails here!
            }
            catch
            {
            }
        }
    }
    Full error message:

    Code:
    The process cannot access the file 'C:\TestDb.GDB' because it is being used by another process
    Why is it still locked?

    I'm using the latest releases:

    - FirebirdSql.Data.FirebirdClient.dll v2.6.0.0
    - Firebird Server v2.5.0.26074
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0
    Just in case anybody else has this problem... The solution is to turn off connection pooling in the connection string:

    Code:
    FbConnectionStringBuilder cs = new FbConnectionStringBuilder();
    cs.Database = "C:\TestDb.GDB";
    cs.UserID = "SYSDBA";
    cs.Password = "masterkey";
    cs.Pooling = false;
    If this is not set the file is not unlocked when the connection is closed. Only the connection is returned to the pool.

    Thanks to Ing. Frie▀negg Patrick on the Firebird-Support group for the answer.

IMN logo majestic logo threadwatch logo seochat tools logo