1 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 ; 38 import javax.servlet.jsp.JspTagException ; 39 import javax.servlet.jsp.tagext.TagSupport ; 40 import javax.servlet.jsp.tagext.TryCatchFinally ; 41 import javax.sql.DataSource ; 42 import java.sql.Connection ; 43 import java.sql.SQLException ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 public class SqlTransactionTag extends TagSupport implements TryCatchFinally { 48 private static final Logger 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 _conn; 55 private int _oldIsolation; 56 57 60 public void setDataSource(Expr dataSource) 61 { 62 _dataSource = dataSource; 63 } 64 65 68 public void setIsolation(Expr isolation) 69 { 70 _isolation = isolation; 71 } 72 73 public int doStartTag() throws JspException 74 { 75 if (pageContext.getAttribute("caucho.jstl.sql.conn") != null) 76 throw new JspTagException (L.l("nexted sql:transaction are forbidden")); 77 78 ELContext env = pageContext.getELContext(); 79 80 try { 81 DataSource 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 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 (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 e) { 115 throw e; 116 } catch (Exception e) { 117 throw new JspException (e); 118 } 119 120 return EVAL_PAGE; 121 } 122 123 public void doCatch(Throwable t) throws Throwable 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 conn = _conn; 138 _conn = null; 139 140 try { 141 conn.commit(); 142 } finally { 143 try { 144 conn.setTransactionIsolation(_oldIsolation); 145 } catch (SQLException e) { 146 } 147 148 try { 149 conn.close(); 150 } catch (SQLException e) { 151 } 152 } 153 } 154 } catch (Exception e) { 155 log.log(Level.FINE, e.toString(), e); 156 } 157 } 158 } 159 | Popular Tags |