KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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

57   public void setDataSource(Object JavaDoc dataSource)
58   {
59     _dataSource = dataSource;
60   }
61
62   /**
63    * Sets the JSP-EL expression for the isolation.
64    */

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