Data Persistence in Jaxer | WebReference

Data Persistence in Jaxer

By Rob Gravelle


[next]

Welcome to the last article in a three-part series on Aptana, Jaxer's server-side JavaScript Application Framework. In the Create a File-based Blog using Jaxer article, we extended our Jaxer file-based blog in Aptana Studio by including the ability to add posts and save them to a file on the server. Today, we'll be looking at data persistence in Jaxer as we create a session variable and move to storing the posts in a proper database.

Session Variables

The init() function can be modified to use a session variable for the posts folder. Short term variable persistence can be problematic in web apps due to the stateless nature of web transactions. To overcome this limitation, server languages usually incorporate a mechanism for storing session state. Jaxer manages state information in the Jaxer.Container class. You the developer don't interact through the Container API directly. Rather, you can treat each state as a JavaScript associative array. Jaxer supports the following four session contexts:

Jaxer.sessionPage: Values are stored in the private context of a single page and belong to a specific session, and as such will endure over the entirety of that session, expiring when the user's session expires.

Jaxer.session: A session is automatically created within the context of an application, expiring when the user's session expires. Session containers are not shared among applications so if a user is working with two distinct Jaxer web applications, that user has a separate session container for each application.

Jaxer.page: Values persist within the private context of a single page and shared within the application and among sessions. This is analogous to a static member of a class in Java; the container belongs to the page yet the values are static within the application. This container only expires when the Jaxer server is shutdown.

Jaxer.application: Stores values accessible within a single application. The application container is always available when any user accesses a page belonging to an application. An application container might be used to cache shared, rarely changed data originally loaded from disk or a database; your application code is responsible for managing the cache and updating the session as necessary. This container and its values expire only when the Jaxer server is shutdown.

We will use the page context because all application sessions will share the same folder. To obtain the path to the posts folder, we can use the Jaxer.request.documentRoot property, which is the path to the project's root web documents folder. By default, Jaxer sets the working directory to: <Aptana Studio Installation Directory>/.metadata/.plugins/com.aptana.ide.framework.jaxer/jaxer_studio_data/
local_jaxer/data/DEFAULT
. We only have to create it once, so there is a check for its existence using the NOT bang operator (!) in conjunction with the in operator. It reads: "If NOT key in Page session object then store it":



Working with a Database

Our next task is to replace the text file persistence with a proper database. In the long run, this approach will be much more efficient than reading numerous text files. Jaxer makes it easy to execute queries and retrieve data without thinking about connections, ODBC connection strings, reuse, etc. Moreover, it doesn't take much work to set up new connections or to change the database type. Jaxer ships with the SQLite3 relational database and driver built in, so that you can use it out of the box. All you need to do to access the default database is to execute some SQL against it:

The toHTML() function formats the resultset as an HTML table, without any special formatting. That part is up to you. Here is what the above code produces:

Running Jaxer in this way is called "managed access" because everything is done for you. Under the covers, Jaxer creates a single connection to the database and uses it to execute your queries. The connection is automatically opened when needed, and closed according to the configuration parameters - either after each query, after each page, or when the server shuts down. In addition to the SQLite3 driver, Jaxer ships with one for MySQL 5 as well. You can use these drivers to connect to your own database by editing the config.js file in your local_jaxer folder. It took a little digging, but I located the config.js file in the "C:\Program Files\Aptana\Aptana Studio 1.2\plugins\com.aptana.ide.framework.jaxer.server.win32_1.2.5.023247\jam\jaxer\default_local_jaxer\conf" directory. To configure yours for SQLite3, uncomment the relevant lines and set them to something like the following:


[next]