Unlock SQLite database file for imediate use after inserting some data

4 posts, 0 answers
  1. Liordino
    Liordino avatar
    9 posts
    Member since:
    Aug 2012

    Posted 02 Jan 2014 Link to this post

    Hi,

    I have the following code:
    ...
    using (var lDbContext = new EntitiesModelTW2ArenaPos(SqlUtil.CommandTimeout))
    {
        lDbContext.ExecuteNonQuery(lScriptBancoPos, CommandType.Text);
        lDbContext.SaveChanges();
    }
     
    List<ProdutoEvento> lEventoProdutos = EventoBLL.Instancia.ObterProdutosEvento(aEventoID);
     
    using (System.Net.WebClient lClient = new System.Net.WebClient())
    {
        lClient.Credentials = new System.Net.NetworkCredential(lUserName, lPassword);
        lClient.UploadFile(string.Format("{0}/{1}.DB", lRequestUriString, "EVENTOS"), "STOR", lCaminhoArquivoSqlite);
     
        foreach (ProdutoEvento lEventoProduto in lEventoProdutos)
        {
            lClient.UploadData(string.Format("{0}/{1}.PNG", lRequestUriString, lEventoProduto.ProdutoNome.Replace(" ", string.Empty).Substring(0, lEventoProduto.ProdutoNome.Length > iTamanhoMaximoNomeImagem ? iTamanhoMaximoNomeImagem : lEventoProduto.ProdutoNome.Length)), "STOR", lEventoProduto.Imagem);
        }
    }
    ...

    In which I delete and insert some rows in a SQLite database and after that send this same database to an FTP server, along with some data from other sources. The problem that I'm facing here is that the SQLite database file stays locked even after the end of the first using scope, preventing me from accessing it and thus sending it to the FTP server.

    My question is: is there anything that I could do to unlock this file or even change my approach and access it direct from the FTP server instead of accessing it locally and them sending it there?

    Thanks in advance!
  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 08 Jan 2014 Link to this post

    Hi Liordino,

    Generally, Telerik OpenAccess ORM creates a static instance of the metadata related to the model during the construction of the first context instance. The metadata instance remains cached in-memory during the lifetime of the application, and this seems to be the reason for the behaviour you experience on your side. You can workaround the issue in two ways.

    The first one is to split the update of the database and its upload in two applications and to run the second application after the first one has stopped working. The second approach is to override the Dispose() method of the context, and to dispose the metadata in it. The first option would be the recommended one, but if it is not feasible in your scenario note that, the second approach would bring a performance penalty in the application. The consideration is that a new instance of the metadata will be created with each instance of the context.

    The workflow for the second approach requires you to extend the context type in a partial class and to override the Dispose() method as demonstrated below:
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        Database db = this.GetScope().Database;
        db.Dispose();
    }

    I hope this helps. If you have additional questions or need further assistance, do not hesitate to get back to us.


    Regards,
    Doroteya
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  3. DevCraft banner
  4. Liordino
    Liordino avatar
    9 posts
    Member since:
    Aug 2012

    Posted 08 Jan 2014 Link to this post

    Thanks, Doroteya.

    I tried the second approach, but unfortunatelly the problem persists. Is just overriding the Dispose method enough? I tried to call it explictly but the application gives me a System.ObjectDisposedException

    The context has already been disposed and it's managed persistent objects can no longer be accessed. The context should be disposed at the end of the life cycle of your business logic instance. This can also be done in the Dispose of your ASP page or MVC controller. Object name: 'Telerik.OpenAccess.OpenAccessContextBase'.


    I don't know if it helps, but the file is locked by the process w3wp.exe, which I researched and seems to be something related to IIS (this code is called via a webservice, and the application is a Silverlight one). Am I right? In this case, do you know what could be wrong in my code?

    Thanks again!
  5. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 13 Jan 2014 Link to this post

    Hello Liordino,

    Thank you for your feedback and for the additional information.

    Usually, the error you are experiencing occurs when the application tries to dispose the Database object twice. For example: when the Dispose() method of the context is overridden, an instance of the context is enclosed in a using statement, and then  the Dispose() method is called explicitly. 

    To avoid the error, you could override the Dispose() method of the context but to remove the explicit call. That way, the instance of the context will take care to dispose the Database object on its disposal, at the end of the using statement. 

    Regarding the workflow that would unlock the database file in your case, it seems like the scenario you are implementing has a lot of specifics, and currently, we do not offer a reliable out-of-the-box resource that demonstrates it. However, we are constantly gathering information and preparing resources based on the needs of our users, and I will make sure to update this thread once we are able to provide you with additional details.

    In case you have further questions, do not hesitate to get back to us.


    Regards,
    Doroteya
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top