How do I use Esent

Sep 18, 2012 at 6:07 AM

I tried following the examples, using a Using statement to open an instance, create a session, attach to the database, open it, then perform some operations like opening a transaction and saving a record. But when I use this pattern for each operation. Each operation like a Save take around  .5 second. So I think I am doing this wrong. but I am not sure how I should be doing this.

should I only have one Instance? can I have more if they are named differently?

can I / should I have more if I have more than one thread?

I see I can have multiple sessions open at the same time, I think that is OK but does every session need to be attached? should the database be opened for each session? I am getting inconsistent results when I test these various scenarios, Sometimes my tests pass sometimes I get exceptions , like trying to use a disposed object, or EsentTempPathInUseException.

So is there any guidance on how I should use this?

Developer
Sep 18, 2012 at 6:21 PM

The Instance is a very heavyweight object, so you should only use one of them. Creating an Instance involves reading the log files on the disk and replaying any outstanding operations.

Yes, you can have multiple sessions open at the same time. They are relatively cheap, so it is fine to have dozens of them (servers may have hundreds or even thousands of open sessions*.

Sessions can NOT be used on different threads. If you open a Session on a thread, you can only use it with that thread**. Doing operations on a second thread requires a second Session. Keep in mind that using multiple sessions has many advantages, it does have a few downsides. Specifically you can't modify the same column in two different sessions. Otherwise you'll get a 'Write-Conflict' because the ESE engine won't know which one should win.***

You only need to JetAttachDatabase a database to an instance once. Every session needs to call JetOpenDatabase() in order to use it. JetOpenDatabase() returns the JET_DBID that other functions use.

Hope that helps. Any other questions?

And this post has had way too many footnotes. :)

-martin

 *If you do go up to that scale you'd probably want to re-use them, but most people can safely just open and close a new Session each time).

**There is a way around it for more advanced users. JetSetSessionContext/JetResetSessionContext.

***The exception to that is escrow-update columns.

Sep 18, 2012 at 6:58 PM

Thanks for your fast response, I really appreciate it.

From your answer: "Specifically you can't modify the same column in two different sessions. Otherwise you'll get a 'Write-Conflict' because the ESE engine won't know which one should win"

What does modify the column mean? Does this include inserting new records? Can one session lock one record and update it, while another session inserts or modifies a different record?

If we think about using it from a basic ASP.NET app, I could/should create a single instance and store it in the application do I have to concern myself with the overlap of the start up and close down of the application in ASP.NET? Can I just use a different instance name to avoid any issues?

Thanks again for your help.

Developer
Sep 18, 2012 at 7:13 PM

It means that if you have a table with:

{ "person1", 15000 }
{ "person2", 9000 }
{ "person3", 94000 }

Then two different sessions can modify two different records with no problems. They can both add new records (as long as they don't conflict on the primary key).

But if the two different sessions both tried to change "person1"'s salary to something else, then the second one will fail with a Write Conflict. Make sense?

Using a different instance will mean using a completely different database file. It's not what you want. :)

I'm not sure about the lifetime of ASP.NET, so I can't offer any good guidance. Sorry!

 

-martin