Step 4: Configuring the Torque Runtime

Before we can start to write a Torque application, we have to configure the runtime environment of Torque. The Torque runtime needs a configuration file in order to retrieve the data which is necessary to connect to the database.

Torque Runtime Properties

The second step in the configuration of the Torque Runtime are the Torque runtime properties. As the name suggests, these properties are used when your application is executing the object model code generated by Torque. The runtime properties control database parameters such as drivers, usernames, and passwords.

We will save our runtime properties in the a file called torque.properties. Create a subdirectory src/main/resources in the top-level directory of your project, and create a new file called torque.properties in it. Add the following lines to this file:

torque.database.default = bookstore
torque.database.bookstore.adapter = mysql

torque.dsfactory.bookstore.factory = org.apache.torque.dsfactory.SharedPool2DataSourceFactory
torque.dsfactory.bookstore.connection.driver = org.gjt.mm.mysql.Driver
torque.dsfactory.bookstore.connection.url = jdbc:mysql://localhost:3306/bookstore
torque.dsfactory.bookstore.connection.user = root
torque.dsfactory.bookstore.connection.password = password
  

Change the adapter, driver, url, user and password parameters to match the parameters for your database. In the following table, the parameters used in the sample configuration are described. For further information, see the Runtime Configuration Reference.

Property Description
torque.database.default Torque has the ability to use multiple databases. This property specifies which database is to be used as the default.
torque.database.XXX.adapter Torque has the ability to deal with multiple database systems. This property specifies the database adapter to use.
torque.dsfactory.XXX.factory The factory class that will be used to provide database connections.
torque.dsfactory.XXX.connection.driver The JDBC database driver to use when connecting to your database.
torque.database.XXX.connection.url The URL that will be used to access your database. Torque's generated object model will perform all database operations using this URL. This value should reflect the database name specified in your database schema file (see the database element's name attribute).
torque.database.XXX.connection.user The username that has sufficient privileges to access your database. This user does not require privileges to create and drop tables, unlike the user that was specified in project.properties.
torque.database.XXX.connection.password The password for the specified username.

It is worth re-iterating that these runtime properties are not used by Torque when generating your object model and creating your database. They are used only by the application utilizing the Torque-generated object model classes at run-time.

Logging configuration

Torque uses in generated java sources commons-logging as a logging interface. To enable logging in your application, read the commons-logging user guide.

Internally Torque uses now Log4j2. To enable logging in your application, read the configuration guide or Manual.

This example uses log4j as logging system and it is configured very simply so that only warnings and errors are printed. In a serious application, you want to improve the logging configuration (using Log4j-1.2 API Adapter).

Where to next

Now you have finished configuring the Torque runtime. You are now ready to use the generated classes to access the database.

Next we will look Writing a Sample Application.

User Comments

User comments for this step