1 55 package org.lateralnz.simpletrans; 56 57 import java.io.PrintWriter ; 58 import java.sql.Connection ; 59 import java.sql.SQLException ; 60 import java.util.Map ; 61 import java.util.HashMap ; 62 import javax.sql.DataSource ; 63 import javax.naming.Reference ; 64 import javax.naming.Referenceable ; 65 import javax.naming.StringRefAddr ; 66 67 import org.apache.log4j.Logger; 68 69 import org.lateralnz.common.util.Constants; 70 import org.lateralnz.common.util.JNDIUtils; 71 import org.lateralnz.common.util.StackThreadLocal; 72 73 79 public class TransDataSource implements Constants, DataSource , Referenceable { 80 private static final Logger log = Logger.getLogger(TransDataSource.class.getName()); 81 private static Map dataSources = new HashMap (); 82 private static SimpleTransactionManager stm = null; 83 84 private String name; 85 private DataSource source; 86 87 public TransDataSource(String name, DataSource source) throws Exception { 88 this.name = name; 89 this.source = source; 90 dataSources.put(name, this); 91 92 if (stm == null) { 93 synchronized (TransDataSource.class) { 94 if (stm == null) { 95 try { 96 Object tmp = JNDIUtils.get("java:/TransactionManager"); 97 if (tmp instanceof SimpleTransactionManager) { 98 stm = (SimpleTransactionManager)tmp; 99 } 100 else { 101 stm = (SimpleTransactionManager)tmp; 102 } 103 } 104 catch (Exception e) { 105 log.error(e); 106 throw e; 107 } 108 } 109 } 110 } 111 } 112 113 116 protected static final TransDataSource getTransDataSource(String name) { 117 return (TransDataSource)dataSources.get(name); 118 } 119 120 123 public Connection getConnection() throws SQLException { 124 return getConnection(null, null); 125 } 126 127 134 public Connection getConnection(String str, String str1) throws SQLException { 135 String methodTrans = (String )StackThreadLocal.peek(); 136 137 if (stm.isInTransaction() 138 && (methodTrans != null && (methodTrans.equals(REQUIRED) 139 || methodTrans.equals(SUPPORTS) 140 || methodTrans.equals(REQUIRES_NEW)))) { 141 SimpleTransaction trans = (SimpleTransaction)stm.getTransaction(); 142 Object tmp = trans.getResource(name); 143 TransConnection tc; 144 if (tmp != null) { 145 tc = (TransConnection)tmp; 146 return tc; 147 } 148 else if (methodTrans.equals(REQUIRED) || methodTrans.equals(REQUIRES_NEW)) { 149 tc = new TransConnection(name, getConn(str, str1), true); 150 trans.enlistResource(tc); 151 return tc; 152 } 153 } 154 155 Connection conn = getConn(str, str1); 156 conn.setAutoCommit(true); 157 return conn; 158 } 159 160 163 private final Connection getConn(String str, String str1) throws SQLException { 164 long time = System.currentTimeMillis(); 165 try { 166 if (str == null && str1 == null) { 167 return source.getConnection(); 168 } 169 else { 170 return source.getConnection(str, str1); 171 } 172 } 173 catch (SQLException e) { 174 time = System.currentTimeMillis() - time; 175 if (log.isDebugEnabled()) { 176 log.debug("time taken at SQL error point was " + time + "ms"); 177 } 178 e.printStackTrace(); 179 throw e; 180 } 181 } 182 183 186 public void setLogWriter(PrintWriter pw) throws SQLException { 187 source.setLogWriter(pw); 188 } 189 190 193 public PrintWriter getLogWriter() throws SQLException { 194 return source.getLogWriter(); 195 } 196 197 200 public void setLoginTimeout(int timeout) throws SQLException { 201 source.setLoginTimeout(timeout); 202 } 203 204 207 public int getLoginTimeout() throws SQLException { 208 return source.getLoginTimeout(); 209 } 210 211 214 public Reference getReference() { 215 Reference ref = new Reference (getClass().getName(), TransDataSourceObjectFactory.class.getName(), null); 216 ref.add(new StringRefAddr ("name", name)); 217 return ref; 218 } 219 } | Popular Tags |