Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9310819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:12:37+00:00 2026-06-19T01:12:37+00:00

I have a database that may be on the network drive. There are two

  • 0

I have a database that may be on the network drive.
There are two things that I want to achieve:

  1. When the first user connects to it in read-only mode (he doesn’t
    have a read-write access to the location, or the database is
    read-only), other users must use the read-only connection also (even
    if they have RW access).
  2. When the first user connects to it in RW mode, others can not
    connect to the database at all.

I’m using SQLite, and the concurrency should not be the problem, as the database should never be used by more than 10 people at the same time.

UPDATE: This is a sample that I’m trying to make work, so I could implement it in the program itself. Almost everything can be changed.

UPDATE: Now when I finally understood what @CL. was telling me, I made it work and this is the updated code.

using System.Diagnostics;
using System.Linq;
using System.IO;
using DbSample.Domain;
using DbSample.Infrastructure;
using NHibernate.Linq;
using NHibernate.Util;


namespace DbSample.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            IDatabaseContext databaseContext = null;

            databaseContext = new SqliteDatabaseContext(args[1]);

        var connection = LockDB(args[1]);
        if (connection == null) return;

        var sessionFactory = databaseContext.CreateSessionFactory();
        if (sessionFactory != null)
        {

            int insertCount = 0;

            while (true) 
            {

                try
                {

                    using (var session = sessionFactory.OpenSession(connection))
                    {
                        string result;
                        session.FlushMode = NHibernate.FlushMode.Never;

                        var command = session.Connection.CreateCommand();

                        command.CommandText = "PRAGMA locking_mode=EXCLUSIVE";
                        command.ExecuteNonQuery();


                        using (var transaction = session.BeginTransaction(ReadCommited))
                        {
                            bool update = false;
                            bool delete = false;
                            bool read = false;
                            bool readall = false;
                            int op = 0;
                            System.Console.Write("\nMenu of the day:\n1: update\n2: delete\n3: read\n4: read all\n0: EXIT\n\nYour choice: ");
                            op = System.Convert.ToInt32(System.Console.ReadLine());
                            if (op == 1)
                                update = true;
                            else if (op == 2)
                                delete = true;
                            else if (op == 3)
                                read = true;
                            else if (op == 4)
                                readall = true;
                            else if (op == 0)
                                break;
                            else System.Console.WriteLine("Are you retarded? Can't you read?");




                            if (delete)
                            {
                                System.Console.Write("Enter the ID of the object to delete: ");
                                var objectToRemove = session.Get<MyObject>(System.Convert.ToInt32(System.Console.ReadLine()));

                                if (!(objectToRemove == null))
                                {
                                    session.Delete(objectToRemove);
                                    System.Console.WriteLine("Deleted {0}, ID: {1}", objectToRemove.MyName, objectToRemove.Id);
                                    deleteCount++;
                                }
                                else
                                    System.Console.WriteLine("\nObject not present in the database!\n");


                            }

                            else if (update)
                            {
                                System.Console.Write("How many objects to add/update? ");
                                int number = System.Convert.ToInt32(System.Console.ReadLine());
                                number += insertCount;
                                for (; insertCount < number; insertCount++)
                                {

                                    var myObject = session.Get<MyObject>(insertCount + 1);

                                    if (myObject == null)
                                    {
                                        myObject = new MyObject
                                            {
                                                MtName = "Object" + insertCount,
                                                IdLegacy = 0,
                                                                                           };
                                        session.Save(myObject);
                                        System.Console.WriteLine("Added {0}, ID: {1}", myObject.MyName, myObject.Id);
                                    }
                                    else
                                    {
                                        session.Update(myObject);
                                        System.Console.WriteLine("Updated {0}, ID: {1}", myObject.MyName, myObject.Id);
                                    }
                                }

                            }

                            else if (read)
                            {

                                System.Console.Write("Enter the ID of the object to read: ");
                                var objectToRead = session.Get<MyObject>(System.Convert.ToInt32(System.Console.ReadLine()));
                                if (!(objectToRead == null))
                                    System.Console.WriteLine("Got {0}, ID: {1}", objectToRead.MyName, objectToRead.Id);
                                else
                                    System.Console.WriteLine("\nObject not present in the database!\n");

                            }

                            else if (readall)
                            {

                                System.Console.Write("How many objects to read? ");
                                int number = System.Convert.ToInt32(System.Console.ReadLine());
                                for (int i = 0; i < number; i++)
                                {
                                    var objectToRead = session.Get<MyObject>(i + 1);
                                    if (!(objectToRead == null))
                                        System.Console.WriteLine("Got {0}, ID: {1}", objectToRead.MyName, objectToRead.Id);
                                    else
                                        System.Console.WriteLine("\nObject not present in the database! ID: {0}\n", i + 1);


                                }

                            }
                            update = false;
                            delete = false;
                            read = false;
                            readall = false;

                            transaction.Commit();
                        }
                    }    
                }
                catch (System.Exception e)
                {
                    throw e;
                }


            }
            sessionFactory.Close();
        }

    }

    private static SQLiteConnection LockDbNew(string database)
    {
        var fi = new FileInfo(database);
        if (!fi.Exists)
            return null;
        var builder = new SQLiteConnectionStringBuilder { DefaultTimeout = 1, DataSource = fi.FullName, Version = 3 };

        var connectionStr = builder.ToString();
        var connection = new SQLiteConnection(connectionStr) { DefaultTimeout = 1 };
        var cmd = new SQLiteCommand(connection);

        connection.Open();

        // try to get an exclusive lock on the database
        try
        {
            cmd.CommandText = "PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE; COMMIT;";
            cmd.ExecuteNonQuery();
        }
        // if we can't get the exclusive lock, it could mean 3 things
        // 1: someone else has locked the database
        // 2: we don't have a write acces to the database location
        // 3: database itself is a read-only file
        // So, we try to connect as read-only
        catch (Exception)
        {
            // we try to set the SHARED lock
            try
            {
                // first we clear the locks
                cmd.CommandText = "PRAGMA locking_mode = NORMAL";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "SELECT COUNT(*) FROM MyObject";
                cmd.ExecuteNonQuery();

                // then set the SHARED lock on the database
                cmd.CommandText = "PRAGMA locking_mode = EXCLUSIVE";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "SELECT COUNT(*) FROM MyObject";
                cmd.ExecuteNonQuery();

                readOnly = true;
            }
            catch (Exception)
            {
                // if we can't set EXCLUSIVE nor SHARED lock, someone else has opened the DB in read-write mode and we can't connect at all
                connection.Close();
                return null;
            }

        } 
        return connection;
    }

 }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:12:39+00:00Added an answer on June 19, 2026 at 1:12 am

    Set PRAGMA locking_mode=EXCLUSIVE to prevent SQLite from releasing its locks after a transaction ends.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that which contains two tables, links and tour. Tour is
I have a website that connects to SQLServer Express database files for Membership and
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1
I have a database that has around 10k records and some of them contain
I have a database that had 15 years of cruft stuffed into it by
I have a database that has a table email_eml that stores 3 attributes name_eml,
I have a database that has 16 tables, but only four are relevant to
I have a database that has the following columns: ------------------- id|domain|hit_count ------------------- And I
I have a Database that contains data about articles , structures and manufacturers .
I have a database that I have created using SQL Server Developer 2008. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.