in the first article of this series, we went through the basics of using berkeley db. in this article, were going to walk through a more extended example of using it. the example im going to use is session management. while this series of examples doesnt illustrate the full power of berkeley db, it will give you a good feel for how to use it. and you might be surprised at how complicated some aspects of using berkeley db are.
there is a global map from session keys to instances of session. persistence of the global map to disk: if the system shuts down and restarts, it should still have the sessions from before the shutdown. old sessions are expired. usually, this happens if the sessions havent been used in a while. simple session management 【程序编程相关:Building the Ultimat】conceptually, session management is very simple. there are three basic requirements: 【推荐阅读:Java 5.0 - Tiger: A 】
create a binding for session (e.g., write implementations of entrytoobject() and objecttoentry()). define where the database will be stored on disk. create a session manager class, sleepycatbasedsessionmanager, which the rest of the application will use. internally, it will manage interactions with berkeley db. as part of sleepycatbasedsessionmanager, this will include a background thread for synchronizing with the disk.weve already talked about sessionbinding. there are, however, two important points that id like to emphasize. the first is that the binding has to be reversible--entrytoobject(objecttoentry(session)) will create a second instance of session. but it should be equivalent to the first session (all of the fields should have the same value). similarly, objecttoentry(entrytoobject(databaseentry)) should yield an equivalent instance of databaseentry. 【扩展信息:QueryParser Rules】
suppose we only wanted to implement the first two requirements: a global map from session keys to instances of session and persistence of the global map to disk. the code for this is pretty simple, and youve already seen most of it. assuming there is already have a class named session, we need to do the following:
... 下一页