1 34 35 36 package com.micronova.jsp.tag; 37 38 import java.util.*; 39 import java.util.regex.*; 40 import java.sql.*; 41 import javax.sql.*; 42 import javax.naming.*; 43 import javax.servlet.jsp.*; 44 import javax.servlet.jsp.jstl.core.Config; 45 import org.apache.taglibs.standard.tag.common.sql.TransactionTagSupport; 46 import com.micronova.util.*; 47 48 49 50 public class TransactionTag extends YuzuTag 51 { 52 protected Object _dataSource; 53 protected int _isolation; 54 protected int _isolationBefore; 55 56 protected Connection _connection; 57 protected boolean _doesOwnConnection; 58 59 60 61 public static Connection openConnection(Object spec) throws Exception 62 { 63 Connection connection = null; 64 65 if (spec instanceof DataSource) 66 { 67 connection = ((DataSource)spec).getConnection(); 68 } 69 else 70 { 71 String specString = (String )spec; 72 73 if (specString.startsWith("jdbc:")) 74 { 75 List parts = StringUtil.split(specString, ','); 76 77 int partsLength = parts.size(); 78 79 String jdbcURL = parts.get(0).toString(); 80 String driverClassName = parts.get(1).toString(); 81 82 String user = null; 83 String password = null; 84 85 if (partsLength >= 3) 86 { 87 user = parts.get(2).toString(); 88 } 89 90 if (partsLength >= 4) 91 { 92 password = parts.get(3).toString(); 93 } 94 95 Class.forName(driverClassName, true, Thread.currentThread().getContextClassLoader()).newInstance(); 96 97 if ((user == null) && (password == null)) 98 { 99 connection = DriverManager.getConnection(jdbcURL); 100 } 101 else 102 { 103 connection = DriverManager.getConnection(jdbcURL, user, password); 104 } 105 } 106 else 107 { 108 Context context = new InitialContext(); 109 Context environment = (Context)context.lookup("java:comp/env"); 110 111 DataSource dataSource = (DataSource)environment.lookup(specString); 112 113 connection = dataSource.getConnection(); 114 } 115 } 116 117 return connection; 118 } 119 120 protected static TransactionTag getAncestorTransactionTag(YuzuTag tag) 121 { 122 return (TransactionTag)tag.getAncestorTag("com.micronova.jsp.tag.TransactionTag"); 123 } 124 125 protected static TransactionTagSupport getAncestorTransactionTagSupport(YuzuTag tag) 126 { 127 return (TransactionTagSupport)tag.getAncestorTag("org.apache.taglibs.standard.tag.common.sql.TransactionTagSupport"); 128 } 129 130 131 132 public static Connection getAncestorConnection(YuzuTag tag) throws Exception 133 { 134 TransactionTag transactionTag = getAncestorTransactionTag(tag); 135 136 if (transactionTag != null) 137 { 138 return transactionTag.getConnection(); 139 } 140 141 TransactionTagSupport transactionTagSupport = getAncestorTransactionTagSupport(tag); 142 143 if (transactionTagSupport != null) 144 { 145 return transactionTagSupport.getSharedConnection(); 146 } 147 148 return null; 149 } 150 151 protected void init() 152 { 153 super.init(); 154 155 _dataSource = null; 156 _isolation = Connection.TRANSACTION_NONE; 157 _isolationBefore = Connection.TRANSACTION_NONE; 158 _connection = null; 159 _doesOwnConnection = false; 160 } 161 162 protected Connection getConnection(Object spec) throws Exception 163 { 164 Connection connection = _connection; 165 166 if (connection != null) 167 { 168 return connection; 169 } 170 else 171 { 172 if (spec == null) 173 { 174 spec = getConfiguration(Config.SQL_DATA_SOURCE, null); 175 } 176 177 connection = openConnection(spec); 178 } 179 180 return connection; 181 } 182 183 protected Connection getConnection() throws Exception 184 { 185 return getConnection(_dataSource); 186 } 187 188 public void initBody() throws Exception 189 { 190 Connection connection = null; 191 192 Object dataSource = _dataSource; 193 194 if (dataSource == null) 195 { 196 connection = getAncestorConnection(this); 197 } 198 199 if (connection != null) 200 { 201 _doesOwnConnection = false; 202 _connection = connection; 203 _isolationBefore = connection.getTransactionIsolation(); 204 } 205 else 206 { 207 _doesOwnConnection = true; 208 connection = getConnection(dataSource); 209 connection.setAutoCommit(false); 210 211 _connection = connection; 212 } 213 214 if (connection != null) 215 { 216 int isolation = _isolation; 217 218 if (isolation != Connection.TRANSACTION_NONE) 219 { 220 connection.setTransactionIsolation(isolation); 221 } 222 } 223 224 super.initBody(); 225 } 226 227 public void afterBody() throws Exception 228 { 229 super.afterBody(); 230 231 if (_doesOwnConnection) 232 { 233 _connection.commit(); 234 } 235 } 236 237 public void doCatch(Throwable t) throws Throwable 238 { 239 boolean doesOwnConnection = _doesOwnConnection; 240 241 if (doesOwnConnection) 242 { 243 _connection.rollback(); 244 } 245 246 super.doCatch(t); 247 } 248 249 public void doFinally() 250 { 251 try 252 { 253 if (_doesOwnConnection) 254 { 255 try 256 { 257 _connection.close(); 258 } 259 catch (Exception ee) 260 { 261 } 262 } 263 else 264 { 265 _connection.setTransactionIsolation(_isolationBefore); 266 } 267 } 268 catch (Exception e) 269 { 270 } 271 272 super.doFinally(); 273 } 274 275 public void setDataSource(Object expression) throws Exception 276 { 277 Object dataSource = evaluateAttribute("dataSource", expression, Object .class); 278 279 if (!isEmptyString(dataSource)) 280 { 281 _dataSource = dataSource; 282 } 283 } 284 285 public void setIsolation(Object expression) throws Exception 286 { 287 String isolationSpec = (String )evaluateAttribute("dataSource", expression, String .class); 288 289 if ("read_committed".equals(isolationSpec)) 290 { 291 _isolation = Connection.TRANSACTION_READ_COMMITTED; 292 } 293 else if ("read_uncommitted".equals(isolationSpec)) 294 { 295 _isolation = Connection.TRANSACTION_READ_UNCOMMITTED; 296 } 297 else if ("repeatable_read".equals(isolationSpec)) 298 { 299 _isolation = Connection.TRANSACTION_REPEATABLE_READ; 300 } 301 else if ("serializable".equals(isolationSpec)) 302 { 303 _isolation = Connection.TRANSACTION_SERIALIZABLE; 304 } 305 else if ("none".equals(isolationSpec)) 306 { 307 _isolation = Connection.TRANSACTION_NONE; 308 } 309 else 310 { 311 throw new JspTagException("unsupported transaction isolation:" + isolationSpec); 312 } 313 } 314 } 315 | Popular Tags |