The Apache Project

How to Access a Database

Accessing a Database

Most developers would consider accessing a database part of the "business end" of an application. Most often, we don't access a databse for the sake of accessing a database. We use the database as part of a larger business transaction. So lets start with accessing business logic from Struts.

The best thing is use the Action as a thin adaptor between the web/presentation-tier and your business classes (including those that access a database).

So you first design a business API that uses plain Java classes. The best thing is to use objects that take ordinary Java types and return a JavaBean or collection of JavaBeans. The Action then calls these objects and passes the result back to the web/presentation tier.

A common approach is to create an Action class for each of business transaction, or use case, in your application. A simple "CRUD" application might have a CreateAction, a RetrieveAction, an UpdateAction, and a DeleteAction. To complete each transaction, the Action can make whatever calls are needed to your business API classes.

Ideally, all the database access code should be encapsulated behind the business API classes, so Struts doesn't know what persistent layer you are using (or even if there is a persistence layer). It just passes a key or search String and gets back a bean or collection of beans. This lets you use the same business API classes in other environments, and also to run unit tests against your business API outside of Struts or a HTTP environment.

The MailReader example application bundled with Struts demonstrates how this is usually done. The MailReader uses the DAO (Data Access Object) pattern to separate the persistence layer from the (Struts) control layer. MailReader defines a DAO interface that the Actions can call, it then defines a implementation that uses a database stored in main memory. Other implementations could be defined and used instead, without changing any of the Struts Action classes.

To get started, it's simplest to setup a 1:1 correspondence between the Actions and your application's use cases. Each use case may make one or more calls to your business API, but from the user's perspective, each use case is a single transaction.

As you gain experience, you will find ways to combine your Action classes, say by using the DispatchAction. It's even possible to use a single "framework" Action to call all of your business classes, as is done with Scaffold ProcessAction in the contrib folder.

Using fewer Actions does require a deeper understanding of how Struts and MVC frameworks operate. Don't hesitate to err on the side of creating more Action classes at first. The Struts configuration makes it easy to refactor your Actions later, since you can change the Action type without changing anything else in the application.

Using DataSources

When you use the DAO approach, all of the database access details are hidden behind the business interface. The implementation of the business classes handle all the gritty details, like using a DataSource to pool connections to the database.

As a rule, you should always use a connection pool to access a database. The DataSource interface is the preferred way to implement a connection pool today. Many containers and database systems now bundle a DataSource implmentation that you can use. Most often, the DataSource is made available through JNDI. The JNDI approach makes it easy for your business classes to access the DataSource without worrying about who set it up.

Persistence Franeworks

There are many useful and mature persistence layer frameworks available. Before using raw JDBC or "rolling your own" solution, you should carefully review one or more of these packages. Here's a short list of packages most often mentioned on the Struts User list:

For more, see the Struts Community Resources area on SourceForge.

The Struts DataSource Manager

Ideally, the business logic layer should encapsulate the data access details, including acquiring a database connection. However, some older application designs expect that the caller be able to provide a database connection or DataSource instance. When you need to access a legacy design, the Struts DataSource manager can make it easy for your Action class to produce these resources on demand.

NOTE: It is preferred that data connectivity be handled directly by the business classes, usually via JNDI. The Struts DataSource manager should only be used with legacy business classes that don't provide their own connectivity. When possible, we strongly recommend use of the standard DAO pattern, so that the Action classes do not need to know anything about the persitence mechanism. The DataSource manager is being retained in Struts 1.x for backward compatibility but may not be retained in Struts 2.x or later.

The Struts DataSource manager is configured as an element in the Struts configuration file (struts-config.xml). The manager can be used to deploy any connection pool that implements the javax.sql.DataSource interface and is configurable totally from JavaBean properties. If your DBMS or container provides a connection pool that meets these requirements, then that component might be your first choice.

The Jakarta Commons dbcp's BasicDataSource [org.apache.commons.dbcp.BasicDataSource] also works well with the DataSource manager, if a native component is not available. DBCP is not supplied as part of the Struts distribution. You can obtain it from Commons DBCP.

Note: The Generic DataSource which was included with previous versions of Struts has been removed as of release 1.2.0 in favor of plugging in the BasicDataSource, or another DataSource implementation.

This is how you would specify a DBCP BasicDataSource for your application:

<!-- configuration for commons BasicDataSource -->
<data-source type="org.apache.commons.dbcp.BasicDataSource">
      value="org.postgresql.Driver" />
      value="jdbc:postgresql://localhost/mydatabase" />
      value="me" />
      value="test" />
      value="10" />
      value="5000" />
      value="false" />
      value="false" />
      value="SELECT COUNT(*) FROM market" />

Note that you can define as many datasource objects as your application requires and refer to each using a logical name. This can be useful in providing better security or scalability, or even to test datasource implementations against each other.

After a DataSource is defined, here is an example of using the manager to establish a connection from within an Action's execute method.

public ActionForward
       execute(ActionMapping mapping,
               ActionForm form,
               HttpServletRequest request,
               HttpServletResponse response) throws Exception
 javax.sql.DataSource dataSource;
 java.sql.Connection myConnection;
 try {
  dataSource = getDataSource(request);
  myConnection = dataSource.getConnection();
  // do what you wish with myConnection
 } catch (SQLException sqle) {
    getServlet().log("Connection.process", sqle);
 } finally {
    //enclose this in a finally block to make
    //sure the connection is closed
    try {
    } catch (SQLException e) {
       getServlet().log("Connection.close", e);

Note: If you use the Commons BasicDataSource with Struts, the query you provide for the pingQuery attribute (if you choose to include it) must return at least one row.


Just be sure you to replace "VALIDTABLE" with the name of a valid table in your database.

Using Multiple Datasources

If you need more than one data source in a module, you can include a key attribute in the data-source element:

   <data-source key="A" type="org.apache.commons.dbcp.BasicDataSource">
      ... properties as before ...
   <data-source key="B" type="org.apache.commons.dbcp.BasicDataSource">
      ... properties as before ...

Which can then be accessed by including the key ("A" in this case) as an additional parameter to the Action.getDataSource() method.

   try {
      dataSourceA = getDataSource(request, "A");
      dataSourceB = getDataSource(request, "B");

Each module can have as many data sources as it needs. The keys only need to be unique within a module since the struts module system maintains a name space for the items in each module to protect you from name clashes.

See Also

Transformation/Data Transfer

Rendering a dynamic result set

The result of most queries will map to the ActionForms you are already using, and so you can render the ResultSet as a collection of ActionForms. But sometimes there are columns in a ResultSet that are not properties of an ActionForm, or even known in advance.

Happily, the Struts tags don't care what type of bean you use with them. You could even output a ResultSet directly. But a ResultSet retains a connection to the database, and passing "all that" directly to a JSP gets messy. So what's a developer to do?

Since Struts 1.1, the simplest option is to use a ResultSetDynaClass to transfer the ResultSet into an ArrayList of DynaBeans. The Struts custom tags can use DynaBean properties as easily as they use conventional JavaBean properties. (See DynaActionForm classes in the Struts User Guide for details.)

Since this is in the BeanUtils jar, you already have it on board, and just need to implement the transfer routine (see the ResultSetDynaClass link).