Step 4: Configuring the Torque Runtime

Before we can start to write a Torque application, we have to configure the runtime environment:

  • We have to make sure that the generated object model classes have access to the Torque runtime and associated libraries.
  • The Torque runtime needs a configuration file in order to retrieve the data which is necessary to connect to the database.
These two steps will be covered in the following sections.

Setting up the classpath

The libraries which the generated classes depend on are specified in the file project.xml in the top level directory of the project. You already created this file in step 3 of the tutorial; now it needs to be extended a bit. Assuming that you use JDK 1.4+, you need to add the following entries to the <dependencies> section of your project.xml

    <dependency>
      <artifactId>torque</artifactId>
      <groupId>torque</groupId>
      <version>3.3</version>
    </dependency>

    <dependency>
      <groupId>avalon-framework</groupId>
      <artifactId>avalon-framework-api</artifactId>
      <version>4.3</version>
      <url>http://avalon.apache.org/</url>
    </dependency>

    <dependency>
        <groupId>avalon-logkit</groupId>
        <artifactId>avalon-logkit</artifactId>
        <version>2.1</version>
        <type>jar</type>
    </dependency>

    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils-core</artifactId>
      <version>1.7.0</version>
      <url>http://commons.apache.org/beanutils/</url>
    </dependency>

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2</version>
      <url>http://commons.apache.org/collections/</url>
    </dependency>

    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.4</version>
      <url>http://commons.apache.org/configuration/</url>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
      <url>http://commons.apache.org/dbcp/</url>
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.3</version>
      <url>http://commons.apache.org/lang/</url>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
      <url>http://commons.apache.org/logging/</url>
    </dependency>

    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.3</version>
      <url>http://commons.apache.org/pool/</url>
    </dependency>

    <dependency>
      <groupId>jcs</groupId>
      <artifactId>jcs</artifactId>
      <version>1.3</version>
      <url>http://jakarta.apache.org/jcs/</url>
    </dependency>

    <dependency>
      <artifactId>village</artifactId>
      <groupId>torque</groupId>
      <version>3.3</version>
    </dependency>

    <dependency>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
      <version>2.6.2</version>
      <url>http://xerces.apache.org/xerces2-j/</url>
    </dependency>

    <dependency>
      <groupId>xml-apis</groupId>
      <artifactId>xml-apis</artifactId>
      <version>2.0.2</version>
      <url>http://xml.apache.org/commons/</url>
    </dependency>

You don't need to download any of these libraries - Maven will download them automatically when you build your project.

Note: There is no need to include the torque-gen jar file in your project classpath, including it may adversly affect the logging configuration of your application.

Torque Runtime Properties

The second step in the configuration of the Torque Runtime are the Torque run-time properties. As the name suggests, these properties are used when your application is executing the object model code generated by Torque. The run-time properties control database parameters such as drivers, usernames, and passwords. These properties can be saved in any file because your application must explicitly initialize Torque (as you'll see later in this tutorial).

We will save our runtime properties in the a file called torque.properties. Create a subdirectory src/conf 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

#Using commons-dbcp
torque.dsfactory.bookstore.factory = org.apache.torque.dsfactory.SharedPoolDataSourceFactory
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 = user
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.username The username that has sufficient privileges to access your database. This user does not require privileges to create and drop tables, unlike the username that was specified in project.properties.
torque.database.XXX.connection.password The password for the specified username.

It is worth re-iterating that these run-time 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 commons-logging as a logging interface. To enable logging in your application, read the commons-logging user guide.

If you have no trouble running the tutorial, you do not need to configure logging now. If, however, you experience runtime errors later on and do not find the reason immediately, it might be a good idea to configure logging and look at the log messages. Also, for a "serious" application, logging is indispensible in order to track down any unexpected errors.

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