Skip to content

Commit

Permalink
Logging and assertions to identify premature disposal of JDBCDataStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
smithkm authored and Kevin Smith committed Jul 16, 2014
1 parent f506886 commit 4ccf5c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Expand Up @@ -445,6 +445,22 @@ public void setSQLDialect(SQLDialect dialect) {
* @return The data source, never <code>null</code>.
*/
public DataSource getDataSource() {
if(dataSource==null){
// Should never return null so throw an exception
if(LOGGER.isLoggable(Level.FINE)) {
// At this log level, dispose() should store an exception
if(disposedBy==null) {
LOGGER.log(Level.SEVERE, "JDBCDataStore was never given a DataSource.");
throw new IllegalStateException("DataSource not available as it was never set.");
} else {
LOGGER.log(Level.WARNING, "JDBCDataStore was disposed.", disposedBy);
throw new IllegalStateException("DataSource not available after calling dispose().");
}
} else {
throw new IllegalStateException("DataSource not available after calling dispose() or before being set.");
}

}
return dataSource;
}

Expand All @@ -455,6 +471,8 @@ public DataSource getDataSource() {
* @param dataSource The data source, never <code>null</code>.
*/
public void setDataSource(DataSource dataSource) {
if(this.dataSource!=null) LOGGER.log(Level.WARNING, "Setting DataSource on JDBCDataStore that already has DataSource set");
if(dataSource==null) throw new NullPointerException("JDBCDataStore's DataSource should not be set to null");
this.dataSource = dataSource;
}

Expand Down Expand Up @@ -1768,9 +1786,7 @@ protected final Connection getConnection(JDBCState state) throws IOException {
protected final Connection createConnection() {
try {
LOGGER.fine( "CREATE CONNECTION");
if( getDataSource() == null ){
throw new NullPointerException("JDBC DataSource not available after dispose() has been called");
}

Connection cx = getDataSource().getConnection();
// isolation level is not set in the datastore, see
// http://jira.codehaus.org/browse/GEOT-2021
Expand Down Expand Up @@ -4554,6 +4570,7 @@ protected void finalize() throws Throwable {

}

private Throwable disposedBy=null;
public void dispose() {
if(dataSource != null && dataSource instanceof ManageableDataSource) {
try {
Expand All @@ -4564,6 +4581,10 @@ public void dispose() {
LOGGER.log(Level.FINE, "Could not close dataSource", e);
}
}
// Store the exception for logging later if the object is used after disposal
if(LOGGER.isLoggable(Level.FINE)) {
disposedBy = new RuntimeException("DataSource disposed in thread "+Thread.currentThread().getName());
}
dataSource = null;
}
/**
Expand Down
Expand Up @@ -251,7 +251,9 @@ public final JDBCDataStore createDataStore(Map params)
dataStore.setDataStoreFactory(this);

//call subclass hook and return
return createDataStoreInternal(dataStore, params);
JDBCDataStore result = createDataStoreInternal(dataStore, params);
assert result.getDataSource()!=null;
return result;
}

/**
Expand Down

0 comments on commit 4ccf5c2

Please sign in to comment.