0

I have a problem with a threaded Client/Server application, I have a serversid that has a Access DB, and with one thread for each client, but I get a problem if both client threads asks to open the DB at the same time. Is there any way to check if the DB is in use (I know I can have a varible and keep controlling/setting that, but would like to avoid that. Here is an example connection

String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + dbPath + "'"; OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand command;

connection.Open();
command = new OleDbCommand("UPDATE Client SET Online = " + online)
command.ExecuteNonQuery();
connection.Close();

Would really like some help!

/Nick

4
  • Can you give more details on the error you get? And some code will be helpful Commented May 29, 2012 at 10:26
  • 1
    the code to open a connection to db is written on server then use locking (Synchronization in threads) to allow only one user to open connection at a time. Commented May 29, 2012 at 10:28
  • Since you are using the OleDbConnection, set the Mode property to be 12 in your connection string - see w3schools.com/ado/prop_rec_mode.asp for a full list of modes. Simply append Mode=12 to your connection string, and the first app to open a connection will do so in exclusive mode - see social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/… for an example, too. Commented May 29, 2012 at 10:32
  • You can use a try-catch block to catch the exception and branch your code to do something such as retry or exit the program. Commented May 28, 2022 at 23:14

1 Answer 1

1

Per this http://www.connectionstrings.com/access-2007 you can set Exclusive=1 in connection string to grant that only one connection can use this database. All another trying will fail.

Sign up to request clarification or add additional context in comments.

1 Comment

That's for the OdbcConnection. You can achieve the same with the OleDbConnection by appending Mode=12 to the connection string. Otherwise, +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.