Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Improve persistence with Apache Derby and iBATIS, Part 3: Transactions, caching, and dynamic SQL

Daniel Wintschel is a regular guy who derives great excitement from solving business problems, streamlining processes, and writing all sorts of Java code. He loves coffee and is currently seeking development opportunities on a telecommuting basis from Singapore. You can contact the author at daniel@humandoing.net.

Summary:  This tutorial series has been demonstrating how you can improve persistence in your database-driven Java™ applications by combining Apache Derby's power as a small-footprint embeddable database with the iBATIS object-relational (OR) mapping framework. In Part 3, the final in the series, learn how iBATIS handles three advanced features of database-driven applications: transactions, caching, and dynamic SQL. Plus, find out how the Data Access Objects (DAO) framework can operate on its own without the Data Mapper framework.

View more content in this series

Date:  07 Mar 2006
Level:  Intermediate PDF:  A4 and Letter (362 KB | 22 pages)Get Adobe® Reader®

Activity:  10759 views
Comments:  

Before you start

About this series

This tutorial is the third in a three-part series that introduces you to the iBATIS Data Mapper and DAO frameworks and shows you how to use iBATIS with Apache Derby.

Part 1 discusses the concepts underlying iBATIS, including descriptions of the Data Mapper and DAO frameworks, and outlines the semantics and terminology that iBATIS uses. You look at some basic examples of how to configure Apache Derby with iBATIS and perform simple OR mapping using Data Mapper.

Part 2 covers the DAO and the Data Mapper frameworks in more depth, using the JPetStore example application to illustrate important iBATIS concepts. Using the DAO with the Data Mapper provides you with a clean and consistent way to access your application's underlying data structure.


About this tutorial

Part 3 continues the use of the two frameworks with JPetStore and covers transaction handling, data caching, and creating dynamic SQL statements. It also takes a brief look at, and provides an example of, how to use the DAO framework without the Data Mapper framework to integrate it as a stand-alone framework within an application.

At this point you should have a good idea of how the DAO and Data Mapper frameworks work within iBATIS and how iBATIS avoids the N+1 select problem. In this tutorial you look at how iBATIS can help you with three advanced features that are important when you use any database with a Java application:

  • Transaction handling: Transactions are essential for data integrity and are available to both the DAO and Data Mapper frameworks. In this tutorial's example, the DAO framework delegates transaction management to the underlying Data Mapper framework.
  • Data caching: Data caching enables large database applications to scale under heavy load. This feature pertains to the iBATIS Data Mapper framework only, not to the DOA. (The DAO provides access to underlying data and isn't responsible for where the data comes from or how it works.)
  • Creating dynamic SQL statements: Dynamic SQL lets you generate SQL statements based on certain criteria without needing to write complex and extensive Java code. Like data caching, this feature pertains only to the Data Mapper framework.

You use Apache Ant to execute most of the examples from the command line, which provides a clear view of what's happening in the database. You also continue to use the modified version of the JPetStore application from Part 2 to build up any additional examples that are needed.

Finally, you learn how to use the DAO framework separately from the Data Mapper framework. The ability to use the two frameworks together or individually illustrates the power and flexibility of iBATIS.


Prerequisites

This tutorial assumes you're comfortable working with basic SQL statements, that you understand basic XML semantics, and that you can read Java code. You need to know how to run Apache Ant to follow the tutorial's examples and create the database.


System requirements

To run the example code in this tutorial, perform the following steps:

  1. Download and install the following applications: You don't need to download iBATIS separately, because the necessary .jar files are included with the source code for this tutorial, which you download in step 3.
  1. Make sure that the environment variables outlined in Table 1 are defined in your shell.
    Table 1. Setting the environmental variables
    Variable nameRequired setting
    DERBY_HOME Set to the root folder of your Derby installation.
    ANT_HOME Set to the root folder of your Ant installation.
    JAVA_HOME Set to the root folder of your Java installation.
    PATH Ensure that ANT_HOME/bin is in your path.
  2. Extract the supplied .zip file (see the Download section) to your preferred location. (This is the project root.) The project is laid out in Listing 1:

    Listing 1. Project layout
                                
    /iBATIS_JPetStore-4.0.5/
         /build/      Ant build.xml and build output
         /ddl/        DDL to create our database and data load SQL
         /devlib/     JARs needed for app compilation
         /doc/        licensing and stuff
         /lib/        JARs needed for application execution
         /src/        Java source code and iBATIS config files
         /web/        JSP, Struts, and webapp stuff
    

  3. Modify the /src/properties/database.properties file to specify where you would like Derby to create your database. I've set the url property in the database.properties file to be jdbc:derby:c:/temp/ibatis. See the database.properties file, shown in Listing 2, and change the path to include something appropriate for your file system.

    Listing 2. The database.properties file
                                
    ####################################
    # Database Connectivity Properties
    ####################################
    
    driver=org.apache.derby.jdbc.EmbeddedDriver
    url=jdbc:derby:c:/temp/ibatis
    username=
    password=
    

Make sure that the value you specify is an existing empty directory. You can leave the username and password blank, as they are. If you're on a UNIX® or Linux® system, your path looks something like /tmp/ibatis, but you can create this directory wherever you want.

1 of 9 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Information Management, Java technology
ArticleID=104998
TutorialTitle=Improve persistence with Apache Derby and iBATIS, Part 3: Transactions, caching, and dynamic SQL
publish-date=03072006
author1-email=daniel@humandoing.net
author1-email-cc=troy@backstopmedia.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.