Postgres database connection pools in Seaside
Part 2. Configurations
Using Rails you have several places to include configuration code for your application, you use database.yml, config.rb, environment.rb, environments/development.rb, environment/production.rb, etc. In those files you can set variables that your application code will use, execute code and basically, anything you need in order to provide the required parameters to your application.
If you use Django you have a settings file for your all your configuration needs.
Seaside is no exception, it provides a very powerful, flexible and versatile mechanism to configure a web application.
A Seaside configuration class provides more than just a set of keyed values, it defines the type of the values and it can provide a list of options that an administrator can change through the administrative interface of Seaside, it also provides some kind of 'inheritance'.
Before getting into database connections with its user, password and other parameters, I will use a configuration class for those parameters. For an introduction about configurations you should read the following pages:
- Configuration and Preferences
- David Shaffer's tutorial: Custom application configuration parameters.
Now back to our business. We will use a configuration class for all the required Glorp parameters:
WASystemConfiguration subclass: #GlorpSessionConfiguration
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LearnGlorpSession'
With the these methods:
ancestors
^ Array with: WASessionConfiguration new
attributes
^ Array
with: (WAStringAttribute key: #sessionClass)
with: (WAStringAttribute key: #glorpServer group:#database)
with: (WAStringAttribute key: #database group:#database)
with: (WAStringAttribute key: #glorpUserName group:#database)
with: (WAStringAttribute key: #glorpPassword group:#database)
glorpDatabase
^'You need to provide a database'
glorpPassword
^''
glorpServer
^'localhost'
glorpUserName
^'postgres'
sessionClass
^MyPostgresSession
sessionClasses
^MyPostgresSession withAllSubclasses
I will assume you don't have a Seaside root component yet so let's write one:
WAComponent subclass: #GlorpExampleComponent
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'GlorpExample'
renderContentOn: html
html text: 'Glorp example'
And with a class method:
canBeRoot
^ true
( You can read more about the root component here )
Then load this url in your browser (using the port you told SSKom to use): http://localhost:9091/seaside/config and enter the user and password ( defaults: admin/seaside ).
Under "Add entry point" define a name for our testing application, for the example I will use: glorpexample. Then select the Root component from the combo 'GlorpExampleComponent'.
And now, pay attention to the options provided in the combo list named 'Ancestry'. Your configuration class is listed there! Select it and click the Add button.
Now save the changes.
What do we have so far?
We have a Seaside application with our configuration class that provides a custom Seaside session that provides a pool of connections to PostgreSQL.
If you load again the configuration page for our glorpexample application you can appreciate a separated section for the database! You can change the field values or revert to the previous ones.
With these, all you need to do now to execute a query using Glorp is: WACurrentSession value execute: query.
I am making several changes. From now on, the url for this blog will be www.brianplugins.com/dump
Installation
I will use Ramón León's Squeak image. It has a customized UI, much better than the default Squeak interface and comes with all we need ( Postgres access, Glorp, Seaside ).
Download and install the corresponding Squeak version for that image (check León's page).
Launch the Squeak image.
If you are on OSX change the port change the port SSKom port to some number greater than 1024, like: SSKom startOn: 9091 and execute ('Do it') that line.
Now you have Seaside running from that image and we can begin writing some code.

As a complete Smalltalk newbie one of my main concerns about writing a web application for the company I work for using Seaside was how to use database connections in a correct and scalable way.
After writing a couple of configuration pages in Seaside for a current internal application written in Rails a few months ago, I was ready to write something 'real'. Worried about how to get that done, I avoided the risks and kept working at other stuff.
Well, now I was getting again into the mood to trying it out and found a blog post about using Glorp and Seaside. The author, Ramon León, directed me to his MagritteGlorp package that provides connection pooling and integration with the Seaside session management code.
Well, after digging into it for a few hours I got it working and will be posting soon a basic guide/example of how to use it.
