FileDocCategorySizeDatePackage
TransactionTagSupport.javaAPI DocGlassfish v2 API6983Sat May 05 19:17:54 BST 2007org.apache.taglibs.standard.tag.common.sql

TransactionTagSupport

public abstract class TransactionTagSupport extends javax.servlet.jsp.tagext.TagSupport implements javax.servlet.jsp.tagext.TryCatchFinally

Tag handler for <Transaction> in JSTL.

author
Hans Bergsten

Fields Summary
private static final String
TRANSACTION_READ_COMMITTED
private static final String
TRANSACTION_READ_UNCOMMITTED
private static final String
TRANSACTION_REPEATABLE_READ
private static final String
TRANSACTION_SERIALIZABLE
protected Object
rawDataSource
protected boolean
dataSourceSpecified
private Connection
conn
private int
isolation
private int
origIsolation
Constructors Summary
public TransactionTagSupport()



    //*********************************************************************
    // Constructor and initialization

      
	super();
	init();
    
Methods Summary
public voiddoCatch(java.lang.Throwable t)
Rollbacks the transaction and rethrows the Throwable.

	if (conn != null) {
	    try {
		conn.rollback();
	    } catch (SQLException e) {
		// Ignore to not hide orignal exception
	    }
	}
	throw t;
    
public intdoEndTag()
Commits the transaction.

	try {
	    conn.commit();
	} catch (SQLException e) {
	    throw new JspTagException(
                Resources.getMessage("TRANSACTION_COMMIT_ERROR",
				     e.toString()), e);
	}
	return EVAL_PAGE;
    
public voiddoFinally()
Restores the Connection to its initial state and closes it.

	if (conn != null) {
	    try {
		if ((isolation != Connection.TRANSACTION_NONE)
		        && (isolation != origIsolation)) {
		    conn.setTransactionIsolation(origIsolation);
		}
		conn.setAutoCommit(true);
		conn.close();
	    } catch (SQLException e) {
		// Not much we can do
	    }
	}
	conn = null;
    
public intdoStartTag()
Prepares for execution by setting the initial state, such as getting the Connection and preparing it for the transaction.


	if ((rawDataSource == null) && dataSourceSpecified) {
	    throw new JspException(
                Resources.getMessage("SQL_DATASOURCE_NULL"));
	}

        DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource,
							     pageContext);

	try {
	    conn = dataSource.getConnection();
	    origIsolation = conn.getTransactionIsolation();
	    if (origIsolation == Connection.TRANSACTION_NONE) {
		throw new JspTagException(
                    Resources.getMessage("TRANSACTION_NO_SUPPORT"));
	    }
	    if ((isolation != Connection.TRANSACTION_NONE)
		    && (isolation != origIsolation)) {
		conn.setTransactionIsolation(isolation);
	    }
	    conn.setAutoCommit(false);
	} catch (SQLException e) {
	    throw new JspTagException(
                Resources.getMessage("ERROR_GET_CONNECTION",
				     e.toString()), e);
	} 

	return EVAL_BODY_INCLUDE;
    
public java.sql.ConnectiongetSharedConnection()
Called by nested parameter elements to get a reference to the Connection.

	return conn;
    
private voidinit()

	conn = null;
	dataSourceSpecified = false;
	rawDataSource = null;
	isolation = Connection.TRANSACTION_NONE;
    
public voidrelease()

	init();
    
public voidsetIsolation(java.lang.String iso)
Setter method for the transaction isolation level.


	if (TRANSACTION_READ_COMMITTED.equals(iso)) {
	    isolation = Connection.TRANSACTION_READ_COMMITTED;
	} else if (TRANSACTION_READ_UNCOMMITTED.equals(iso)) {
	    isolation = Connection.TRANSACTION_READ_UNCOMMITTED;
	} else if (TRANSACTION_REPEATABLE_READ.equals(iso)) {
	    isolation = Connection.TRANSACTION_REPEATABLE_READ;
	} else if (TRANSACTION_SERIALIZABLE.equals(iso)) {
	    isolation = Connection.TRANSACTION_SERIALIZABLE;
	} else {
	    throw new JspTagException(
                Resources.getMessage("TRANSACTION_INVALID_ISOLATION"));
	}