KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > sql > TransactionTagSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.sql;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27
28 import org.apache.taglibs.standard.resources.Resources;
29
30
31 /**
32  * <p>Tag handler for &lt;Transaction&gt; in JSTL.
33  *
34  * @author Hans Bergsten
35  */

36
37 public abstract class TransactionTagSupport extends TagSupport JavaDoc
38     implements TryCatchFinally JavaDoc {
39
40     //*********************************************************************
41
// Private constants
42

43     private static final String JavaDoc TRANSACTION_READ_COMMITTED
44     = "read_committed";
45     private static final String JavaDoc TRANSACTION_READ_UNCOMMITTED
46     = "read_uncommitted";
47     private static final String JavaDoc TRANSACTION_REPEATABLE_READ
48     = "repeatable_read";
49     private static final String JavaDoc TRANSACTION_SERIALIZABLE
50     = "serializable";
51
52
53     //*********************************************************************
54
// Protected state
55

56     protected Object JavaDoc rawDataSource;
57     protected boolean dataSourceSpecified;
58
59
60     //*********************************************************************
61
// Private state
62

63     private Connection JavaDoc conn;
64     private int isolation;
65     private int origIsolation;
66
67
68     //*********************************************************************
69
// Constructor and initialization
70

71     public TransactionTagSupport() {
72     super();
73     init();
74     }
75
76     private void init() {
77     conn = null;
78     dataSourceSpecified = false;
79     rawDataSource = null;
80     isolation = Connection.TRANSACTION_NONE;
81     }
82
83
84     //*********************************************************************
85
// Tag logic
86

87     /**
88      * Prepares for execution by setting the initial state, such as
89      * getting the <code>Connection</code> and preparing it for
90      * the transaction.
91      */

92     public int doStartTag() throws JspException JavaDoc {
93
94     if ((rawDataSource == null) && dataSourceSpecified) {
95         throw new JspException JavaDoc(
96                 Resources.getMessage("SQL_DATASOURCE_NULL"));
97     }
98
99         DataSource JavaDoc dataSource = DataSourceUtil.getDataSource(rawDataSource,
100                                  pageContext);
101
102     try {
103         conn = dataSource.getConnection();
104         origIsolation = conn.getTransactionIsolation();
105         if (origIsolation == Connection.TRANSACTION_NONE) {
106         throw new JspTagException JavaDoc(
107                     Resources.getMessage("TRANSACTION_NO_SUPPORT"));
108         }
109         if ((isolation != Connection.TRANSACTION_NONE)
110             && (isolation != origIsolation)) {
111         conn.setTransactionIsolation(isolation);
112         }
113         conn.setAutoCommit(false);
114     } catch (SQLException JavaDoc e) {
115         throw new JspTagException JavaDoc(
116                 Resources.getMessage("ERROR_GET_CONNECTION",
117                      e.toString()), e);
118     }
119
120     return EVAL_BODY_INCLUDE;
121     }
122
123     /**
124      * Commits the transaction.
125      */

126     public int doEndTag() throws JspException JavaDoc {
127     try {
128         conn.commit();
129     } catch (SQLException JavaDoc e) {
130         throw new JspTagException JavaDoc(
131                 Resources.getMessage("TRANSACTION_COMMIT_ERROR",
132                      e.toString()), e);
133     }
134     return EVAL_PAGE;
135     }
136
137     /**
138      * Rollbacks the transaction and rethrows the Throwable.
139      */

140     public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
141     if (conn != null) {
142         try {
143         conn.rollback();
144         } catch (SQLException JavaDoc e) {
145         // Ignore to not hide orignal exception
146
}
147     }
148     throw t;
149     }
150
151     /**
152      * Restores the <code>Connection</code> to its initial state and
153      * closes it.
154      */

155     public void doFinally() {
156     if (conn != null) {
157         try {
158         if ((isolation != Connection.TRANSACTION_NONE)
159                 && (isolation != origIsolation)) {
160             conn.setTransactionIsolation(origIsolation);
161         }
162         conn.setAutoCommit(true);
163         conn.close();
164         } catch (SQLException JavaDoc e) {
165         // Not much we can do
166
}
167     }
168     conn = null;
169     }
170
171     // Releases any resources we may have (or inherit)
172
public void release() {
173     init();
174     }
175
176
177     //*********************************************************************
178
// Public utility methods
179

180     /**
181      * Setter method for the transaction isolation level.
182      */

183     public void setIsolation(String JavaDoc iso) throws JspTagException JavaDoc {
184
185     if (TRANSACTION_READ_COMMITTED.equals(iso)) {
186         isolation = Connection.TRANSACTION_READ_COMMITTED;
187     } else if (TRANSACTION_READ_UNCOMMITTED.equals(iso)) {
188         isolation = Connection.TRANSACTION_READ_UNCOMMITTED;
189     } else if (TRANSACTION_REPEATABLE_READ.equals(iso)) {
190         isolation = Connection.TRANSACTION_REPEATABLE_READ;
191     } else if (TRANSACTION_SERIALIZABLE.equals(iso)) {
192         isolation = Connection.TRANSACTION_SERIALIZABLE;
193     } else {
194         throw new JspTagException JavaDoc(
195                 Resources.getMessage("TRANSACTION_INVALID_ISOLATION"));
196     }
197     }
198
199     /**
200      * Called by nested parameter elements to get a reference to
201      * the Connection.
202      */

203     public Connection JavaDoc getSharedConnection() {
204     return conn;
205     }
206 }
207
Popular Tags