1 16 17 package org.apache.taglibs.standard.tag.common.sql; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.JspTagException ; 24 import javax.servlet.jsp.tagext.TagSupport ; 25 import javax.servlet.jsp.tagext.TryCatchFinally ; 26 import javax.sql.DataSource ; 27 28 import org.apache.taglibs.standard.resources.Resources; 29 30 31 36 37 public abstract class TransactionTagSupport extends TagSupport 38 implements TryCatchFinally { 39 40 43 private static final String TRANSACTION_READ_COMMITTED 44 = "read_committed"; 45 private static final String TRANSACTION_READ_UNCOMMITTED 46 = "read_uncommitted"; 47 private static final String TRANSACTION_REPEATABLE_READ 48 = "repeatable_read"; 49 private static final String TRANSACTION_SERIALIZABLE 50 = "serializable"; 51 52 53 56 protected Object rawDataSource; 57 protected boolean dataSourceSpecified; 58 59 60 63 private Connection conn; 64 private int isolation; 65 private int origIsolation; 66 67 68 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 87 92 public int doStartTag() throws JspException { 93 94 if ((rawDataSource == null) && dataSourceSpecified) { 95 throw new JspException ( 96 Resources.getMessage("SQL_DATASOURCE_NULL")); 97 } 98 99 DataSource 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 ( 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 e) { 115 throw new JspTagException ( 116 Resources.getMessage("ERROR_GET_CONNECTION", 117 e.toString()), e); 118 } 119 120 return EVAL_BODY_INCLUDE; 121 } 122 123 126 public int doEndTag() throws JspException { 127 try { 128 conn.commit(); 129 } catch (SQLException e) { 130 throw new JspTagException ( 131 Resources.getMessage("TRANSACTION_COMMIT_ERROR", 132 e.toString()), e); 133 } 134 return EVAL_PAGE; 135 } 136 137 140 public void doCatch(Throwable t) throws Throwable { 141 if (conn != null) { 142 try { 143 conn.rollback(); 144 } catch (SQLException e) { 145 } 147 } 148 throw t; 149 } 150 151 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 e) { 165 } 167 } 168 conn = null; 169 } 170 171 public void release() { 173 init(); 174 } 175 176 177 180 183 public void setIsolation(String iso) throws JspTagException { 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 ( 195 Resources.getMessage("TRANSACTION_INVALID_ISOLATION")); 196 } 197 } 198 199 203 public Connection getSharedConnection() { 204 return conn; 205 } 206 } 207 | Popular Tags |