KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > SqlTransactionTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import javax.el.ELContext;
37 import javax.servlet.jsp.JspException JavaDoc;
38 import javax.servlet.jsp.JspTagException JavaDoc;
39 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
40 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
41 import javax.sql.DataSource JavaDoc;
42 import java.sql.Connection JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 public class SqlTransactionTag extends TagSupport JavaDoc implements TryCatchFinally JavaDoc {
48   private static final Logger JavaDoc log = Log.open(SqlTransactionTag.class);
49   private static final L10N L = new L10N(SqlTransactionTag.class);
50   
51   private Expr _dataSource;
52   private Expr _isolation;
53
54   private Connection JavaDoc _conn;
55   private int _oldIsolation;
56
57   /**
58    * Sets the JSP-EL expression for the SQL.
59    */

60   public void setDataSource(Expr dataSource)
61   {
62     _dataSource = dataSource;
63   }
64
65   /**
66    * Sets the JSP-EL expression for the isolation.
67    */

68   public void setIsolation(Expr isolation)
69   {
70     _isolation = isolation;
71   }
72
73   public int doStartTag() throws JspException JavaDoc
74   {
75     if (pageContext.getAttribute("caucho.jstl.sql.conn") != null)
76       throw new JspTagException JavaDoc(L.l("nexted sql:transaction are forbidden"));
77
78     ELContext env = pageContext.getELContext();
79     
80     try {
81       DataSource JavaDoc ds;
82
83       if (_dataSource != null)
84         ds = SqlQueryTag.getDataSource(pageContext, _dataSource.evalObject(env));
85       else
86         ds = SqlQueryTag.getDataSource(pageContext, null);
87
88       int isolationCode = -1;
89       if (_isolation != null) {
90         String JavaDoc isolation = _isolation.evalString(env);
91
92         if (isolation.equals("read_committed"))
93           isolationCode = Connection.TRANSACTION_READ_COMMITTED;
94         else if (isolation.equals("read_uncommitted"))
95           isolationCode = Connection.TRANSACTION_READ_UNCOMMITTED;
96         else if (isolation.equals("repeatable_read"))
97           isolationCode = Connection.TRANSACTION_REPEATABLE_READ;
98         else if (isolation.equals("serializable"))
99           isolationCode = Connection.TRANSACTION_SERIALIZABLE;
100         else
101           throw new JspTagException JavaDoc(L.l("unknown sql:transaction isolation ~{0}'", isolation));
102       }
103
104       _conn = ds.getConnection();
105
106       _oldIsolation = _conn.getTransactionIsolation();
107
108       _conn.setAutoCommit(false);
109       
110       if (_isolation != null && isolationCode != _oldIsolation)
111         _conn.setTransactionIsolation(isolationCode);
112
113       pageContext.setAttribute("caucho.jstl.sql.conn", _conn);
114     } catch (JspException JavaDoc e) {
115       throw e;
116     } catch (Exception JavaDoc e) {
117       throw new JspException JavaDoc(e);
118     }
119
120     return EVAL_PAGE;
121   }
122
123   public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc
124   {
125     if (_conn != null)
126       _conn.rollback();
127
128     throw t;
129   }
130
131   public void doFinally()
132   {
133     try {
134       pageContext.removeAttribute("caucho.jstl.sql.conn");
135       
136       if (_conn != null) {
137         Connection JavaDoc conn = _conn;
138         _conn = null;
139
140         try {
141           conn.commit();
142         } finally {
143           try {
144             conn.setTransactionIsolation(_oldIsolation);
145           } catch (SQLException JavaDoc e) {
146           }
147           
148           try {
149             conn.close();
150           } catch (SQLException JavaDoc e) {
151           }
152         }
153       }
154     } catch (Exception JavaDoc e) {
155       log.log(Level.FINE, e.toString(), e);
156     }
157   }
158 }
159
Popular Tags