This project is read-only.

Best patterns and practices

Aug 20, 2013 at 10:58 PM
Edited Aug 20, 2013 at 11:00 PM
Hello, I'm going to try NDatabase on an ASP.NET MVC application and I would like to know which are the best practices about opening and closing database.
Since it is a multithreaded environment and there could be several threads reading/writing to de database file I wonder if there's some pattern to left the database open for a period of time or I should open the file for every request.
Every example I see in the documentation uses a "using" clause that opens the file an then disposes the resources but I think this could be quite time consuming to do in every action of every controller...
The other alternative is to open the database once on application start and use the same odb variable for all threads, but then I don't know when to close the database or when to flush to disk, and what's worse the application could be terminated by IIS (if one client ends its session and there are no more connections in a period of time) and then the database file could be corrupted.

Please, could you provide some guidance about this?

Thanks in advance and best regards
Aug 22, 2013 at 7:21 AM
Every example I see in the documentation uses a "using" clause that opens the file an then disposes the resources -> that's the only recommended way ;)
Nov 6, 2013 at 1:26 AM
But when a database has opened ,and are dealing with the data,there is another thread need to open the database ,It will need to wait for the database file closed,and then it may cause deadlock,there is any way to solve this situation?
Nov 6, 2013 at 6:27 AM
NDatabase has serialization transaction level, so it has all advantages and disadvantages of that, but agree if from some reason file will be locked by bad working sample, then your app will throw exception that it cannot access db (it tries several times). There is no way there could be a deadlock or I cannot see it ;)

Regards,
Jacek