Hi, I'm happily serving requests for the same webapp (so single servlet) which uses multiple JE environments. In my case I have an environment for each db, one of these environments is a login details only db. Things work great, three questions though:
1. Right now I'm keeping environment/entity store references in global vars (everything below entity store, views etc. is created and discarded for each request), is there a better way?
2. When I want to add a new app (so additional servlet) - how can I make sure the login environment is shared across servlets, to take advantage of shared cache etc?
3. As indexes are thread safe, is it considered best practice to store index refs. in global vars. and have each incoming thread (in a servlet) share these vars. or should each request build the object refs. it uses and discard accordingly?
Hoping the above makes sense.
I think the best solution for all your requirements would be to have a servlet context listener intiialize everything and set the respective attributes in application scope. Each servlet could get their own refs and have access to the shared env cache.
Have a look at this article here, which discusses something similar.
As for the EntityIndex'es, yes they are thread-safe and you can use the same approach as for the Environment, EntityStore etc refs (get the respective index ref, as an attribute, from the ServletContext).
If this does not work for you let me know and I can look for other solutions.
Hi Andrei, thanks for the link, this approach is perfect for my requirements.
With regard to sharing index refs. across threads etc. still a bit conflicted as to what the best practice is ie. are there good reasons not to do it? Time taken to build the refs. on each request seems negligible, I'd be interested to know what others do and perhaps why. Also are there a group of 'best practise patterns' for berkelydb je development?
Thanks again, much appreciated.
For simplicity it is best to have a single Environment instance and a single EntityStore (or Database) instance per store (or database). There is little or no performance impact of sharing these objects among threads.
EntityCursors (or Cursors), on the other hand, should always be per-thread and closed as soon as possible.
There is a section at the end of the Getting Started Guide about multi-threading and additional information in the FAQ.
Hi Mark, to confirm, it's fine to treat index object refs. in the same way as environments and stores - meaning share between threads - but everything below (created from) indexes, should be created/destroyed per request, agreed?
Cheers for you input.
Yes, EntityIndex objects are also thread-safe. Really only EntityCursor objects are not.
The entity objects themselves are not owned by JE. You will have problems if you have multiple threads changing them concurrently (if one thread is changing an entity object's properties and another thread is storing it, for example). However, it is up to you if you want to share entity objects between threads; there is nothing wrong with that, since there is no interaction with JE. But your app will need to enforce thread safety, perhaps by using them only in a read-only fashion, for example.