001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.sql;
040:
041: import java.sql.Connection;
042: import java.sql.SQLException;
043:
044: import javax.servlet.jsp.JspException;
045: import javax.servlet.jsp.JspTagException;
046: import javax.servlet.jsp.tagext.TagSupport;
047: import javax.servlet.jsp.tagext.TryCatchFinally;
048: import javax.sql.DataSource;
049:
050: import org.apache.taglibs.standard.resources.Resources;
051:
052: /**
053: * <p>Tag handler for <Transaction> in JSTL.
054: *
055: * @author Hans Bergsten
056: */
057:
058: public abstract class TransactionTagSupport extends TagSupport
059: implements TryCatchFinally {
060:
061: //*********************************************************************
062: // Private constants
063:
064: private static final String TRANSACTION_READ_COMMITTED = "read_committed";
065: private static final String TRANSACTION_READ_UNCOMMITTED = "read_uncommitted";
066: private static final String TRANSACTION_REPEATABLE_READ = "repeatable_read";
067: private static final String TRANSACTION_SERIALIZABLE = "serializable";
068:
069: //*********************************************************************
070: // Protected state
071:
072: protected Object rawDataSource;
073: protected boolean dataSourceSpecified;
074:
075: //*********************************************************************
076: // Private state
077:
078: private Connection conn;
079: private int isolation;
080: private int origIsolation;
081:
082: //*********************************************************************
083: // Constructor and initialization
084:
085: public TransactionTagSupport() {
086: super ();
087: init();
088: }
089:
090: private void init() {
091: conn = null;
092: dataSourceSpecified = false;
093: rawDataSource = null;
094: isolation = Connection.TRANSACTION_NONE;
095: }
096:
097: //*********************************************************************
098: // Tag logic
099:
100: /**
101: * Prepares for execution by setting the initial state, such as
102: * getting the <code>Connection</code> and preparing it for
103: * the transaction.
104: */
105: public int doStartTag() throws JspException {
106:
107: if ((rawDataSource == null) && dataSourceSpecified) {
108: throw new JspException(Resources
109: .getMessage("SQL_DATASOURCE_NULL"));
110: }
111:
112: DataSource dataSource = DataSourceUtil.getDataSource(
113: rawDataSource, pageContext);
114:
115: try {
116: conn = dataSource.getConnection();
117: origIsolation = conn.getTransactionIsolation();
118: if (origIsolation == Connection.TRANSACTION_NONE) {
119: throw new JspTagException(Resources
120: .getMessage("TRANSACTION_NO_SUPPORT"));
121: }
122: if ((isolation != Connection.TRANSACTION_NONE)
123: && (isolation != origIsolation)) {
124: conn.setTransactionIsolation(isolation);
125: }
126: conn.setAutoCommit(false);
127: } catch (SQLException e) {
128: throw new JspTagException(Resources.getMessage(
129: "ERROR_GET_CONNECTION", e.toString()), e);
130: }
131:
132: return EVAL_BODY_INCLUDE;
133: }
134:
135: /**
136: * Commits the transaction.
137: */
138: public int doEndTag() throws JspException {
139: try {
140: conn.commit();
141: } catch (SQLException e) {
142: throw new JspTagException(Resources.getMessage(
143: "TRANSACTION_COMMIT_ERROR", e.toString()), e);
144: }
145: return EVAL_PAGE;
146: }
147:
148: /**
149: * Rollbacks the transaction and rethrows the Throwable.
150: */
151: public void doCatch(Throwable t) throws Throwable {
152: if (conn != null) {
153: try {
154: conn.rollback();
155: } catch (SQLException e) {
156: // Ignore to not hide orignal exception
157: }
158: }
159: throw t;
160: }
161:
162: /**
163: * Restores the <code>Connection</code> to its initial state and
164: * closes it.
165: */
166: public void doFinally() {
167: if (conn != null) {
168: try {
169: if ((isolation != Connection.TRANSACTION_NONE)
170: && (isolation != origIsolation)) {
171: conn.setTransactionIsolation(origIsolation);
172: }
173: conn.setAutoCommit(true);
174: conn.close();
175: } catch (SQLException e) {
176: // Not much we can do
177: }
178: }
179: conn = null;
180: }
181:
182: // Releases any resources we may have (or inherit)
183: public void release() {
184: init();
185: }
186:
187: //*********************************************************************
188: // Public utility methods
189:
190: /**
191: * Setter method for the transaction isolation level.
192: */
193: public void setIsolation(String iso) throws JspTagException {
194:
195: if (TRANSACTION_READ_COMMITTED.equals(iso)) {
196: isolation = Connection.TRANSACTION_READ_COMMITTED;
197: } else if (TRANSACTION_READ_UNCOMMITTED.equals(iso)) {
198: isolation = Connection.TRANSACTION_READ_UNCOMMITTED;
199: } else if (TRANSACTION_REPEATABLE_READ.equals(iso)) {
200: isolation = Connection.TRANSACTION_REPEATABLE_READ;
201: } else if (TRANSACTION_SERIALIZABLE.equals(iso)) {
202: isolation = Connection.TRANSACTION_SERIALIZABLE;
203: } else {
204: throw new JspTagException(Resources
205: .getMessage("TRANSACTION_INVALID_ISOLATION"));
206: }
207: }
208:
209: /**
210: * Called by nested parameter elements to get a reference to
211: * the Connection.
212: */
213: public Connection getSharedConnection() {
214: return conn;
215: }
216: }
|