1 19 20 package org.apache.cayenne.access; 21 22 import java.io.PrintWriter ; 23 import java.sql.Connection ; 24 import java.sql.SQLException ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 import javax.sql.DataSource ; 32 33 import org.apache.cayenne.CayenneRuntimeException; 34 import org.apache.cayenne.conn.PoolManager; 35 import org.apache.cayenne.dba.DbAdapter; 36 import org.apache.cayenne.map.AshwoodEntitySorter; 37 import org.apache.cayenne.map.DataMap; 38 import org.apache.cayenne.map.EntityResolver; 39 import org.apache.cayenne.map.EntitySorter; 40 import org.apache.cayenne.query.Query; 41 42 52 public class DataNode implements QueryEngine { 53 54 protected String name; 55 protected DataSource dataSource; 56 protected DbAdapter adapter; 57 protected String dataSourceLocation; 58 protected String dataSourceFactory; 59 protected EntityResolver entityResolver; 60 protected EntitySorter entitySorter; 61 protected Map dataMaps; 62 63 TransactionDataSource readThroughDataSource; 64 65 68 public DataNode() { 69 this(null); 70 } 71 72 75 public DataNode(String name) { 76 this.name = name; 77 this.dataMaps = new HashMap (); 78 this.readThroughDataSource = new TransactionDataSource(); 79 80 this.entitySorter = new AshwoodEntitySorter(Collections.EMPTY_LIST); 84 } 85 86 89 public String getName() { 90 return name; 91 } 92 93 public void setName(String name) { 94 this.name = name; 95 } 96 97 101 public String getDataSourceLocation() { 102 return dataSourceLocation; 103 } 104 105 public void setDataSourceLocation(String dataSourceLocation) { 106 this.dataSourceLocation = dataSourceLocation; 107 } 108 109 112 public String getDataSourceFactory() { 113 return dataSourceFactory; 114 } 115 116 public void setDataSourceFactory(String dataSourceFactory) { 117 this.dataSourceFactory = dataSourceFactory; 118 } 119 120 123 public Collection getDataMaps() { 124 return Collections.unmodifiableCollection(dataMaps.values()); 125 } 126 127 public void setDataMaps(Collection dataMaps) { 128 Iterator it = dataMaps.iterator(); 129 while (it.hasNext()) { 130 DataMap map = (DataMap) it.next(); 131 this.dataMaps.put(map.getName(), map); 132 } 133 134 entitySorter.setDataMaps(dataMaps); 135 } 136 137 140 public void addDataMap(DataMap map) { 141 this.dataMaps.put(map.getName(), map); 142 143 entitySorter.setDataMaps(getDataMaps()); 144 } 145 146 public void removeDataMap(String mapName) { 147 DataMap map = (DataMap) dataMaps.remove(mapName); 148 if (map != null) { 149 entitySorter.setDataMaps(getDataMaps()); 150 } 151 } 152 153 156 public DataSource getDataSource() { 157 return dataSource != null ? readThroughDataSource : null; 158 } 159 160 public void setDataSource(DataSource dataSource) { 161 this.dataSource = dataSource; 162 } 163 164 168 public DbAdapter getAdapter() { 169 return adapter; 170 } 171 172 public void setAdapter(DbAdapter adapter) { 173 this.adapter = adapter; 174 } 175 176 181 public DataNode lookupDataNode(DataMap dataMap) { 182 return this; 184 } 185 186 191 public void performQueries(Collection queries, OperationObserver callback) { 192 193 int listSize = queries.size(); 194 if (listSize == 0) { 195 return; 196 } 197 198 if (callback.isIteratedResult() && listSize > 1) { 199 throw new CayenneRuntimeException( 200 "Iterated queries are not allowed in a batch. Batch size: " 201 + listSize); 202 } 203 204 QueryLogger.logQueryStart(listSize); 205 206 getAdapter().getExtendedTypes(); 210 211 Connection connection = null; 212 213 try { 214 connection = this.getDataSource().getConnection(); 215 } 216 catch (Exception globalEx) { 217 QueryLogger.logQueryError(globalEx); 218 219 Transaction transaction = Transaction.getThreadTransaction(); 220 if (transaction != null) { 221 transaction.setRollbackOnly(); 222 } 223 224 callback.nextGlobalException(globalEx); 225 return; 226 } 227 228 try { 229 DataNodeQueryAction queryRunner = new DataNodeQueryAction(this, callback); 230 Iterator it = queries.iterator(); 231 while (it.hasNext()) { 232 Query nextQuery = (Query) it.next(); 233 234 try { 236 queryRunner.runQuery(connection, nextQuery); 237 } 238 catch (Exception queryEx) { 239 QueryLogger.logQueryError(queryEx); 240 241 callback.nextQueryException(nextQuery, queryEx); 244 245 Transaction transaction = Transaction.getThreadTransaction(); 246 if (transaction != null) { 247 transaction.setRollbackOnly(); 248 } 249 break; 250 } 251 } 252 } 253 finally { 254 try { 255 connection.close(); 256 } 257 catch (SQLException e) { 258 } 260 } 261 } 262 263 266 public EntityResolver getEntityResolver() { 267 return entityResolver; 268 } 269 270 277 public void setEntityResolver(org.apache.cayenne.map.EntityResolver entityResolver) { 278 this.entityResolver = entityResolver; 279 } 280 281 284 public EntitySorter getEntitySorter() { 285 return entitySorter; 286 } 287 288 293 public void setEntitySorter(EntitySorter entitySorter) { 294 this.entitySorter = entitySorter; 295 } 296 297 300 public synchronized void shutdown() { 301 try { 302 if (dataSource instanceof PoolManager) { 306 ((PoolManager) dataSource).dispose(); 307 dataSource = null; 308 } 309 } 310 catch (SQLException ex) { 311 } 312 } 313 314 final class TransactionDataSource implements DataSource { 317 318 final String CONNECTION_RESOURCE_PREFIX = "DataNode.Connection."; 319 320 public Connection getConnection() throws SQLException { 321 Transaction t = Transaction.getThreadTransaction(); 322 if (t != null) { 323 String key = CONNECTION_RESOURCE_PREFIX + name; 324 Connection c = t.getConnection(key); 325 326 if (c == null || c.isClosed()) { 327 c = dataSource.getConnection(); 328 t.addConnection(key, c); 329 } 330 331 return new TransactionConnectionDecorator(c); 335 } 336 337 return dataSource.getConnection(); 338 } 339 340 public Connection getConnection(String username, String password) 341 throws SQLException { 342 343 Transaction t = Transaction.getThreadTransaction(); 344 if (t != null) { 345 String key = CONNECTION_RESOURCE_PREFIX + name; 346 Connection c = t.getConnection(key); 347 348 if (c == null || c.isClosed()) { 349 c = dataSource.getConnection(); 350 t.addConnection(key, c); 351 } 352 353 return new TransactionConnectionDecorator(c); 357 } 358 359 return dataSource.getConnection(username, password); 360 } 361 362 public int getLoginTimeout() throws SQLException { 363 return dataSource.getLoginTimeout(); 364 } 365 366 public PrintWriter getLogWriter() throws SQLException { 367 return dataSource.getLogWriter(); 368 } 369 370 public void setLoginTimeout(int seconds) throws SQLException { 371 dataSource.setLoginTimeout(seconds); 372 } 373 374 public void setLogWriter(PrintWriter out) throws SQLException { 375 dataSource.setLogWriter(out); 376 } 377 } 378 } 379 | Popular Tags |