Ssh hosting - JavaSercer Pages The package scope getConnection( ) method,

JavaSercer Pages The package scope getConnection( ) method, also shown in Example 17.23, is used by the tag handlers for the and actions. To see how it’s used, let’s look at the method with the same name in the DBTag class that we skipped earlier. The DBTag ’s getConnection( ) method is shown in Example 17.24. Example 17.24. The DBTag’s getConnection( ) Method private Connection getConnection( ) throws JspException { Connection conn = null; TransactionTag transactionTag = (TransactionTag) findAncestorWithClass(this, TransactionTag.class); if (transactionTag != null) { conn = transactionTag.getConnection( ); isPartOfTransaction = true; if (dataSourceName != null) { throw new JspException(”A dataSource must not be ” + “specified when the action is part of a ” + “transaction”); } } else { DataSource dataSource = (DataSource) pageContext.getAttribute(dataSourceName, PageContext.APPLICATION_SCOPE); if (dataSource == null) { throw new JspException(”dataSource ” + dataSourceName + ” not found”); } try { conn = dataSource.getConnection( ); } catch (SQLException e) { throw new JspException(”SQL error: ” + e.getMessage( )); } } return conn; } The findAncestorWithClass( ) is used to find out whether or not the action is nested in the body of an tag. If a TransactionTag is found, the action is part of a transaction, so the TransactionTag’s get-Connection( ) is used to retrieve the shared Connection, and a boolean flag is set to remember that the action is part of a transaction. If a parent tag handler is not found, the action is not part of a transaction. In this case, the DBTag’s dataSource property value is used instead to locate the DataSource in the application scope and get a Connection. The doEndTag( ) method in the DBTag class contains some details related to database transactions that we also skipped earlier. Let’s revisit this method, shown in Example 17.25. Example 17.25. The DBTag’s doEndTag( ) Method public int doEndTag( ) throws JspException { Connection conn = getConnection( ); sqlCommandBean.setConnection(conn); sqlCommandBean.setSqlValue(sqlValue); sqlCommandBean.setValues(values); Object result = null; try { result = execute(sqlCommandBean); } catch (SQLException e) { try { isExceptionThrown = true; conn.rollback( ); } catch (SQLException se) { // Ignore, probably a result of the main exception } throw new JspException(”SQL error: ” + e.getMessage( )); } catch (UnsupportedTypeException e) { try { isExceptionThrown = true; conn.rollback( ); } catch (SQLException se) { // Ignore, probably caused by the main exception } throw new JspException(”Query result error: ” + e.getMessage( )); } page 255
Hint: If you are looking for high quality webhost to host and run your jsp application check Vision web hosting jsp services

Comments are closed.