@Override public void close() throws MetadataStoreException { JDBCUtils.close( rs, stmt, conn, LOG ); }
@Override public SQLDialect createDialect( final Connection conn ) { Statement stmt = null; ResultSet rs = null; String version = null; try { version = determinePostGISVersion( conn, LOG ); } catch ( Exception e ) { LOG.trace( e.getMessage(), e ); throw new ResourceInitException( e.getMessage(), e ); } finally { close( rs, stmt, null, LOG ); } return new PostGISDialect( version ); }
@Override public void setExpiryDate( long expiryDate ) throws FeatureStoreException { synchronized ( manager ) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = connection.getConnection(); conn.setAutoCommit( false ); stmt = conn.prepareStatement( "UPDATE LOCKS SET EXPIRES=? WHERE ID=?" ); stmt.setTimestamp( 1, new Timestamp( expiryDate ) ); stmt.setString( 2, id ); if ( stmt.executeUpdate() != 1 ) { String msg = "Could not reset expiry date for lock with id " + id; throw new FeatureStoreException( msg ); } expires = new Date( expiryDate ); conn.commit(); } catch ( SQLException e ) { rollbackQuietly( conn ); String msg = "Could not reset expiry date for lock with id " + id; LOG.debug( msg, e ); throw new FeatureStoreException( msg, e ); } finally { close( rs, stmt, conn, LOG ); } } }
private List<String> performUpdateRelational( QName ftName, List<ParsedPropertyReplacement> replacementProps, IdFilter filter ) throws FeatureStoreException, FilterEvaluationException { FeatureTypeMapping ftMapping = schema.getFtMapping( ftName ); FIDMapping fidMapping = ftMapping.getFidMapping(); int updated = 0; PreparedStatement stmt = null; try { String sql = createRelationalUpdateStatement( ftMapping, fidMapping, replacementProps, filter.getSelectedIds() ); if ( sql != null ) { LOG.debug( "Update: " + sql ); stmt = conn.prepareStatement( sql.toString() ); setRelationalUpdateValues( replacementProps, ftMapping, stmt, filter, fidMapping ); int[] updates = stmt.executeBatch(); for ( int noUpdated : updates ) { updated += noUpdated; } } } catch ( SQLException e ) { JDBCUtils.log( e, LOG ); throw new FeatureStoreException( JDBCUtils.getMessage( e ), e ); } finally { JDBCUtils.close( stmt ); } LOG.debug( "Updated {} features.", updated ); return new ArrayList<String>( filter.getMatchingIds() ); }
try { conn = getConnection( true ); useLegacyPredicates = JDBCUtils.useLegayPostGISPredicates( conn, LOG ); } catch ( MetadataStoreException e ) { throw new ResourceInitException( e.getMessage(), e ); } finally { JDBCUtils.close( conn ); throw new ResourceInitException( msg, e ); } finally { JDBCUtils.close( result, stmt, conn, LOG );
public static boolean useLegayPostGISPredicates( Connection conn, Logger log ) { boolean useLegacyPredicates = false; String version = determinePostGISVersion( conn, log ); if ( version.startsWith( "0." ) || version.startsWith( "1.0" ) || version.startsWith( "1.1" ) || version.startsWith( "1.2" ) ) { log.debug( "PostGIS version is " + version + " -- using legacy (pre-SQL-MM) predicates." ); useLegacyPredicates = true; } else { log.debug( "PostGIS version is " + version + " -- using modern (SQL-MM) predicates." ); } return useLegacyPredicates; }
@Override public void close() { LOG.debug( "Closing JDBC ResultSet, Statement and Connection" ); JDBCUtils.close( rs, stmt, conn, LOG ); }
@Override public void dropDB( Connection adminConn, String dbName ) throws SQLException { String sql = "DROP DATABASE \"" + dbName + "\""; Statement stmt = null; try { stmt = adminConn.createStatement(); stmt.executeUpdate( sql ); } finally { JDBCUtils.close( null, stmt, null, LOG ); } }
@Override public void createDB( Connection adminConn, String dbName ) throws SQLException { String sql = "CREATE DATABASE \"" + dbName + "\" WITH template=template_postgis"; Statement stmt = null; try { stmt = adminConn.createStatement(); stmt.executeUpdate( sql ); } finally { JDBCUtils.close( null, stmt, null, LOG ); } }
@Override public void rollback() throws MetadataStoreException { LOG.debug( "Performing rollback of transaction." ); try { conn.rollback(); } catch ( SQLException e ) { String msg = "Rollback failed: " + e.getMessage(); LOG.debug( msg ); throw new MetadataStoreException( msg ); } finally { JDBCUtils.close( conn ); } }
@Override public void commit() throws MetadataStoreException { LOG.debug( "Performing commit of transaction." ); try { conn.commit(); } catch ( SQLException e ) { String msg = "Commit failed: " + e.getMessage(); LOG.debug( msg ); throw new MetadataStoreException( msg ); } finally { JDBCUtils.close( conn ); } }
private List<RasterLevel> fetchScaleLevels( Connection conn ) throws SQLException { List<RasterLevel> scaleLevels = new ArrayList<RasterLevel>(); Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery( "SELECT id, level, minscale, maxscale FROM " + levelTableName + " ORDER BY minscale" ); while ( rs.next() ) { int id = rs.getInt( 1 ); int level = rs.getInt( 2 ); double minScale = rs.getDouble( 3 ); double maxScale = rs.getDouble( 4 ); RasterLevel o = new RasterLevel( id, level, minScale, maxScale ); scaleLevels.add( o ); } return scaleLevels; } finally { JDBCUtils.close( rs ); JDBCUtils.close( stmt ); } }
private RasterLevel[] fetchScaleLevels( Connection conn ) throws SQLException { List<RasterLevel> scaleLevels = new ArrayList<RasterLevel>(); Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery( "SELECT id, level, minscale, maxscale FROM " + levelTableName + " ORDER BY minscale" ); while ( rs.next() ) { int id = rs.getInt( 1 ); int level = rs.getInt( 2 ); double minScale = rs.getDouble( 3 ); double maxScale = rs.getDouble( 4 ); RasterLevel o = new RasterLevel( id, level, minScale, maxScale ); scaleLevels.add( o ); } return scaleLevels.toArray( new RasterLevel[scaleLevels.size()] ); } finally { JDBCUtils.close( rs ); JDBCUtils.close( stmt ); } }
private void deleteExistingRows( Connection connection, int operatesOnId, String databaseTable ) throws MetadataStoreException { PreparedStatement stmt = null; StringWriter sqlStatement = new StringWriter(); try { sqlStatement.append( "DELETE FROM " + databaseTable + " WHERE " + fk_main + " = ?" ); stmt = connection.prepareStatement( sqlStatement.toString() ); stmt.setInt( 1, operatesOnId ); LOG.debug( stmt.toString() ); stmt.executeUpdate(); } catch ( SQLException e ) { String msg = Messages.getMessage( "ERROR_SQL", sqlStatement.toString(), e.getMessage() ); LOG.debug( msg ); throw new MetadataStoreException( msg ); } finally { close( null, stmt, null, LOG ); } }
@Override public void commit() throws MetadataStoreException { LOG.debug( Messages.getMessage( "INFO_TA_COMMIT" ) ); try { conn.commit(); } catch ( SQLException e ) { String msg = Messages.getMessage( "ERROR_TA_COMMIT", e.getMessage() ); LOG.debug( msg ); throw new MetadataStoreException( msg ); } finally { JDBCUtils.close( conn ); } }
@Override public void rollback() throws MetadataStoreException { LOG.debug( Messages.getMessage( "INFO_TA_ROLLBACK" ) ); try { conn.rollback(); } catch ( SQLException e ) { String msg = Messages.getMessage( "ERROR_TA_ROLLBACK", e.getMessage() ); LOG.debug( msg ); throw new MetadataStoreException( msg ); } finally { JDBCUtils.close( conn ); } }
@Override public void release( String fid ) throws FeatureStoreException { synchronized ( manager ) { if ( isLocked( fid ) ) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = connection.getConnection(); stmt = conn.prepareStatement( "DELETE FROM LOCKED_FIDS WHERE FID=?" ); stmt.setString( 1, fid ); stmt.executeUpdate(); conn.commit(); --numLocked; } catch ( SQLException e ) { String msg = "Could not release locked feature: " + e.getMessage(); LOG.debug( msg, e ); throw new RuntimeException( msg, e ); } finally { close( rs, stmt, conn, LOG ); } } } }