Revision 27672 branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/jdbc/JDBCStoreProvider.java
JDBCStoreProvider.java | ||
---|---|---|
38 | 38 |
import java.util.Iterator; |
39 | 39 |
import java.util.List; |
40 | 40 |
|
41 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
42 |
import org.gvsig.fmap.dal.exception.CloseException; |
|
41 | 43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.exception.OpenException; |
|
45 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
46 |
import org.gvsig.fmap.dal.feature.Feature; |
|
42 | 47 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
48 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
50 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
|
43 | 51 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
44 | 52 |
import org.gvsig.fmap.dal.feature.spi.FeatureData; |
53 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
54 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
|
45 | 55 |
import org.gvsig.fmap.dal.resource.Resource; |
46 | 56 |
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
47 | 57 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
48 | 58 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
49 | 59 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
60 |
import org.gvsig.tools.persistence.PersistenceException; |
|
61 |
import org.gvsig.tools.persistence.PersistentState; |
|
62 |
import org.slf4j.Logger; |
|
63 |
import org.slf4j.LoggerFactory; |
|
50 | 64 |
|
51 | 65 |
|
52 | 66 |
/** |
... | ... | |
56 | 70 |
public abstract class JDBCStoreProvider extends AbstractFeatureStoreProvider |
57 | 71 |
implements ResourceConsumer { |
58 | 72 |
|
73 |
final static private Logger logger = LoggerFactory |
|
74 |
.getLogger(JDBCStoreProvider.class); |
|
75 |
|
|
59 | 76 |
private List resulsetList; |
60 | 77 |
|
61 | 78 |
|
... | ... | |
75 | 92 |
*/ |
76 | 93 |
protected abstract void resourceEnd(); |
77 | 94 |
|
78 |
protected abstract String compoundSelect(String[] fields, String filter,
|
|
95 |
public String compoundSelect(FeatureType type, String filter,
|
|
79 | 96 |
String order, |
97 |
long limit, long offset){ |
|
98 |
FeatureAttributeDescriptor attr; |
|
99 |
List fieldList = new ArrayList(); |
|
100 |
Iterator iter = type.iterator(); |
|
101 |
while (iter.hasNext()){ |
|
102 |
attr = (FeatureAttributeDescriptor) iter.next(); |
|
103 |
fieldList.add(attr.getName()); |
|
104 |
} |
|
105 |
|
|
106 |
String[] fields = (String[]) fieldList.toArray(new String[0]); |
|
107 |
|
|
108 |
return compoundSelect(fields, filter, order, limit, offset); |
|
109 |
} |
|
110 |
|
|
111 |
public abstract String compoundSelect(String[] fields, String filter, |
|
112 |
String order, |
|
80 | 113 |
long limit, long offset); |
81 | 114 |
|
82 | 115 |
/** |
... | ... | |
139 | 172 |
|
140 | 173 |
} |
141 | 174 |
|
142 |
public final synchronized int createResultSet(String sql)
|
|
175 |
public final int createResultSet(String sql) |
|
143 | 176 |
throws DataException { |
144 | 177 |
return createResultSet(sql, null); |
145 | 178 |
} |
146 | 179 |
|
147 |
public final synchronized int createResultSet(String sql, Object[] values)
|
|
180 |
public final int createResultSet(String sql, Object[] values) |
|
148 | 181 |
throws DataException { |
149 |
resourceBegin(); |
|
150 |
try { |
|
151 |
ResultSet newRs = createNewResultSet(sql, values); |
|
152 |
int newId = getNewId(); |
|
153 |
if (newId < 0) { |
|
154 |
newId = resulsetList.size(); |
|
155 |
resulsetList.add(newRs); |
|
156 |
} else { |
|
157 |
resulsetList.set(newId, newRs); |
|
182 |
synchronized (this) { |
|
183 |
resourceBegin(); |
|
184 |
try { |
|
185 |
ResultSet newRs = createNewResultSet(sql, values); |
|
186 |
int newId = getNewId(); |
|
187 |
if (newId < 0) { |
|
188 |
newId = resulsetList.size(); |
|
189 |
resulsetList.add(newRs); |
|
190 |
} else { |
|
191 |
resulsetList.set(newId, newRs); |
|
192 |
} |
|
193 |
if (logger.isDebugEnabled()) { |
|
194 |
logger.debug(" id: " + newId + " (total " |
|
195 |
+ getResultsetOpenCount() + ")"); |
|
196 |
} |
|
197 |
|
|
198 |
return newId; |
|
199 |
} finally { |
|
200 |
resourceEnd(); |
|
158 | 201 |
} |
159 | 202 |
|
160 |
return newId; |
|
161 |
} finally{ |
|
162 |
resourceEnd(); |
|
163 | 203 |
} |
164 | 204 |
} |
165 | 205 |
|
... | ... | |
179 | 219 |
return -1; |
180 | 220 |
} |
181 | 221 |
|
222 |
protected final void forceCloseAllResultSet() |
|
223 |
throws ResourceBeginException, |
|
224 |
JDBCSQLException { |
|
225 |
synchronized (this) { |
|
226 |
Iterator iter = resulsetList.iterator(); |
|
227 |
Integer rsID = null; |
|
228 |
while (iter.hasNext()) { |
|
229 |
rsID = (Integer) iter.next(); |
|
230 |
if (rsID != null) { |
|
231 |
try { |
|
232 |
logger.warn("Close forced of resultSet ({})", rsID); |
|
233 |
closeResulset(rsID.intValue()); |
|
234 |
} catch (InvalidResultSetIdException e) { |
|
235 |
continue; |
|
236 |
} |
|
237 |
} |
|
238 |
iter.remove(); |
|
239 |
} |
|
240 |
|
|
241 |
} |
|
242 |
|
|
243 |
} |
|
244 |
|
|
182 | 245 |
protected final ResultSet getResultSet(int resultsetID) |
183 | 246 |
throws InvalidResultSetIdException { |
184 | 247 |
if (resultsetID >= resulsetList.size()) { |
... | ... | |
192 | 255 |
|
193 | 256 |
} |
194 | 257 |
|
195 |
protected final ResultSet dropResultSet(int resultsetID)
|
|
258 |
private ResultSet dropResultSet(int resultsetID)
|
|
196 | 259 |
throws InvalidResultSetIdException { |
197 | 260 |
if (resultsetID >= resulsetList.size()) { |
198 | 261 |
throw new InvalidResultSetIdException(resultsetID); |
... | ... | |
223 | 286 |
} |
224 | 287 |
} |
225 | 288 |
|
226 |
public final synchronized void closeResulset(int resultsetID)
|
|
289 |
public final void closeResulset(int resultsetID) |
|
227 | 290 |
throws JDBCSQLException, |
228 | 291 |
InvalidResultSetIdException, ResourceBeginException { |
229 |
ResultSet rs = getResultSet(resultsetID); |
|
230 |
resourceBegin(); |
|
231 |
try { |
|
232 |
rs = dropResultSet(resultsetID); |
|
233 |
closeResulset(rs); |
|
234 |
} finally { |
|
235 |
resourceEnd(); |
|
292 |
synchronized (this) { |
|
293 |
resourceBegin(); |
|
294 |
try { |
|
295 |
ResultSet rs = dropResultSet(resultsetID); |
|
296 |
closeResulset(rs); |
|
297 |
if (logger.isDebugEnabled()) { |
|
298 |
logger.debug(" id: " + resultsetID + " (total " |
|
299 |
+ getResultsetOpenCount() + ")"); |
|
300 |
} |
|
301 |
|
|
302 |
} finally { |
|
303 |
resourceEnd(); |
|
304 |
} |
|
305 |
|
|
236 | 306 |
} |
237 | 307 |
} |
238 | 308 |
|
239 |
protected void closeResulset(ResultSet rs) throws JDBCSQLException { |
|
309 |
private int getResultsetOpenCount() { |
|
310 |
int count = 0; |
|
311 |
Iterator iter = resulsetList.iterator(); |
|
312 |
while (iter.hasNext()) { |
|
313 |
if (iter.next() != null) { |
|
314 |
count++; |
|
315 |
} |
|
316 |
} |
|
317 |
return count; |
|
318 |
} |
|
319 |
|
|
320 |
protected void closeResulset(ResultSet rs) throws JDBCSQLException, |
|
321 |
ResourceBeginException { |
|
322 |
resourceBegin(); |
|
240 | 323 |
try { |
241 | 324 |
Statement st = rs.getStatement(); |
242 | 325 |
Connection con = st.getConnection(); |
... | ... | |
249 | 332 |
} |
250 | 333 |
} catch (SQLException e) { |
251 | 334 |
throw new JDBCSQLException(e); |
335 |
} finally { |
|
336 |
resourceEnd(); |
|
252 | 337 |
} |
338 |
|
|
253 | 339 |
} |
254 | 340 |
|
255 | 341 |
protected final int openResulsetCount() { |
... | ... | |
274 | 360 |
protected abstract boolean closeResource(ResourceProvider resource) |
275 | 361 |
throws ResourceNotifyCloseException; |
276 | 362 |
|
363 |
public void dispose() throws CloseException { |
|
364 |
this.close(); |
|
365 |
resulsetList = null; |
|
366 |
super.dispose(); |
|
367 |
} |
|
368 |
|
|
369 |
public DataServerExplorer getExplorer() throws ReadException { |
|
370 |
// TODO Auto-generated method stub |
|
371 |
return null; |
|
372 |
} |
|
373 |
|
|
374 |
public void performEditing(Iterator deleteds, Iterator inserteds, |
|
375 |
Iterator updateds) throws PerformEditingException { |
|
376 |
// TODO Auto-generated method stub |
|
377 |
|
|
378 |
} |
|
379 |
|
|
380 |
public PersistentState getState() throws PersistenceException { |
|
381 |
// TODO Auto-generated method stub |
|
382 |
return null; |
|
383 |
} |
|
384 |
|
|
385 |
public void loadState(PersistentState state) throws PersistenceException { |
|
386 |
// TODO Auto-generated method stub |
|
387 |
|
|
388 |
} |
|
389 |
|
|
390 |
public void setState(PersistentState state) throws PersistenceException { |
|
391 |
// TODO Auto-generated method stub |
|
392 |
|
|
393 |
} |
|
394 |
|
|
395 |
public Iterator getChilds() { |
|
396 |
// TODO Auto-generated method stub |
|
397 |
return null; |
|
398 |
} |
|
399 |
|
|
400 |
public Object getSourceId() { |
|
401 |
// TODO Auto-generated method stub |
|
402 |
return null; |
|
403 |
} |
|
404 |
|
|
405 |
public void open() throws OpenException { |
|
406 |
// TODO Auto-generated method stub |
|
407 |
|
|
408 |
} |
|
409 |
|
|
410 |
public void append(Feature feature) throws DataException { |
|
411 |
// TODO Auto-generated method stub |
|
412 |
|
|
413 |
} |
|
414 |
|
|
415 |
public void beginAppend() throws DataException { |
|
416 |
// TODO Auto-generated method stub |
|
417 |
|
|
418 |
} |
|
419 |
|
|
420 |
public boolean canWriteGeometry(int geometryType) throws DataException { |
|
421 |
// TODO Auto-generated method stub |
|
422 |
return false; |
|
423 |
} |
|
424 |
|
|
425 |
public Object createNewOID() { |
|
426 |
// TODO Auto-generated method stub |
|
427 |
return null; |
|
428 |
} |
|
429 |
|
|
430 |
public FeatureSetProvider createSet(FeatureQuery query, |
|
431 |
FeatureType featureType) throws DataException { |
|
432 |
// TODO Auto-generated method stub |
|
433 |
return null; |
|
434 |
} |
|
435 |
|
|
436 |
public void endAppend() throws DataException { |
|
437 |
// TODO Auto-generated method stub |
|
438 |
|
|
439 |
} |
|
440 |
|
|
441 |
public FeatureData getFeatureDataByReference( |
|
442 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
|
443 |
throws DataException { |
|
444 |
// TODO Auto-generated method stub |
|
445 |
return null; |
|
446 |
} |
|
447 |
|
|
448 |
public FeatureData getFeatureDataByReference( |
|
449 |
FeatureReferenceProviderServices reference) throws DataException { |
|
450 |
// TODO Auto-generated method stub |
|
451 |
return null; |
|
452 |
} |
|
453 |
|
|
454 |
public int getFeatureReferenceOIDType() { |
|
455 |
// TODO Auto-generated method stub |
|
456 |
return 0; |
|
457 |
} |
|
458 |
|
|
459 |
public String getName() { |
|
460 |
// TODO Auto-generated method stub |
|
461 |
return null; |
|
462 |
} |
|
463 |
|
|
464 |
public boolean supportsAppendMode() { |
|
465 |
// TODO Auto-generated method stub |
|
466 |
return false; |
|
467 |
} |
|
468 |
|
|
469 |
public void resourceChanged(ResourceProvider resource) { |
|
470 |
// TODO Auto-generated method stub |
|
471 |
|
|
472 |
} |
|
473 |
|
|
474 |
public void close() throws CloseException { |
|
475 |
super.close(); |
|
476 |
try { |
|
477 |
resourceBegin(); |
|
478 |
} catch (ResourceBeginException e) { |
|
479 |
new CloseException(this.getName(), e); |
|
480 |
} |
|
481 |
try { |
|
482 |
forceCloseAllResultSet(); |
|
483 |
} catch (DataException e) { |
|
484 |
new CloseException(this.getName(), e); |
|
485 |
} finally { |
|
486 |
resourceEnd(); |
|
487 |
} |
|
488 |
} |
|
489 |
|
|
277 | 490 |
} |
Also available in: Unified diff