January 27, 2007 on 10:27 am | In Java |
JavaSercer Pages 17.3.3 The Action The final database action in the example tag library is the action. A database transaction consists of the execution of a number of SQL statements on the same Connection; they either all succeed or all fail. As you may recall, the data changes resulting from executing all statements are then either permanently saved by committing the transaction, or ignored by rolling back the transaction. The task for the action is to provide a Connection to all database actions enclosed in its body, and commit the transaction when they have all been executed. The nested database actions must cooperate with the action tag handler by retrieving the shared Connection and rolling back the transaction if they fail. Let’s look at how the Connection is handled first. Example 17.22 shows the TransactionTag class declaration, the dataSource property setter method, and the doStartTag( ) method. Example 17.22. Part of the TransactionTag Class package com.ora.jsp.tags.sql; import java.io.*; import java.util.*; import java.sql.*; import javax.sql.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class TransactionTag extends TagSupport { private String dataSourceName; private Connection conn; public void setDataSource(String dataSourceName) { this.dataSourceName = dataSourceName; } public int doStartTag( ) throws JspException { conn = getTransactionConnection( ); return EVAL_BODY_INCLUDE; } The TransactionTag class extends the TagSupport class. It doesn’t need to access its body content; it only needs to tell the JSP container to execute all actions and possible scripting elements in the body, so the TagSupport class is the proper choice. The dataSource property corresponds to the attribute with the same name. The page author sets it to the name of the DataSource object in the application scope to use for all nested database actions. The doStartTag( ) method, invoked by the container before the actions in the body are processed, sets the conn instance variable by calling the get-TransactionConnection( ) method, shown in Example 17.23. Example 17.23. The TransactionTag’s getTransactionConnection( ) and getConnection( ) Methods private Connection getTransactionConnection( ) throws JspException { DataSource dataSource = (DataSource) pageContext.getAttribute(dataSourceName, PageContext.APPLICATION_SCOPE); if (dataSource == null) { throw new JspException(”dataSource ” + dataSourceName + ” not found”); } try { conn = dataSource.getConnection( ); conn.setAutoCommit(false); } catch (SQLException e) { throw new JspException(”SQL error: ” + e.getMessage( )); } return conn; } Connection getConnection( ) { return conn; } This method retrieves the DataSource object from the application scope and gets a Connection. A Connection automatically commits each SQL statement by default. In order to use the Connection to execute more than one statement within the same transaction, the setAutoCommit( ) method is called with the value false. page 254
Hint: This post is supported by Gama php5 hosting services
Sorry, the comment form is closed at this time.